<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
            v-if="attributeData.status != 1 && !isWt"
            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)"
            v-if="
              attributeData.approvalStatus != 1 &&
              attributeData.approvalStatus != 2
            "
            >删除</el-link
          >
        </template>
      </ele-pro-table>
    </el-card>
    <routingDialog ref="routingDialogRef" @reload="reload"></routingDialog>
  </div>
</template>

<script>
  import { getMbomPage } from '@/api/produce/bom.js';
  import routingDialog from './routingDialog.vue';
  import route from '@/api/technology/route';

  import { workingProcedureUpdate } from '@/api/produce/bom.js';
  export default {
    name: 'technologyRoute',
    components: {
      routingDialog
    },
    props: {
      taskParam: Object,
      resourceBomId: String,

      attributeData: {
        type: Object,
        default: {}
      },

      isWt: {
        type: Boolean,
        default: false
      }
    },
    watch: {
      attributeData: {
        handler(val) {
          this.$nextTick(() => {
            this.reload();
          });
        },
        deep: true,
        immediate: true
      }
    },
    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() {
        console.log(this.taskParam, this.tableData);

        this.$refs.routingDialogRef.open(this.taskParam, this.tableData);
      },
      search(e) {
        this.reload(this.where);
      },
      /* 表格数据源 */
      async datasource({ page, limit, where }) {
        let data = await getMbomPage({
          ...where,
          bomCategoryId: this.resourceBomId,
          pageNum: page,
          size: limit
        });
        if (data?.length > 0) {
          this.tableData = data[0];
          return data[0].processRoute.list || [];
        } else {
          this.tableData = {};
          return [];
        }
      },

      handleDel(row) {
        // route.getProcessById([row.id]).then((data) => {

        // })

        // console.log(row,'41444444',this.tableData.processRoute.list);
        // return
        this.$confirm('是否删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            workingProcedureUpdate({
              id: this.tableData.id,
              categoryId: this.taskParam.categoryId,
              bomCategoryId: this.taskParam.id,
              categoryCode: this.taskParam.categoryCode,
              isRouting: 1,
              processRoute: {
                list: this.tableData.processRoute.list.filter(
                  (item) => item.id !== row.id
                )
              }
            }).then((data) => {
              this.$message.success('删除成功');
              this.reload();
            });
          })
          .catch(() => console.info('操作取消'));
      },
      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>
