<template>
  <!-- 转外协 -->
  <el-dialog
    title="转外协"
    append-to-body
    :modelValue="showDialog"
    width="90%"
    @close="closeDialog"
    @open="handleOpen"
  >
    <el-form :model="outsourceForm" :rules="outsourceRules" label-width="100px">
      <el-form-item label="备注" prop="memo">
        <el-input placeholder="请输入备注" v-model="outsourceForm.memo" type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <el-table :data="outsourceData">
      <el-table-column align="center" type="selection"></el-table-column>
      <el-table-column align="center" type="index"></el-table-column>
      <el-table-column align="center" label="工序号" prop="prWorkPlan.orders"></el-table-column>
      <el-table-column
        align="center"
        label="工序代码"
        prop="prWorkPlan.procedureSet.ppsCode"
      ></el-table-column>
      <el-table-column
        align="center"
        label="工序名称"
        prop="prWorkPlan.procedureSet.ppsName"
      ></el-table-column>
    
      <el-table-column align="center" label="工艺能力" prop="bsCraftAbility.caId">
        <template #default="scope">
          <el-select v-model="scope.row.bsCraftAbility.caId">
            <el-option
              v-for="item in craftData"
              :key="item.caId"
              :label="item.caName"
              :value="item.caId"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      
      <el-table-column align="center" label="工序描述" prop="makeMemo"> </el-table-column>
      <el-table-column align="center" label="不可转外协原因" prop="reason"> </el-table-column>
    </el-table>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="turnOem">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
    showRadio: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      outsourceForm: {},
      outsourceRules: {
        memo: [{ required: true, message: '请输入转外协备注', trigger: 'blur' }],
      },
      outsourceData: [],
      craftData: [
        {
          bsWorkType: null,
          caCode: 'E3240',
          caId: 25,
          caName: '化学氧化',
          keyValue: 25,
          updateTime: '2023-04-03 18:35:23',
        },
        {
          bsWorkType: null,
          caCode: 'E802',
          caId: 26,
          caName: '镀后检验',
          keyValue: 26,
          updateTime: '2023-04-11 15:31:32',
        },
        {
          bsWorkType: null,
          caCode: 'E390',
          caId: 27,
          caName: '超声波清洗',
          keyValue: 27,
          updateTime: '2023-04-11 10:19:50',
        },
        {
          bsWorkType: null,
          caCode: 'E379',
          caId: 28,
          caName: '滚镀金或振镀金',
          keyValue: 28,
          updateTime: '2023-04-11 10:20:29',
        },
        {
          bsWorkType: null,
          caCode: 'E386',
          caId: 36,
          caName: '化学镀镍',
          keyValue: 36,
          updateTime: '2023-04-11 15:31:25',
        },
      ],
    };
  },
  mounted() {},
  methods: {
    closeDialog() {
      this.$emit('closeDialog');
    },
    // 切换计划结束时间
    changeEndTime(val, index) {
      console.log('val-------------', val);
      console.log('index-------------', index);
      if (val) {
        if (this.outsourceData.length > index) {
          this.outsourceData[index + 1].startTime = val;
        }
      }
    },
    // 送审
    turnOem() {
      if (this.outsourceData.length == 0) {
        this.$message.error('当前数据列表为空');
        return;
      }
      var bool = false;
      var isCheck = false;
      this.outsourceData.forEach(item => {
        if (item.handle && (!item.startTime || !item.endTime)) {
          bool = true;
        }
        if (item.handle) {
          isCheck = true;
        }
      });
      if (bool) {
        this.$message.error('请维护外协工序的计划开始/结束日期!');
        return;
      }
      if (!isCheck) {
        this.$message.error('请勾选需要外协的工序');
        return;
      }
      this.closeDialog();
    },
    handleOpen() {
      this.outsourceData = [
        {
          // 工序号/代码/名称相关嵌套结构
          prWorkPlan: {
            orders: 'GX001',
            procedureSet: {
              ppsCode: 'PCS001',
              ppsName: '粗加工',
            },
          },
          // 工艺能力相关嵌套结构
          bsCraftAbility: {
            caId: 1,
          },
          // 工序描述
          makeMemo: '该工序完成零件外圆粗车加工',
          // 不可转外协原因
          reason: '外协无成本优势',
        },
        {
          prWorkPlan: {
            orders: 'GX002',
            procedureSet: {
              ppsCode: 'PCS002',
              ppsName: '精加工',
            },
          },
          bsCraftAbility: {
            caId: 2,
          },
          makeMemo: '需使用硬质合金刀具',
          reason: '外协厂商无对应高精度铣床',
        },
      ];
    },
  },
};
</script>

<style></style>
