<template>
  <el-form
    v-loading="loading"
    :model="ruleForm"
    :rules="rules"
    status-icon
    :size="'mini'"
    ref="ruleForm"
    label-width="90px"
    class="demo-ruleForm"
  >
    <el-row>
      <el-col :span="6">
        <el-form-item label="退货总数" prop="wsm_num">
          <el-input
            placeholder="发货总数"
            disabled
            v-model="ruleForm.return_num"
            maxlength="100"
          >
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="物流公司" prop="post_company">
          <search-express
            :disabled="!(status === '5' && ppowers.some((i) => i == '5'))"
            :value="ruleForm.post_company"
            :placeholder="'物流公司'"
            :order_source="'0'"
            :names="''"
            :size="'mini'"
            :is-detail="false"
            @searchChange="handleCompany"
          />
        </el-form-item>
      </el-col>
      <el-col :span="10">
        <el-form-item label="物流单号" prop="post_code">
          <el-input
            placeholder="物流单号"
            :disabled="!(status === '5' && ppowers.some((i) => i == '5'))"
            v-model="ruleForm.post_code"
            maxlength="100"
          >
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="2" style="text-align: right">
        <el-button
          type="primary"
          v-if="status === '5' && ppowers.some((i) => i == '5')"
          :size="'mini'"
          @click="submitForm"
          >保 存
        </el-button>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import asyncRequest from "@/apis/service/purchaseIn/wsmInOrder";
import resToken from "@/mixins/resToken";
import { isnumber, isAlphanumeric } 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 validateCode = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("不能为空!"));
      } else {
        if (!isAlphanumeric(value)) {
          callback(new Error("仅支持字母和数字!"));
        } else {
          callback();
        }
      }
    };
    return {
      loading: true,
      status: "",
      showModel: false,
      modelAllnum: "",
      modelitem: null,
      modelId: "",
      ruleForm: {
        return_num: "",
        post_company: [],
        post_code: "",
      },
      rules: {
        return_num: {
          required: true,
          validator: validateWeight,
          trigger: "blur",
        },
        post_company: {
          required: true,
          message: "请选择物流公司",
          trigger: "change",
        },
        post_code: {
          required: true,
          validator: validateCode,
          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;
    },
    errorModel(e) {
      if (e) {
        let index = e.index;
        if (index === -1) {
          this.tableForm.error_list.push(e);
        } else {
          this.tableForm.error_list[index] = e;
        }
      }
    },
    //初始化表单
    async resetForm() {
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { wsm_in_code, status } = this.sitem;
          const { return_num, post_code, post_company } = this.sitem.return;
          this.status = status;
          this.ruleForm = {
            wsm_in_code: wsm_in_code || "",
            return_num: return_num || "0",
            post_company: post_company ? [post_company] : [],
            post_code: post_code || "",
          };
        }
      });
    },
    handleCompany(e) {
      const { code, label } = e;
      this.ruleForm.post_company = code ? [label] : [];
      this.$refs.ruleForm.validateField("post_company");
    },
    // 保存更改
    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));
          item.post_company = item.post_company.toString();
          delete item["return_num"];
          const res = await asyncRequest.returnsend(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>
