<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <order-search @search="getList" ref="searchRef"> </order-search>
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
      >
        <!-- 操作列 -->
        <template v-slot:toolbar>
          <el-button
            type="primary"
            slot="reference"
            :disabled="selection.length == 0"
            @click="disposeList(1)"
            >批量处置</el-button
          >

          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            @confirm="remove"
            style="margin-left: 10px"
          >
            <template v-slot:reference>
              <el-button :disabled="selection.length == 0" type="danger"
                >批量删除</el-button
              >
            </template>
          </el-popconfirm>
        </template>
        <template v-slot:action="{ row }">
          <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
            slot="reference"
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            v-if="row.status == 0"
            @click="disposeList(2, row)"
          >
            处置
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>

    <el-dialog :visible.sync="dialogVisible" width="10%" @close="close">
      <DictSelection
        dictName="不良品处理类型"
        :filterArr="all ? ['返工返修'] : []"
        v-model="disposeType"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消 </el-button>
        <el-button type="primary" @click="dispose()">确 定</el-button>
      </span>
    </el-dialog>
    <Edit ref="edit" @success="save" />
  </div>
</template>

<script>
import OrderSearch from './components/detail-search.vue';
import Edit from './components/edit.vue';

import dictMixins from '@/mixins/dictMixins';
import { unacceptedProductStatus } from '@/utils/util';

import {
  getDetail,
  deleteUnacceptedProductDetail,
  dispose
} from '@/api/unacceptedProduct/index';
export default {
  components: {
    OrderSearch,
    Edit
  },
  mixins: [dictMixins],
  data() {
    return {
      loading: false,
      selection: [],
      datasource: [],
      disposeType: '',
      dialogVisible: false,
      current: null,
      all: false
    };
  },
  computed: {
    // 表格列配置
    columns() {
      return [
        {
          width: 45,
          type: 'selection',
          columnKey: 'selection',
          align: 'center',
          selectable: (row, index) => {
            return row.status == 0; //
          }
        },
        {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'code',
          label: '编码',
          align: 'center'
        },
        {
          prop: 'workOrderId',
          label: '工单号',
          align: 'center'
        },
        {
          prop: 'batchNo',
          label: '批次号',
          align: 'center'
        },
        {
          prop: 'engravingCode',
          label: '刻码',
          align: 'center'
        },
        {
          prop: 'productName',
          label: '产品名称',
          align: 'center'
        },
        {
          prop: 'productCode',
          label: '产品编码',
          align: 'center'
        },
        {
          prop: 'specification',
          label: '规格',
          align: 'center'
        },
        {
          prop: 'brandNo',
          label: '牌号',
          align: 'center'
        },

        {
          prop: 'unqualifiedQuantity',
          label: '数量',
          align: 'center',
          width: 60
        },
        {
          prop: 'produceRoutingName',
          label: '工艺路线',
          align: 'center'
        },
        {
          prop: 'produceTaskName',
          label: '工序',
          align: 'center'
        },
        {
          prop: 'unqualifiedReason',
          label: '原因',
          align: 'center'
        },

        {
          prop: 'createTime',
          label: '创建时间',
          align: 'center',
          width: 160
        },
        {
          prop: 'reviewerName',
          label: '创建人',
          align: 'center'
        },
        {
          prop: 'type',
          label: '处置类型',
          align: 'center',
          // slot: 'SlottingType'
          formatter: (row, column, cellValue) => {
            return this.getDictValue('不良品处理类型', cellValue + '');
          }
        },
        {
          prop: 'status',
          label: '状态',
          align: 'center',
          formatter: (row, column, cellValue) => {
            return unacceptedProductStatus(cellValue);
          }
        },

        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          fixed: 'right',
          slot: 'action',
          showOverflowTooltip: true
        }
      ];
    }
  },
  created() {
    this.getList();
  },

  methods: {
    getList() {
      getDetail(this.$route.query.id).then((res) => {
        this.datasource = res;
      });
    },
    // /* 表格数据源 */
    // async datasource({ page, limit, where }) {
    //   const result = await getDetail(this.$route.query.id);
    //   console.log(result, 'result');
    //   return {
    //     list: result,
    //     count: result.length
    //   };
    // },
    remove(row) {
      let ids = row ? [row.id] : this.selection.map((item) => item.id);
      deleteUnacceptedProductDetail(ids).then((res) => {
        this.$message.success('删除' + res);
        this.getList();
      });
    },
    close() {
      this.all = false;
      this.disposeType=''
    },
    // // 处置
    disposeList(type, row) {
      if (type == 1) {
        this.all = true;
      } else {
        this.current = row;
      }
      this.dialogVisible = true;
      // let ids = this.current ? [this.current.id] : this.selection.map((item) => item.id);
    },
    async dispose() {
      if (!this.disposeType) {
        return;
      }
      this.$refs.edit.open(this.all?this.selection:[this.current], this.disposeType);
      this.dialogVisible = false;
    },
    async save(data) {
      if (data.data && data.data.length > 0) {
        data.data.forEach((item) => {
          item['fid'] = item.id||this.current.id;
        });
      }
      let ids =
      data.type == 1 ? [this.current.id] : data.data.map((item) => item.id);
      await dispose({
        id: ids,
        disposeType: Number(data.type),
        poList: data.data
      });
      this.dialogVisible = false;
      this.current = null;
      this.$message.success('处置成功！');
      this.getList();
    }
  }
};
</script>

<style lang="scss" scoped>
.unacceptedProductSelect {
  display: flex;
  flex-direction: column;
  align-items: center;
}
</style>
