<template>
  <div v-loading="loading">
    <el-form
      :model="ruleForm"
      :rules="rules"
      status-icon
      :size="'mini'"
      ref="ruleForm"
      label-width="110px"
      class="demo-ruleForm"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="发货总数">
            <el-input
              placeholder="发货总数"
              disabled
              v-model="ruleForm.send_num"
              maxlength="100"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="正常入库数量" prop="wsm_num">
            <el-input
              placeholder="正常入库数量"
              :disabled="status !== '2'"
              v-model="ruleForm.wsm_num"
              maxlength="100"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="异常补发数量" prop="reissue_num">
            <el-input
              placeholder="异常补发数量"
              :disabled="status !== '2'"
              v-model="ruleForm.reissue_num"
              maxlength="100"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="异常退货数量" prop="return_num">
            <el-input
              placeholder="异常退货数量"
              :disabled="status !== '2'"
              v-model="ruleForm.return_num"
              maxlength="100"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="收件联系人" prop="contactor">
            <el-input
              placeholder="收件联系人"
              :disabled="status !== '2'"
              v-model="ruleForm.contactor"
              maxlength="100"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="收件联系电话" prop="mobile">
            <el-input
              placeholder="收件联系电话"
              :disabled="status !== '2'"
              v-model="ruleForm.mobile"
              maxlength="100"
            >
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="18">
          <el-form-item label="收件地址" prop="addr">
            <el-input
              placeholder="收件地址"
              :disabled="status !== '2'"
              v-model="ruleForm.addr"
              maxlength="250"
            />
          </el-form-item>
          <el-form-item label="审核备注" prop="error_remark">
            <el-input
              placeholder="审核备注"
              :disabled="status !== '2'"
              v-model="ruleForm.error_remark"
              maxlength="250"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24" style="text-align: right; padding: 0 0 10px 0">
          <el-button
            :size="'mini'"
            v-if="status === '2' && ppowers.some((i) => i == '2')"
            type="primary"
            @click="submitForm"
            >保 存
          </el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/purchaseIn/purchaseOrder";
import resToken from "@/mixins/resToken";
import { isMobile, isLicense, isnumber, isPhone } from "@/utils/validate";
export default {
  name: "wsmInOrderAdd",
  props: ["id", "sitem", "newTime", "modelSitem"],
  mixins: [resToken],
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "wsmInOrderDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "RKD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    const validateWeight = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("不能为空!"));
      } else {
        if (!isnumber(value)) {
          callback(new Error("仅支持整数!"));
        } else {
          callback();
        }
      }
    };
    const validatemobile = (rule, value, callback) => {
      if (value !== "") {
        if (isPhone(value) || isMobile(value)) {
          callback();
        } else {
          callback(new Error("联系电话格式不正确！"));
        }
      } else {
        callback(new Error("请输入收货联系电话！"));
      }
    };
    return {
      loading: true,
      status: "",
      showModel: false,
      modelAllnum: "",
      modelitem: null,
      modelId: "",
      ruleForm: {
        wsm_in_code: "",
        send_num: "",
        wsm_num: "",
        reissue_num: "",
        return_num: "",
        contactor: "",
        addr: "",
        mobile: "",
        error_remark: "",
        remark: "",
      },
      rules: {
        wsm_num: {
          required: true,
          validator: validateWeight,
          trigger: "blur",
        },
        reissue_num: {
          required: true,
          validator: validateWeight,
          trigger: "blur",
        },
        return_num: {
          required: true,
          validator: validateWeight,
          trigger: "blur",
        },
        contactor: {
          required: true,
          message: "收件联系人不能为空！",
          trigger: "blur",
        },
        addr: {
          required: true,
          message: "收件地址不能为空！",
          trigger: "blur",
        },
        mobile: {
          required: true,
          validator: validatemobile,
          trigger: "blur",
        },
        error_remark: {
          required: true,
          message: "审核备注不能为空！",
          trigger: "blur",
        },
      },
    };
  },
  watch: {
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },
  mounted() {
    this.initForm();
  },
  methods: {
    async initForm() {
      this.loading = true;

      await this.resetForm();
      this.loading = false;
    },
    //初始化表单
    async resetForm() {
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { wsm_in_code, status, send_num, remark } = this.sitem;
          const {
            return_contactor,
            return_addr,
            return_mobile,
            wsm_num,
            reissue_num,
            return_num,
            error_remark,
          } = this.sitem.return;

          this.status = status;
          this.ruleForm = {
            wsm_in_code: wsm_in_code || "",
            send_num: send_num || "0",
            wsm_num: wsm_num || "0",
            reissue_num: reissue_num || "0",
            return_num: return_num || "0",
            contactor: return_contactor || "",
            addr: return_addr || "",
            mobile: return_mobile || "",
            error_remark: error_remark || "",
            remark: remark || "",
          };
        }
        if (this.$refs.tableForm) {
          this.$refs.tableForm.resetFields();
          this.$refs.tableForm.clearValidate();

          const { checkinfo } = this.sitem;
          this.tableForm = {
            error_list: checkinfo || [],
          };
        }
      });
    },
    addError(index) {
      this.modelAllnum = "2";
      this.modelitem = index === -1 ? {} : this.tableForm.error_list[index];
      this.modelId = index;
      this.showModel = true;
    },

    // 保存更改
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          let item = JSON.parse(JSON.stringify(this.ruleForm));
          let rnum = parseInt(item.return_num),
            wnum = parseInt(item.wsm_num),
            anum = parseInt(item.send_num),
            renum = parseInt(item.reissue_num);

          if (rnum + wnum !== anum) {
            this.$message.warning("正常入库数量 + 异常退货数量 不等于 发货总数！");
            this.loading = false;
            return;
          }
          if (renum > rnum) {
            this.$message.warning("异常补发数量不能大于异常退货数量！");
            this.loading = false;
            return;
          }

          delete item["send_num"];
          const res = await asyncRequest.orderinreturn(item);
          this.loading = false;
          if (res && res.code === 0) {
            this.$notify.success({
              title: "提交成功",
              message: "",
            });
            this.$emit("refresh"); //抛出事件给详情页。
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.stockInDetail {
  .el-table-column-error_img-ul {
    position: relative;
    box-sizing: border-box;
    li {
      float: left;
      border: 1px solid #dfe6ec;
      margin: 0px 2px 2px 0px;
      box-sizing: border-box;
      img {
        display: inline-block;
        box-sizing: border-box;
        width: 50px;
        vertical-align: top;
        height: 32px;
        &:hover {
          cursor: pointer;
        }
      }
    }
  }
}
</style>
