<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-table ref="table"
             size="middle"
             :scroll="{ x: true }"
             bordered
             rowKey="id"
             :columns="columns"
             :dataSource="dataSource"
             :loading="loading">
      <span slot="action" slot-scope="text, record">
         <input-number v-model="record.replaceQuantity" :min="0" :max="record.quantity" @change="increaseQuantity(record)" />
      </span>
    </a-table>
  </j-modal>
</template>

<script>
import { getAction, postAction } from '@api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { mixinDevice } from '@/utils/mixin'

export default {
  name: 'MaterialRecordModal',
  mixins: [JeecgListMixin, mixinDevice],
  props: {
    onRefresh: {
      type: Function,
      default: () => {}
    }
  },
  data() {
    return {
      title: "申请配件更换",
      width: 800,
      visible: false,
      disableSubmit: false,
      workOrderId: '',
      loading: false,
      dataSource: [],
      columns: [
        {
          title: '序号',
          align: "center",
          dataIndex: 'seq',
          key: 'seq'
        },
        {
          title: '物料名称',
          align: "center",
          dataIndex: 'itemName'
        },
        {
          title: '规格',
          align: "center",
          dataIndex: 'specification'
        },
        {
          title: '使用数量',
          align: "center",
          dataIndex: 'quantity'
        },
        {
          title: '申请更换数量',
          align: "center",
          dataIndex: 'replaceQuantity',
          key: 'replaceQuantity',
          fixed:"right",
          width:147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      urls: {
        queryListByWorkOrderId: "/apply/applyDetails/queryListByWorkOrderId",
        addBatch: "/workOrder/replacementRecords/addBatch"
      }
    }
  },
  methods: {
    add(id) {
      this.workOrderId = id;
      this.loading = true;
      getAction(this.urls.queryListByWorkOrderId, { workOrderId: this.workOrderId }).then(res => {
        if (res.success) {
          const materialData = res.result.records; // 假设返回的数据在 records 字段中
          console.log("materialData", materialData);
          this.dataSource = materialData.map((item, index) => ({
            ...item,
            seq: index + 1, // 自动填充序号
            replaceQuantity: 0 // 初始化更换数量为0
          }));
          this.visible = true; // 显示对话框
        } else {
          this.$message.error(res.message);
        }
      }).catch(err => {
        this.$message.error('请求失败: ' + err.message);
      }).finally(() => {
        this.loading = false;
      });
    },
    handleOk() {
      const selectedData = this.dataSource.filter(item => item.replaceQuantity > 0);
      console.log("更换数量不为0的数据:", selectedData);
      const replacementRecords = selectedData.map(item => ({
        workOrder: this.workOrderId,
        materialCode: item.itemId, // itemId 是物料编码
        materialDescription: item.itemName, // itemName 是物料描述
        quantity: item.replaceQuantity,
        unit: item.unit,
        seq: item.seq, // 暂不清楚这个值是什么，先这么传
        createBy: item.createBy,
        updateBy: item.updateBy,
        sysOrgCode: item.sysOrgCode
      }));

      // 发送 POST 请求到后端
      postAction(this.urls.addBatch, replacementRecords).then(res => {
        if (res.success) {
          this.$message.success('添加成功！');
          this.visible = false;
          this.$emit('refreshTable'); // 触发自定义事件
        } else {
          this.$message.error('添加失败: ' + res.message);
        }
      }).catch(err => {
        this.$message.error('请求失败: ' + err.message);
      });
    },
    handleCancel() {
      this.visible = false;
    },
    increaseQuantity(record) {
      if (record.replaceQuantity < record.quantity) {
        record.replaceQuantity++;
      }
    },
    decreaseQuantity(record) {
      if (record.replaceQuantity > 0) {
        record.replaceQuantity--;
      }
    }
  }
}
</script>

<style scoped>
/* 可以添加一些样式 */
</style>
