<!-- 不良品列表 -->
<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <order-search @search="reload" ref="searchRef"> </order-search>
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :pageSize="20"
        :height="tableHeight"
        @fullscreen-change="fullscreenChange"
      >
        <!-- 操作列 -->
        <template v-slot:toolbar>
          <el-button type="primary" @click="open()">新建</el-button>
        </template>

        <template v-slot:unqualifiedProductsCode="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="goDetail(row, 'detail')"
            >{{ row.unqualifiedProductsCode }}</el-link
          >
        </template>

        <template v-slot:qualityType="{ row }">
          {{ getQuality(row) }}
        </template>

        <template v-slot:quantity="{ row }">
          {{ row.quantity }}{{ row.measureUnit }}
        </template>

        <template v-slot:status="{ row }">
          {{ getDispose(row) }}
        </template>
        <template v-slot:action="{ row }">
          <el-link
            v-if="row.status == 0"
            type="primary"
            :underline="false"
            icon="el-icon-truck"
            @click="open(row)"
          >
            编辑
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            v-if="row.status == 0"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
          <el-link
            v-if="row.status == 0 || row.status == 1"
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="goDetail(row)"
          >
            处置
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <Create ref="create" @refresh="reload" />
  </div>
</template>

<script>
  import OrderSearch from './components/order-search.vue';
  import dictMixins from '@/mixins/dictMixins';
  import { unacceptedProductStatus } from '@/utils/util';
  import Create from './components/create.vue';
  import { getByCode } from '@/api/system/dictionary-data';

  import { getList } from '@/api/unacceptedProduct/index';
  import { getQualityDetails } from '@/api/manufacture/spreadWorkOrder';
  export default {
    components: {
      OrderSearch,
      Create
    },
    mixins: [dictMixins],
    data() {
      return {
        visible: false,
        loading: false,
        releasParams: {
          teamId: '',
          id: ''
        },

        current: null,
        qualityList: [],
        disposeList: [],
        tableHeight: 'calc(100vh - 340px)'
      };
    },
    computed: {
      // 表格列配置
      columns() {
        return [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'unqualifiedProductsCode',
            label: '编码',
            slot: 'unqualifiedProductsCode',
            align: 'center',
            width: 150,
            showOverflowTooltip: true
          },
          {
            prop: 'sourceCode',
            label: '来源编码',
            align: 'center',
            width: 150,
            showOverflowTooltip: true
          },
          {
            prop: 'categoryCode',
            label: '物品编码',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          },
          {
            prop: 'categoryName',
            label: '物品名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'specification',
            label: '规格',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          {
            prop: 'brandNum',
            label: '牌号',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          {
            prop: 'modelType',
            label: '型号',
            align: 'center',
            width: 160,
            showOverflowTooltip: true
          },
          {
            prop: 'quantity',
            slot: 'quantity',
            label: '数量',
            align: 'center',
            width: 80,
            showOverflowTooltip: true
          },
          // {
          //   prop: 'unit',
          //   label: '计量单位',
          //   align: 'center',
          //   width: 100,
          //   showOverflowTooltip: true
          // },
          {
            prop: 'batchNo',
            label: '批次号',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          {
            prop: 'workOrderCode',
            label: '工单编号',
            align: 'center',
            width: 100,
            showOverflowTooltip: true
          },
          // {
          //   prop: 'produceRoutingName',
          //   label: '工艺路线',
          //   align: 'center',
          //   width: 120,
          //   showOverflowTooltip: true
          // },
          // {
          //   prop: 'taskName',
          //   label: '工序',
          //   align: 'center',
          //   width: 120,
          //   showOverflowTooltip: true
          // },
          {
            prop: 'disposeTime',
            label: '处置时间',
            align: 'center',
            width: 180,
            showOverflowTooltip: true
          },
          {
            prop: 'qualityType',
            slot: 'qualityType',
            label: '质检类型',
            align: 'center',
            width: 120,
            showOverflowTooltip: true
          },
          {
            prop: 'status',
            slot: 'status',
            label: '状态',
            align: 'center',
            width: 80
            // formatter: (row, column, cellValue) => {
            //   console.log(this.getDictName('处置状态', cellValue));

            //   return this.getDictName('处置状态', cellValue);
            // }
          },

          {
            columnKey: 'action',
            label: '操作',
            width: 200,
            align: 'center',
            resizable: false,
            fixed: 'right',
            slot: 'action'
          }
        ];
      }
    },
    created() {
      this.requestDict('质检计划类型');
      this.requestDict('处置状态');

      this.getDisposeList('dispose_status');
      this.getQualityList('inspection_plan_type');
    },
    filters: {},
    methods: {
      //处置
      goDetail(row, type) {
        this.$router.push({
          path: '/unacceptedProduct/detailList',
          query: {
            id: row.id,
            qualityType: row.qualityType,
            workOrderCode: row.workOrderCode,
            type
          }
        });
      },
      /* 表格数据源 */
      datasource({ page, limit, where }) {
        return getList({
          pageNum: page,
          size: limit,
          ...where
        });
      },
      open(row) {
        this.$refs.create.open(row ? JSON.parse(JSON.stringify(row)) : '');
      },
      remove(row) {
        deleteUnacceptedProduct(row.id).then((res) => {
          this.$message.success('删除' + res);
          this.reload();
        });
      },
      // // 下达
      // toRelease(row) {
      //   this.current = row;
      //   this.visible = true;
      // },
      // // 下达
      // confirm(row) {
      //   this.$confirm('确认下发选中的任务吗？', '提示', {
      //     confirmButtonText: '确定',
      //     cancleButtonText: '取消',
      //     type: 'warning'
      //   })
      //     .then(async () => {
      //       const res = await issuedPutMeshWorkOrder({
      //         id: [row.id],
      //         status: 1
      //       });
      //       this.$message.success('任务下发成功！');
      //       this.reload();
      //     })
      //     .catch(() => {});
      // },
      /* 刷新表格 */
      reload(where) {
        this.$nextTick(() => {
          this.$refs.table.reload({ page: 1, where });
        });
      },

      fullscreenChange(fullscreen) {
        if (fullscreen) {
          this.tableHeight = 'calc(100vh - 120px)';
        } else {
          this.tableHeight = 'calc(100vh - 340px)';
        }
      },

      getDispose(item) {
        const data = this.disposeList.find((it) => it.value == item.status);

        return data ? data.label : '';
      },

      getQuality(item) {
        for (let i = 0; i < this.qualityList.length; i++) {
          if (this.qualityList[i].value == item.qualityType) {
            return this.qualityList[i].label;
          }
        }
      },

      async getDisposeList(code) {
        let res = await getByCode(code);
        if (res?.code == 0) {
          let list = res.data.map((item) => {
            let key = Object.keys(item)[0];
            return { value: Number(key), label: item[key] };
          });
          this.disposeList = list;
        }
      },
      async getQualityList(code) {
        let res = await getByCode(code);
        if (res?.code == 0) {
          let list = res.data.map((item) => {
            let key = Object.keys(item)[0];
            return { value: Number(key), label: item[key] };
          });
          this.qualityList = list;
        }
      }
    }
  };
</script>

<style lang="scss" scoped></style>
