ShipmentPickDetailModal.vue 5.53 KB
<template>
  <j-modal
    :title="title"
    :width="modalWidth"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="物料编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="matnr">
              <a-input v-model="model.matnr" disabled />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="计划批次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zcharg">
              <a-input
                v-model="model.zcharg"
                readonly
                placeholder="点击选择计划批次"
                @click="openBatchModal" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>

    <!-- 批次选择弹窗 -->
    <a-modal
      v-model:visible="batchModalVisible"
      title="选择计划批次"
      :width="modalWidth"
      @ok="handleBatchConfirm"
      @cancel="batchModalVisible = false">
      <a-table
        :rowSelection="rowSelection"
        :columns="batchColumns"
        :dataSource="batchList"
        rowKey="id"
        bordered />
    </a-modal>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage';

export default {
  name: 'ShipmentPickDetailModal',
  props: {
    mainId: {
      type: String,
      required: false,
      default: '',
    },
  },
  data() {
    return {
      title: '操作',
      visible: false,
      confirmLoading: false,
      batchModalVisible: false,
      model: {},

      labelCol: { xs: { span: 24 }, sm: { span: 5 } },
      wrapperCol: { xs: { span: 24 }, sm: { span: 16 } },

      validatorRules: {},
      batchList: [],
      selectedBatchIds: [], // 存储选中的批次ID
      batchColumns: [],

      rowSelection: {
        selectedRowKeys: [], // 与表格的行选择绑定
        onChange: this.handleRowSelect, // 行选择事件
      },
      url: {
        add: "/shipment/pick/header/addShipmentPickDetail",
        edit: "/shipment/pick/header/editShipmentPickDetail",
      }
    };
  },
  computed: {
    modalWidth() {
      const columnCount = this.batchColumns.length;
      const maxColumnWidth = 150;
      const minWidth = 800;
      return Math.max(minWidth, columnCount * maxColumnWidth);
    },
  },
  methods: {
    handleRowSelect(selectedRowKeys, selectedRows) {
      this.rowSelection.selectedRowKeys = selectedRowKeys; // 更新选中行的键
      this.selectedBatchIds = selectedRows.map(row => row.id); // 更新选中批次ID
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    close() {
      this.$emit('close');
      this.visible = false;
      this.$refs.form.clearValidate();
    },
    handleOk() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.confirmLoading = true;
          const httpurl = this.model.id ? this.url.edit : this.url.add;
          const method = this.model.id ? 'put' : 'post';
          this.model['shipmentPickId'] = this.mainId;

          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                this.$message.success(res.message);
                this.$emit('ok');
              } else {
                this.$message.warning(res.message);
              }
            })
            .finally(() => {
              this.confirmLoading = false;
              this.close();
            });
        }
      });
    },
    handleCancel() {
      this.close();
    },
    openBatchModal() {
      this.loadBatchList();
      this.batchModalVisible = true;
    },
    handleBatchConfirm() {
      const selectedBatches = this.batchList.filter(batch =>
        this.selectedBatchIds.includes(batch.id)
      );
      this.model.zcharg = selectedBatches.map(batch => batch.batch).join(',');
      this.batchModalVisible = false;
    },
    loadBatchList() {
      const params = this.model.matnr;

      if (!params) {
        this.$message.warning('物料编码不能为空');
        return;
      }

      const url = `/inventory/inventoryDetail/getInventoryListByMaterialCode?materialCode=${encodeURIComponent(params)}`;

      httpAction(url, {}, 'get')
        .then(res => {
          if (res.success) {
            this.batchList = res.result;

            this.batchColumns = [
              {title: '批次', dataIndex: 'batch', key: 'batch'},
              {title: '库房地点', dataIndex: 'lgort', key: 'lgort'},
              {title: '库位号', dataIndex: 'locationCode', key: 'locationCode'},
              {title: '物料编码', dataIndex: 'materialCode', key: 'materialCode'},
              {title: '物料名称', dataIndex: 'materialName', key: 'materialName'},
              {title: '物料规格', dataIndex: 'materialSpec', key: 'materialSpec'},
              {title: '物料单位', dataIndex: 'materialUnit', key: 'materialUnit'},
              {title: '数量', dataIndex: 'qty', key: 'qty'},
              {title: '明细id', dataIndex: 'id', key: 'id'},
            ];
          } else {
            this.$message.warning('加载批次数据失败:' + res.message);
          }
        })
        .catch(() => {
          this.$message.error('接口请求失败,请检查网络连接');
        });
    },
  },
};
</script>