<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="1060px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="false"
    custom-class="ele-dialog-form"
    title="工序"
    @update:visible="updateVisible(false)"
  >
    <header-title title="基本信息"> </header-title>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="加工方式:" prop="produceType">
            <el-select
              v-model="form.produceType"
              style="width: 100%"
              @change="changeProduceType"
            >
              <el-option
                v-for="item of producedList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="BOM版本:" prop="bomCategoryId">
            <el-select
              v-model="form.bomCategoryId"
              style="width: 100%"
              @change="changeBomId"
            >
              <el-option
                v-for="item of bomVersionList"
                :key="item.id"
                :label="item.name + '(V' + item.versions + '.0)'"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工艺路线:" prop="produceRoutingId">
            <!--  @click.native="openVersion"   -->
            <el-select
              v-model="form.produceRoutingId"
              style="width: 100%"
              @change="changeRoute"
            >
              <el-option
                v-for="item of routingList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <ele-pro-table
      ref="table"
      :needPage="false"
      :columns="columns"
      :datasource="datasource"
      @cell-click="cellClick"
      row-key="id"
    >
      <template v-slot:orderNum="{ row }">
        {{ row.orderNum }}
      </template>
      <template v-slot:action="{ row }">
        <el-radio class="radio" v-model="radio" :label="row.id"
          ><i></i
        ></el-radio>
      </template>
    </ele-pro-table>

    <template v-slot:footer>
      <el-button type="primary" @click="save"> 确定 </el-button>
      <el-button @click="updateVisible(false)"> 返回 </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import { bomListByPlan, bomRoutingList, taskinstanceList } from '@/api/aps';

  export default {
    components: {},
    props: {
      // 弹窗是否打开
      visible: Boolean
    },
    data() {
      const defaultForm = {
        produceType: '',
        bomCategoryId: '',
        produceRoutingId: ''
      };
      return {
        defaultForm,
        radio: null,
        current: {},
        producedList: [
          { code: 2, name: '加工(MBOM)' },
          { code: 3, name: '装配(ABOM)' }
        ],
        index: '',
        bomVersionList: [],
        routingList: [],
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {},
        versionList: [],
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,

        columns: [
          {
            action: 'action',
            slot: 'action',
            align: 'center',
            label: '选择',
            width: 80
          },
          {
            prop: 'orderNum',
            label: '排序',
            align: 'center',
            slot: 'orderNum',
            width: 80
          },
          {
            prop: 'code',
            label: '工序编码',
            // sortable: 'custom',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },

          {
            prop: 'name',
            label: '工序名称',
            showOverflowTooltip: true,
            align: 'center',
            minWidth: 110
          },
          {
            align: 'center',
            prop: 'controlName',
            label: '工序控制码',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'workCenterName',
            label: '所属工作中心',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          }
        ]
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      open(row, index) {
        this.data = row;
        this.index = index;
      },
      changeProduceType() {
        this.form.bomCategoryId = '';
        this.form['bomCategoryName'] = '';
        this.form['bomCategoryVersions'] = '';

        this.bomVersionList = [];
        this.routingList = [];
        this.form.produceRoutingId = '';
        this.form.produceRoutingName = '';
        this.form.produceVersionName = '';
        this.bomListVersion();
      },
      bomListVersion() {
        let param = {
          bomType: this.form.produceType,
          categoryId: this.data.productId
        };
        bomListByPlan(param).then((res) => {
          this.bomVersionList = res || [];
        });
      },
      changeBomId() {
        this.routingList = [];
        this.form.produceRoutingId = '';
        this.form.produceRoutingName = '';
        this.form.produceVersionName = '';
        this.bomVersionList.forEach((f) => {
          if (f.id == this.form.bomCategoryId) {
            this.$set(this.form, 'bomCategoryName', f.name);
            this.$set(this.form, 'bomCategoryVersions', f.versions);
          }
        });
        this.getPlanRouting();
      },
      getPlanRouting() {
        bomRoutingList(this.form.bomCategoryId).then((res) => {
          this.routingList = res || [];
        });
      },
      changeRoute(val) {
        if (!val) return;
        this.routingList.forEach((f) => {
          if (f.id == this.form.produceRoutingId) {
            this.$set(this.form, 'produceRoutingName', f.name);
            this.$set(this.form, 'produceVersionName', f.version);
          }
        });
        this.$refs.table.reload();
      },
      /* 表格数据源 */
      async datasource() {
        if (this.form.produceRoutingId) {
          const res = await taskinstanceList({
            routingId: this.form.produceRoutingId,
            isDetail: true,
            pageNum: 1,
            size: -1
          });

          let arr = res.list.map((it) => {
            it.detail.orderNum = it.orderNum;
            return it.detail;
          });
          return {
            list: arr
          };
        } else {
          return {
            list: []
          };
        }

        //this.$refs.table.reload()
      },
      // 单击获取id
      cellClick(row) {
        this.current = row;
        this.radio = row.id;
      },
      /* 保存编辑 */
      save() {
        if (!this.radio) return this.$message.warning('请选择工序');
        this.$emit('saveTaskInstance', {
          ...this.current,
          index: this.index,
          produceRoutingId: this.form.produceRoutingId
        });
        this.updateVisible(false);
      },

      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },

    watch: {}
  };
</script>
