<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="100px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="任务编号" prop="taskCode">
            <el-input v-model="form.taskCode" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="仓库" prop="warehouseName">
            <el-input v-model="form.warehouseName" disabled/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="计划编号" prop="planCode">
            <el-input v-model="form.planCode"  disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="明细编号" prop="planDetailCode">
            <el-input v-model="form.planDetailCode" disabled/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="成品名称" prop="productName">
            <el-input v-model="form.productName"  disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="物料名称" prop="materialName">
            <el-input v-model="form.materialName" disabled/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="物料条码" prop="materialBarcode">
            <el-input v-model="form.materialBarcode" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="批次码" prop="batchCode">
            <el-input v-model="form.batchCode"  disabled/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="计划退库数量" prop="planAmount">
            <el-input v-model="form.planAmount" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2" v-if="detailFlag">
          <el-form-item label="已退库数量" prop="returnAmount">
            <el-input v-model="form.returnAmount" disabled/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="申请人" prop="applyBy">
            <el-input v-model="form.applyBy"  disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="申请时间" prop="applyDate">
            <el-input v-model="form.applyDate"  disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="16" :offset="2">
          <el-form-item label="申请原因" prop="applyReason">
            <el-input v-model="form.applyReason" type="textarea" disabled/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="16" :offset="2">
          <el-form-item label="退库类型" prop="taskType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_raw_return_task_type" :value="form.taskType"/>
            </template>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-if="detailFlag">
        <el-col :span="8" :offset="2">
          <el-form-item label="审核人" prop="auditBy">
            <el-input v-model="form.auditBy" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="审核时间" prop="auditDate">
            <el-input v-model="form.auditDate" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="16" :offset="2">
          <el-form-item label="审核原因" prop="auditReason">
            <el-input v-model="form.auditReason" type="textarea" :disabled="detailFlag"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-if="detailFlag">
        <el-col :span="16" :offset="2">
          <el-form-item label="同步ERP数量" prop="erpAmount">
            <el-input v-model="form.erpAmount" disabled/>
          </el-form-item>
        </el-col>
      </el-row>


      <el-row v-if="detailFlag">
        <el-col :span="8" :offset="2">
          <el-form-item label="审核状态" prop="auditStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_audit_status" :value="form.auditStatus"/>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="执行状态" prop="executeStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_execute_status" :value="form.executeStatus"/>
            </template>
          </el-form-item>
        </el-col>
      </el-row>


    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="auditPass" v-if="!detailFlag">审核通过</el-button>
      <el-button type="primary" @click="auditFail" v-if="!detailFlag">审核不通过</el-button>
      <el-button @click="cancel">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import {auditRawReturn, getRawreturn4Audit} from "@/api/wms/rawreturn";

export default {
  name: "Rawreturn",
  dicts: ["wms_audit_status","wms_erp_status","wms_execute_status","wms_raw_return_task_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWmsRawReturnDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 原材料退库记录表格数据
      rawreturnList: [],
      // 原材料退库记录明细表格数据
      wmsRawReturnDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验

      detailFlag: true,
      rawReturnId: null,
    };
  },
  created() {
    const rawReturnId = this.$route.params && this.$route.params.rawReturnId;
    const taskCode = this.$route.params && this.$route.params.taskCode;
    const detailFlag = this.$route.params && this.$route.params.detailFlag;
    const warehouseName = this.$route.params && this.$route.params.warehouseName;
    this.detailFlag = detailFlag == "1";
    this.form.taskCode = taskCode;
    this.warehouseName = warehouseName;
    this.rawReturnId = rawReturnId;
    this.getRawReturn();
  },
  methods: {
    /** 查询原材料退库记录列表 */
    getRawReturn() {
      this.loading = true;
      getRawreturn4Audit(this.rawReturnId).then(response => {
        this.form = response.data;
        this.form.warehouseName = this.warehouseName;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      const obj = {path: "/wms/rawmanagement/rawreturn", query: {t: Date.now(), pageNum: this.$route.query.pageNum}};
      this.$tab.closeOpenPage(obj);
    },

    // 表单重置
    reset() {
      this.form = {
        rawReturnId: null,
        taskCode: null,
        warehouseId: null,
        locationCode: null,
        planCode: null,
        planDetailCode: null,
        productId: null,
        materialId: null,
        materialBarcode: null,
        batchCode: null,
        planAmount: null,
        returnAmount: null,
        operationType: null,
        taskType: null,
        endStationCode: null,
        applyReason: null,
        auditReason: null,
        auditStatus: null,
        executeStatus: null,
        applyBy: null,
        applyDate: null,
        auditBy: null,
        auditDate: null,
        updateBy: null,
        updateDate: null,
        beginTime: null,
        endTime: null,
        erpStatus: null,
        erpAmount: null
      };
      this.wmsRawReturnDetailList = [];
      this.resetForm("form");
    },
    auditPass() {
      this.form.auditStatus = "1";
      this.submitForm();
    },

    auditFail() {
      this.form.auditStatus = "2";
      if (!this.form.auditReason || this.form.auditReason == "" || this.form.auditReason == undefined) {
        this.$modal.msgWarning("请输入审核原因!");
        return;
      }
      this.submitForm();
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          auditRawReturn(this.form).then(response => {
            this.$modal.msgSuccess("保存成功");
            this.cancel();
          });
        }
      });
    },
  }
};
</script>
