<template>
  <!-- 转外协 -->
  <el-dialog
    title="转外协"
    append-to-body
    :modelValue="showDialog"
    width="60%"
    @close="closeDialog"
  >
    <el-form :model="outsourceForm" :rules="outsourceRules" label-width="70px">
      <el-form-item label="备注：" prop="memo">
        <el-input
          placeholder="请输入备注"
          v-model="outsourceForm.memo"
          type="textarea"
          style="width: 40%"
        ></el-input>
      </el-form-item>
    </el-form>
    <el-table :data="outsourceData" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection"></el-table-column>
      <el-table-column
        align="center"
        label="工序号"
        prop="orders"
      ></el-table-column>
      <el-table-column
        align="center"
        label="工序代码"
        prop="workPlan.processSet.code"
      ></el-table-column>
      <el-table-column
        align="center"
        label="工序名称"
        prop="workPlan.processSet.name"
      ></el-table-column>

      <el-table-column align="center" label="工艺能力" prop="processAbility.name"> </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" :loading="loading">取 消</el-button>
        <el-button type="primary" @click="turnOem" :loading="loading">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { getListProProcess, transferBill } from '@/api/productionManagement/productionMonitoring';
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
    itemData: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      loading:false,
      outsourceForm: {},
      outsourceRules: {
        memo: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      outsourceData: [],
      selectedRows: [],
    };
  },
  mounted() {
    this.getListProProcess();
  },
  methods: {
    // 获取转外协数据
    getListProProcess() {
      let params = {
        woIds: this.itemData.map(item => item.woId),
        runType: 1, //转换类型：1-外协，2-厂内
      };
      getListProProcess(params).then(res => {
        this.outsourceData = res.data.data;
      });
    },
    closeDialog() {
      this.$emit('closeDialog');
    },
    // 监听选择变化
    handleSelectionChange(selection) {
      this.selectedRows = selection;
    },
    // 确定
    turnOem() {
      if (this.outsourceData.length == 0) {
        this.$message.error('当前数据列表为空');
        return;
      }
      if (this.selectedRows.length == 0) {
        this.$message.error('请选择需要转外协的数据');
        return;
      }
      this.loading = true
      let orderData = [];
      this.itemData.forEach(item => {
        item.runType = 1;
        orderData.push({
          runType: 1,
          woId: item.woId,
        });
      });
      let query = {
        workOrderRuns: orderData,
        workPlanRuns: this.selectedRows,
      };

      transferBill(query).then(res => {
        this.$message.success('操作成功');
        this.closeDialog();
        this.loading = false
      }).catch(err=>{
        this.loading = false
      })
    },
  },
};
</script>

<style></style>
