<script>
// 定义表格列
import { getAction, postAction } from '@api/manage'

const columns = [
  {
    title: "序号",
    scopedSlots: { customRender: "index" },
    width: "10%",
  },
  {
    title: "名称",
    dataIndex: "name",
    width: "25%",
  },
  {
    title: "型号",
    dataIndex: "model",
    width: "25%",
  },
  {
    title: "数量",
    dataIndex: "quantity",
    width: "15%",
  },
  {
    title: "单位",
    dataIndex: "unit",
    width: "15%",
  },
];
export default {
  name: 'ApplyDialog',
  props:{
    // 控制对话框显示/隐藏
    value: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      required: true
    },
    orderId:{
      type: String,
      required: true
    }
  },
  data(){
    return{
      columns, // 表格列定义
      applyType: 0,
      visible: this.value, // 对话框显示状态
      dataSource: [], // 表格数据
      url: {
        materials: "/regular/regularMaterial/workorder/",
        tools: "/regular/regularTools/workorder/",
        apply: "/apply/applyRecord/add"
      },
    }
  },
  watch: {
    // 监听外部传入的 value 属性，同步 visible 状态
    value(newVal) {
      this.visible = newVal;
    },
    // 监听外部传入的 data 属性，更新表格数据
    data(newVal) {
      this.dataSource = newVal;
    },
  },
  mounted() {
    this.loadData()
  },
  methods: {
    loadData(){
      if(this.title.indexOf("物料") >=0 ){
        this.applyType = 0;
        this.dataSource = []
        //加载物料数据
        getAction(this.url.materials + this.orderId).then((res) => {
          this.dataSource = res.result.map(item => {
            return {
              id: item.materialId,
              name: item.materialId_dictText,
              unit: item.unit_dictText,
              quantity: item.count,
              model: item.specification,
            }
          });
        })
      }else{
        this.applyType = 1;
        this.dataSource = []
        //加载工具数据
        getAction(this.url.tools + this.orderId).then((res) => {
          this.dataSource = res.result.map(item => {
            return {
              id: item.toolId,
              name: item.toolId_dictText,
              unit: item.unit_dictText,
              quantity: item.count,
              model: item.specification,
            }
          });
        })
      }
    },
    // 点击确定按钮
    handleOk() {
      postAction(this.url.apply, {
        workOrder: this.orderId,
        applyType: this.applyType,
        details: this.dataSource.map(item => {
          return {
            itemId: item.id,
            specification: item.specification,
            quantity: item.quantity,
          }
        })
      }).then(res => {
        console.log(res)
        this.visible = false;
        this.$emit("ok", this.dataSource); // 触发确认事件，传递数据
        this.$emit("input", false); // 更新 v-model 的值
      })
    },
    // 点击取消按钮
    handleCancel() {
      this.visible = false;
      this.$emit("input", false); // 更新 v-model 的值
      this.$emit("cancel"); // 触发取消事件
    },
  },
}
</script>

<template>
  <a-modal
    v-model="visible"
    :title="title"
    :width="800"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-table
      :columns="columns"
      :data-source="dataSource"
      :pagination="false"
      row-key="id"
    >
      <template slot="index" slot-scope="text, record, index">
        {{ index + 1 }}
      </template>
    </a-table>

    <template slot="footer">
      <a-button @click="handleCancel">取消</a-button>
      <a-button :disabled="dataSource.length <= 0" type="primary" @click="handleOk">立即申请</a-button>
    </template>
  </a-modal>
</template>

<style scoped>

</style>