MachineTransferModal.vue 7.04 KB
<template>
  <j-modal
    :title="title"
    :width="width"
    :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="fromLocationCode">
              <a-input placeholder="请输入起始点位" :disabled="true" v-model="model.fromLocationCode"></a-input>
            </a-form-model-item>
          </a-col>
          <!-- 目标点位:库区所有点位过滤起始点位 -->
          <a-col :span="24">
            <a-form-model-item label="目标点位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="toLocationCode">
              <j-popup v-if="this.querySource.zoneCode==='A1'" v-model="model.toLocationCode" code="getA1MachineLocationList" field="code" orgFields="code" destFields="code" :multi="false"/>
<!--              <a-select-->
<!--                v-model="model.toLocationCode"-->
<!--                placeholder="请选择目标点位"-->
<!--                style="width: 100%"-->
<!--                :disabled="!model.fromLocationCode || !querySource.zoneCode"-->
<!--              >-->
<!--                <a-select-option-->
<!--                  v-for="item in filterToPortList"-->
<!--                  :key="`to_port_${item.code}`"-->
<!--                  :value="item.code"-->
<!--                >-->
<!--                  {{ item.code }}-->
<!--                </a-select-option>-->
<!--              </a-select>-->
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import {getAction, httpAction} from '@/api/manage'
import {validateDuplicateValue} from '@/utils/util'
import {createAgv, createTransferTask} from '@/api/api'

export default {
  name: "MachineTransferModal",
  components: {},
  data() {
    return {
      title: "操作",
      width: 500,
      allPortList: [], // 库区下所有点位列表
      machineList: [], // 冗余字段,可后续删除
      querySource: {}, // 父组件传入的库区编码等信息
      visible: false,
      model: {
        fromLocationCode: '', // 起始点位(只读)
        toLocationCode: ''    // 目标点位
      },
      labelCol: {
        xs: {span: 24},
        sm: {span: 6},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      confirmLoading: false,
      validatorRules: {
        fromLocationCode: [
          {required: true, message: '请确认起始点位!', trigger: 'change'},
        ],
        toLocationCode: [
          {required: true, message: '请选择目标点位!', trigger: 'change'},
          {validator: this.checkToLocation, trigger: 'change'}
        ],
      },
      url: {
        add: "/task/taskHeader/createTransferTask",
      },
      modelDefault: {} // 备份原始值
    }
  },
  created() {
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  computed: {
    // 过滤目标点位(排除起始点位)
    filterToPortList() {
      if (!this.allPortList.length || !this.model.fromLocationCode) {
        return this.allPortList;
      }
      return this.allPortList.filter(item => item.code !== this.model.fromLocationCode);
    }
  },
  watch: {
    // 监听弹窗显示,打开时加载库区点位
    visible(newVal) {
      if (newVal && this.querySource.zoneCode) {
        this.getAllPortListByZone();
      } else if (!newVal) {
        this.allPortList = []; // 关闭弹窗清空点位
      }
    },
    'model.fromLocationCode'() {}
  },
  methods: {
    /**
     * 根据库区编码获取所有点位列表
     */
    async getAllPortListByZone() {
      // 新增空值校验,避免无效请求
      if (!this.querySource.zoneCode) {
        this.$message.warning('未获取到库区编码,无法查询点位!');
        return;
      }
      let params = {"zoneCode": this.querySource.zoneCode};
      console.log('当前库区编码:', this.querySource.zoneCode);
      try {
        const res = await getAction("/config/location/list", params);
        if (res.success) {
          this.allPortList = res.result.records;
        }
        if (res.code === 510) {
          this.$message.warning(res.message);
        }
      } catch (error) {
        this.$message.error('获取库区点位列表失败');
        console.error(error);
      }
    },

    /**
     * 自定义校验:目标点位不能与起始点位相同
     */
    checkToLocation(rule, value, callback) {
      if (value && value === this.model.fromLocationCode) {
        callback(new Error('目标点位不能与起始点位相同!'));
      } else {
        callback();
      }
    },

    /**
     * 新增操作
     */
    add() {
      this.model = JSON.parse(JSON.stringify(this.modelDefault));
      this.visible = true;
      this.allPortList = [];
    },

    /**
     * 编辑操作:接收父组件传入的record,赋值起始点位+库区编码
     * @param {Object} record 父组件传入的{zoneCode, fromLocationCode}
     */
    edit(record) {
      this.model = JSON.parse(JSON.stringify(this.modelDefault));
      this.model.fromLocationCode = record.fromLocationCode;
      // 核心:接收父组件传入的zoneCode,赋值给querySource(Vue2对象赋值直接生效)
      this.querySource.zoneCode = record.zoneCode;
      this.visible = true;
      this.allPortList = [];
    },

    /**
     * 关闭弹窗
     */
    close() {
      this.$emit('close');
      this.visible = false;
      if (this.$refs.form) {
        this.$refs.form.clearValidate();
      }
      this.allPortList = [];
    },

    /**
     * 确认提交
     */
    async handleOk() {
      try {
        const valid = await this.$refs.form.validate();
        if (!valid) return;
        this.confirmLoading = true;
        const taskParams = {
          fromPort: this.model.fromLocationCode,
          toPort: this.model.toLocationCode,
          taskType: null,
          priority: 10,
          status: 0,
          zoneCode: this.querySource.zoneCode,
          preTaskNo: 0,
          containerCode: '',
          carno: '',
          backWarehouse: 0,
          productionDetailId: 0,
          checkContainer: 0
        };
        const res = await createAgv(taskParams);
        if (res.success) {
          this.$message.success(res.message);
          this.$emit('ok');
          this.model = JSON.parse(JSON.stringify(this.modelDefault));
        } else {
          this.$message.warning(res.message);
        }
      } catch (error) {
        this.$message.error('提交失败,请重试');
        console.error(error);
      } finally {
        this.confirmLoading = false;
        this.close();
      }
    },

    /**
     * 取消操作
     */
    handleCancel() {
      this.close();
    }
  }
}
</script>

<style scoped>
.ant-select {
  margin-bottom: 0;
}
</style>