<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 数据表格 -->
  
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        row-key="id"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="add"
            >新增</el-button
          >
        </template>
        <!-- 状态列 -->
        <template v-slot:status="{ row }">
          {{ checkStatus(row) }}
        </template>

        <template v-slot:routeType="{ row }">
         {{ row.routeType == 2 ? '委外' :  row.routeType == 1 ? '生产' :   row.routeType == 3 ? '质检' : ''}} 
        </template>

        <template v-slot:action="{ row, $index }">
          <el-link type="danger" @click="handleDel(row)">删除</el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <routingDialog ref="routingDialogRef" @reload="reload"></routingDialog>
  </div>
</template>

<script>
  import { getMbomPage } from '@/api/material/BOM';
  import routingDialog from './routingDialog.vue';

  import { workingProcedureUpdate } from '@/api/material/BOM';
  export default {
    name: 'technologyRoute',
    components: {
      routingDialog
    },
    props: {
      taskParam: Object
    },
    data() {
      return {
        tableData: [],
        selection: [],
        versionList: [],
        where: {},
        // 表格列配置
        columns: [
          {
            prop: 'code',
            label: '工艺路线组编码',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110,
            slot: 'code'
          },
          {
            prop: 'name',
            label: '工艺路线名称',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },

          {
            prop: 'version',
            label: '工艺路线版本',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },

          {
            prop: 'produceVersionName',
            label: '生产版本',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'status',
            label: '状态',
            align: 'center',
            slot: 'status',
            showOverflowTooltip: true,
            minWidth: 110
          },

          {
            slot: 'routeType',
            label: '类型',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'action',
            label: '操作',
            align: 'center',
            slot: 'action'
          }
        ],

        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        detailEdit: false,

        statusList: [
          { label: '草稿', value: -1 },
          { label: '失效', value: 0 },
          { label: '生效', value: 1 }
        ],
        loading: false
      };
    },
    methods: {
      add() {
    
        this.$refs.routingDialogRef.open(this.taskParam, this.tableData);
      },
      search() {
        this.reload(this.where);
      },
      /* 表格数据源 */
      async datasource({ page, limit, where }) {
        let data = await getMbomPage({
          ...where,
          bomCategoryId: this.taskParam.id,
          pageNum: page,
          size: limit
        });
        if (data?.length > 0) {
          this.tableData = data[0];
          return data[0].processRoute.list || [];
        } else {
          this.tableData = {};
          return [];
        }
      },

      handleDel(row) {

        workingProcedureUpdate({
          id: this.tableData.id,
          categoryId: this.taskParam.categoryId,
          bomCategoryId: this.taskParam.id,
          categoryCode: this.taskParam.categoryCode,
          processRoute: {
            list: this.tableData.processRoute.list.filter(
              (item) => item.id !== row.id
            )
          }
        }).then((data) => {
          this.$message.success('删除成功');
          this.reload();
        });
      },
      checkStatus(row) {
        let obj = this.statusList.find((it) => it.value == row.status);
        return obj.label;
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      }
    }
  };
</script>
