<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      v-dialogDrag
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="45%"
      append-to-body
    >
      <el-form
        ref="formRef"
        v-loading="formLoading"
        :model="formData"
        :rules="formRules"
        label-width="100px"
      >
        <el-form-item label="行号" prop="lineNo">
          <el-input v-model="formData.lineNo" placeholder="请输入行号" />
        </el-form-item>
        <el-form-item label="物料编码" prop="materialNo">
          <el-input
            v-model="formData.materialNo"
            placeholder="请输入物料编码"
          />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input
            v-model="formData.materialName"
            placeholder="请输入物料名称"
          />
        </el-form-item>
        <el-form-item label="物料规格" prop="materialSpec">
          <el-input
            v-model="formData.materialSpec"
            placeholder="请输入物料规格"
          />
        </el-form-item>
        <el-form-item label="批号" prop="batchNumber">
          <el-input v-model="formData.batchNumber" placeholder="请输入批号" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="formData.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="计划数量" prop="planQty">
          <el-input v-model="formData.planQty" placeholder="请输入计划数量" />
        </el-form-item>
        <el-form-item label="实际数量" prop="actualQty">
          <el-input v-model="formData.actualQty" placeholder="请输入实际数量" />
        </el-form-item>
        <el-form-item label="已完成数量" prop="completedQty">
          <el-input
            v-model="formData.completedQty"
            placeholder="请输入已完成数量"
          />
        </el-form-item>
        <el-form-item label="质量状态" prop="qualityStatus">
          <el-radio-group v-model="formData.qualityStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生产日期" prop="productionDate">
          <el-date-picker
            v-model="formData.productionDate"
            clearable
            type="date"
            value-format="timestamp"
            placeholder="选择生产日期"
          />
        </el-form-item>
        <el-form-item label="失效日期" prop="expirationDate">
          <el-date-picker
            v-model="formData.expirationDate"
            clearable
            type="date"
            value-format="timestamp"
            placeholder="选择失效日期"
          />
        </el-form-item>
        <el-form-item label="库存状态" prop="inventoryStatus">
          <el-radio-group v-model="formData.inventoryStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="源明细行号" prop="sourceLineNo">
          <el-input
            v-model="formData.sourceLineNo"
            placeholder="请输入源明细行号"
          />
        </el-form-item>
        <el-form-item label="源批次号" prop="sourceBatchNumber">
          <el-input
            v-model="formData.sourceBatchNumber"
            placeholder="请输入源批次号"
          />
        </el-form-item>
        <el-form-item label="行备注" prop="remark">
          <el-input v-model="formData.remark" placeholder="请输入行备注" />
        </el-form-item>
        <el-form-item label="行状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="formLoading" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as InoutRequestDetailApi from "@/api/wms/output/inoutrequestdetail";
export default {
  name: "InoutRequestDetailForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        requestId: undefined,
        lineNo: undefined,
        materialNo: undefined,
        materialName: undefined,
        materialSpec: undefined,
        batchNumber: undefined,
        unit: undefined,
        planQty: undefined,
        actualQty: undefined,
        completedQty: undefined,
        qualityStatus: undefined,
        productionDate: undefined,
        expirationDate: undefined,
        inventoryStatus: undefined,
        sourceLineNo: undefined,
        sourceBatchNumber: undefined,
        sourceReference: undefined,
        remark: undefined,
        status: undefined,
        stockCode: undefined,
        areaCode: undefined,
        locationCode: undefined,
        sourceRequestId: undefined,
        sourceDetailId: undefined,
      },
      // 表单校验
      formRules: {
        lineNo: [{ required: true, message: "行号不能为空", trigger: "blur" }],
        materialNo: [
          { required: true, message: "物料编码不能为空", trigger: "blur" },
        ],
        planQty: [
          { required: true, message: "计划数量不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "行状态不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await InoutRequestDetailApi.getInoutRequestDetail(id);
          this.formData = res.data;
          this.title = "修改出入库申请明细";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增出入库申请明细";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await InoutRequestDetailApi.updateInoutRequestDetail(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit("success");
          return;
        }
        // 添加的提交
        await InoutRequestDetailApi.createInoutRequestDetail(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit("success");
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        requestId: undefined,
        lineNo: undefined,
        materialNo: undefined,
        materialName: undefined,
        materialSpec: undefined,
        batchNumber: undefined,
        unit: undefined,
        planQty: undefined,
        actualQty: undefined,
        completedQty: undefined,
        qualityStatus: undefined,
        productionDate: undefined,
        expirationDate: undefined,
        inventoryStatus: undefined,
        sourceLineNo: undefined,
        sourceBatchNumber: undefined,
        sourceReference: undefined,
        remark: undefined,
        status: undefined,
        stockCode: undefined,
        areaCode: undefined,
        locationCode: undefined,
        sourceRequestId: undefined,
        sourceDetailId: undefined,
      };
      this.resetForm("formRef");
    },
  },
};
</script>
