<template>
  <el-form
    ref="ruleForm"
    :loading="loading"
    :model="ruleForm"
    status-icon
    :size="'mini'"
    :rules="rulesThis"
    label-width="100px"
    style="width: 100%; padding: 20px 0 0 0"
    class="demo-ruleForm"
  >
    <el-row>
      <el-col :span="6">
        <el-form-item label="物流公司" prop="express_company">
          <search-express
            :value="ruleForm.express_company"
            :placeholder="'物流公司'"
            :names="''"
            :order_source="order_source"
            :size="'mini'"
            :disabled="false"
            :is-detail="false"
            @searchChange="handleCompany"
          />
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="物流单号" prop="express_numbres">
          <el-input
            placeholder="物流单号"
            v-model="ruleForm.express_numbres"
            maxlength="100"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6" v-if="isShow">
        <el-form-item label="物流费用" prop="express_fee">
          <digital-input
            :values="ruleForm.express_fee"
            :placeholder="'物流费用'"
            :min="0"
            :disabled="false"
            :max="100000000000"
            :position="'right'"
            :precision="2"
            :size="'mini'"
            :controls="false"
            :append="'元'"
            @reschange="number_change($event, 'lead_time')"
          />
        </el-form-item>
      </el-col>

      <el-col :span="isShow ? 6 : 12" style="text-align: right">
        <el-button type="primary" :size="'mini'" @click="submitForm" v-if="true"
          >保 存</el-button
        >
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/sellOutOrder";
import resToken from "@/mixins/resToken";
import { isAlphanumeric, isnumber } from "@/utils/validate.js";
export default {
  name: "purchase_check",
  props: ["id", "showModelThis", "newTime", "sitem"],
  mixins: [resToken],
  data() {
    const validateexpress = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("物流单号不能为空！"));
      } else {
        if (!isAlphanumeric(value)) {
          callback(new Error("物流单号格式不正确！"));
        } else {
          callback();
        }
      }
    };
    const validate_num = (rule, value, callback) => {
      const { required } = rule;
      if (required && value === "") {
        callback(new Error("不能为空！"));
      } else {
        callback();
      }
    };
    return {
      options: [
        {
          value: "1",
          label: "个人承担",
        },
        {
          value: "2",
          label: "公司承担",
        },
        {
          value: "3",
          label: "供应商承担",
        },
      ],
      returnCode: "",
      order_source: "",
      code: "",
      isShow: false,
      loading: false,
      status: "", //存储详情接口返的状态
      showModelThis: this.showModel,
      resign_name: "", //离职人
      hand_name: "", //交接人
      ruleForm: {
        post_company: "", //入参快递公司
        express_company: [], //物流公司
        express_numbres: "", //物流单号
        express_fee: 0, //物流费用
        is_th: "",
      },
      rulesThis: this.rules,
      // 验证规则
      rules: {
        express_company: [
          {
            type: "array",
            required: true,
            trigger: "change",
            message: "请选择物流公司",
          },
        ],
        express_numbres: [
          {
            required: true,
            trigger: "blur",
            validator: validateexpress,
          },
        ],
        express_fee: [
          {
            required: true,
            trigger: "blur",
            validator: validate_num,
          },
        ],
      },
    };
  },
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find((i) => i.menu_route == "purchase_check") ||
        {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },

  mounted() {
    this.initForm();
  },
  watch: {
    id: function (val) {
      if (val) {
        this.initForm();
      }
    },
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },
  methods: {
    //   关闭弹窗,直接隐藏表单
    // async closeAddEdit() {
    //   this.$emit("closeModel", false); //抛出一个事件，关闭弹窗
    //
    // },
    closeAdd() {
      this.showModelThis = false;
    },
    async initForm() {
      this.loading = true;
      if (this.id === "add") {
        this.status = "";
        this.rulesThis = this.rules;
        await this.resetForm();
      } else {
        this.rulesThis = this.rules;
        await this.resetForm();
        // await this.initData();
      }
      this.loading = false;
    },

    async resetForm() {
      this.resign_name = "";
      this.hand_name = "";
      this.status = "";
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const {
            returnCode,
            is_th,
            company,
            post_code,
            post_fee,
            order_source,
          } = this.sitem;
          this.order_source = order_source;
          this.is_th = is_th;
          this.returnCode = returnCode;
          this.ruleForm = {
            post_company: "", //入参快递公司
            express_company: company ? [company] : [], //物流公司
            express_numbres: post_code || "", //物流单号
            express_fee: post_fee || "0", //物流费用
            is_th: is_th || "",
          };
          this.isShow = is_th !== "1";
          this.rulesThis.express_fee[0].required = is_th !== "1";
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          let _model = {
            returnCode: this.returnCode, //退货单号
            company: model.post_company, //快递公司
            post_code: model.express_numbres, //快递编号
            post_fee: model.express_fee || 0, //快递费用
            is_th: this.is_th,
          };
          const res = await asyncRequest.setdelivery(_model);
          this.loading = false;
          if (res && res.code === 0) {
            // const title = this.id === "add" ? "添加成功！" : "修改成功！";
            this.$notify.success({
              title: res.message,
              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;
        }
      });
    },
    handleCompany(e) {
      if (e && e.code) {
        this.ruleForm.post_company = e.shortName;
        this.ruleForm.express_company = [e.code];
        this.ruleForm.page = 1;
      }
    },
    async number_change(e, key) {
      this.ruleForm[key] = e + "" || "0";
      this.$refs.ruleForm.validateField(key);
    },
    handleChange(e) {
      if (e && e == "2") {
        this.isShow = true;
      } else {
        this.isShow = false;
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
