<!--
 * @Descripttion: (接口管理/SAP交验单记录表/sap_deliver_record)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2023-05-10)
 * @LastEditors: (admin)
 * @LastEditTime: (2023-05-10)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent
      size="large">
      <el-row>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="queryParams.materialName" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="queryParams.materialCode" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="BN / SN码" prop="bnSnCode">
          <el-input v-model="queryParams.bnSnCode" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="规格型号 / 图号" prop="materialSpec">
          <el-input v-model="queryParams.materialSpec" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="物料类型" prop="category">
          <!-- <el-select v-model="queryParams.category" placeholder="请选择" clearable style="width:120px" @change="handleQuery">
            <el-option v-for="dict in options.materialTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue">
              <dict-tag :options="options.materialTypeOptions" :value="dict.dictValue" />
            </el-option>
          </el-select> -->
          <el-radio-group v-model="queryParams.category" @change="handleQuery">
            <el-radio-button>全部</el-radio-button>
            <el-radio-button v-for="dict in options.materialTypeOptions" :label="dict.dictValue">
              {{ dict.dictLabel }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="关键件标识" prop="keyFlag">
          <!-- <el-select v-model="queryParams.keyFlag" placeholder="请选择" clearable style="width:120px" @change="handleQuery">
            <el-option v-for="dict in options.keyFlagOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue">
              <dict-tag :options="options.keyFlagOptions" :value="dict.dictValue" />
            </el-option>
          </el-select> -->
          <el-radio-group v-model="queryParams.keyFlag" @change="handleQuery">
            <el-radio-button>全部</el-radio-button>
            <el-radio-button v-for="dict in options.keyFlagOptions" :label="dict.dictValue">
              {{ dict.dictLabel }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
          <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- <el-button type="danger" :disabled="multiple" v-hasPermi="['business:sapdeliverrecord:delete']" plain
          icon="delete" @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button> -->
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <!-- 数据区域 -->
    <el-table :data="dataList" v-loading="loading" ref="table" highlight-current-row>
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="category" label="物料类型" align="center" :show-overflow-tooltip="true" width="80"
        v-if="columns.showColumn('category')">
        <template #default="scope">
          <dict-tag :options="options.materialTypeOptions" :value="scope.row.category" />
        </template>
      </el-table-column>
      <el-table-column prop="materialCode" label="物料编码" align="center" :show-overflow-tooltip="true" width="150"
        v-if="columns.showColumn('materialCode')" />
      <el-table-column prop="bnSnCode" label="BN / SN码" align="center" :show-overflow-tooltip="true" width="150"
        v-if="columns.showColumn('bnSnCode')" />
      <el-table-column prop="materialName" label="物料名称" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('materialName')" />
      <el-table-column prop="materialSpec" label="规格型号 / 图号" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('materialSpec')" />
      <el-table-column prop="keyFlag" label="关键件" align="center" v-if="columns.showColumn('keyFlag')" width="80">
        <template #default="scope">
          <el-icon v-if="scope.row.keyFlag" size="large" color="red">
            <StarFilled />
          </el-icon>
          <el-icon v-else />
        </template>
      </el-table-column>
      <el-table-column prop="sumQaQty" label="合格数量" align="center" v-if="columns.showColumn('sumQaQty')" width="80" />
      <el-table-column prop="sumInstoreQty" label="入库数量" align="center" v-if="columns.showColumn('sumInstoreQty')"
        width="80" />
      <el-table-column prop="sumOutstoreQty" label="出库数量" align="center" v-if="columns.showColumn('sumOutstoreQty')"
        width="80" />
      <el-table-column prop="sumAssQty" label="装配数量" align="center" v-if="columns.showColumn('sumAssQty')" width="80" />
      <el-table-column label="追溯" align="center" width="400">
        <template #default="scope">
          <el-button-group>
            <el-button plain type="primary" @click="handleReportQa(scope.row)" icon="view">质检</el-button>
            <el-button plain type="primary" @click="handleReportIn(scope.row)" icon="download"> 入库 </el-button>
            <el-button plain type="primary" @click="handleReportOut(scope.row)" icon="upload"> 出库 </el-button>
            <el-button plain type="primary" @click="handleReportAss(scope.row)" icon="setting"> 装配 </el-button>
            <!-- <el-button plain type="primary" @click="handleReport(scope.row)" icon="clock"> 时间线 </el-button> -->
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <pagination class="mt10" background :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 关键件信息追溯对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open" @close="closeDialog" width="70%">
      <QaRecord v-if="openType == 1" :tranceParams="tranceParams" />
      <InRecord v-if="openType == 2" :tranceParams="tranceParams" />
      <OutRecord v-if="openType == 3" :tranceParams="tranceParams" />
      <AssRecord v-if="openType == 4" :tranceParams="tranceParams" />
    </el-dialog>
  </div>
</template>

<script setup name="report">

import { reportQaInspectRecord } from '@/api/business/qainspectrecord.js';

import QaRecord from '@/views/tables/QaRecord'
import InRecord from '@/views/tables/InstoreRecord'
import OutRecord from '@/views/tables/OutstoreRecord'
import AssRecord from '@/views/tables/AssembleRecord'

const { proxy } = getCurrentInstance()
// 选中id数组数组
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
})
const columns = ref([
  { visible: true, prop: 'category', label: '物料类型' },
  { visible: true, prop: 'materialCode', label: '物料编码' },
  { visible: true, prop: 'bnSnCode', label: 'BN / SN码' },
  { visible: true, prop: 'materialName', label: '物料名称' },
  { visible: true, prop: 'materialSpec', label: '规格型号 / 图号' },
  { visible: true, prop: 'keyFlag', label: '关键件' },
  { visible: true, prop: 'sumQaQty', label: '质检数量' },
  { visible: true, prop: 'sumInstoreQty', label: '入库数量' },
  { visible: true, prop: 'sumOutstoreQty', label: '出库数量' },
  { visible: true, prop: 'sumAssQty', label: '装配数量' },
])

const total = ref(0)
const dataList = ref([])
const queryRef = ref()

function getList() {
  loading.value = true
  reportQaInspectRecord(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}
/*************** form操作 ***************/
const formRef = ref()
const title = ref("")
//  1质检  2入库  3出库  4装配
const openType = ref(0)
const open = ref(false)
const tranceParams = reactive({
  materialCode: undefined,
  bnSnCode: undefined
})
const state = reactive({
  form: {},
  options: {
    keyFlagOptions: [], // 关键件标识字典
    operationResultOptions: [],// 操作结果字典
    materialTypeOptions: [],// 物料类型字典
    qaResultOptions: [],//质检结果字典
  }
})

const { form, rules, options } = toRefs(state)

var dictParams = [
  { dictType: 'my_key_flag', columnName: 'keyFlagOptions' },
  { dictType: 'my_operation_result', columnName: 'operationResultOptions' },
  { dictType: 'my_material_type', columnName: 'materialTypeOptions' },
  { dictType: 'my_qa_result', columnName: 'qaResultOptions' },
]
proxy.getDicts(dictParams).then((response) => {
  response.data.forEach((element) => {
    state.options[element.columnName] = element.list
  })
})
// 打开dialog
function handleReportQa(row) {
  openType.value = 1
  title.value = "【质检记录】  " + row.materialName
  tranceParams.materialCode = row.materialCode
  tranceParams.bnSnCode = row.bnSnCode
  open.value = true
}
function handleReportIn(row) {
  openType.value = 2
  title.value = "【入库记录】  " + row.materialName
  tranceParams.materialCode = row.materialCode
  tranceParams.bnSnCode = row.bnSnCode
  open.value = true
}
function handleReportOut(row) {
  openType.value = 3
  title.value = "【出库记录】  " + row.materialName
  tranceParams.materialCode = row.materialCode
  tranceParams.bnSnCode = row.bnSnCode
  open.value = true
}
function handleReportAss(row) {
  openType.value = 4
  title.value = "【装配记录】  " + row.materialName
  tranceParams.materialCode = row.materialCode
  tranceParams.bnSnCode = row.bnSnCode
  open.value = true
}
// 关闭dialog
function closeDialog() {
  openType.value = 0
  open.value = false
  tranceParams.materialCode = undefined
  tranceParams.bnSnCode = undefined
}

handleQuery()
</script>
