<template>
  <el-form
    v-loading="loading"
    :model="ruleForm"
    :rules="rules"
    status-icon
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-row>
      <el-col :span="12">
        <el-form-item label="发货总数" prop="send_num" required>
          <el-input
            disabled
            placeholder="物流单号"
            v-model="ruleForm.send_num"
            maxlength="100"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="物流公司" prop="post_name">
          <search-express
            :value="ruleForm.post_name"
            :placeholder="'物流公司'"
            :names="''"
            :order_source="order_source"
            :is-detail="false"
            @searchChange="handleCompany"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="物流单号" prop="post_code">
          <el-input placeholder="物流单号" v-model="ruleForm.post_code" maxlength="100" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="物流费用" prop="post_fee">
          <el-input placeholder="物流费用" v-model="ruleForm.post_fee" maxlength="100">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </el-col>

      <el-col
        :span="24"
        style="text-align: right"
        v-if="powers.some((i) => i == '045')"
      >
        <el-button type="primary" :size="'mini'" @click="submitForm">保 存 </el-button>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
// import asyncRequest from "@/apis/service/sellOut/zxoutOrder";
import resToken from "@/mixins/resToken";
import { isnumber, isnumber2, isAlphanumeric } from "@/utils/validate";
export default {
  name: "wsmInOrderAdd",
  props: ["id", "sitem", "newTime"],
  mixins: [resToken],
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "zxoutOrderDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    const validatePrice = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("不能为空！"));
      } else {
        if (isnumber(value)) {
          callback();
        } else if (isnumber2(value)) {
          callback();
        } else {
          callback(new Error("仅支持整数或两位小数!"));
        }
      }
    };
    const validateCode = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("不能为空!"));
      } else {
        if (!isAlphanumeric(value)) {
          callback(new Error("仅支持字母和数字!"));
        } else {
          callback();
        }
      }
    };

    return {
      order_source: "",
      loading: true,
      ruleForm: {
        outCode: "",
        send_num: "",
        post_name: [],
        post_code: "",
        post_fee: "",
        sendtime: "",
      },
      rules: {
        post_name: {
          type: "array",
          required: true,
          trigger: "change",
          message: "请输入物流公司",
        },
        post_code: {
          required: true,
          validator: validateCode,
          trigger: "blur",
        },
        post_fee: {
          required: true,
          validator: validatePrice,
          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 {
            outCode,
            send_num,
            post_code,
            post_name,
            post_fee,
            order_source,
          } = this.sitem;
          this.order_source = order_source;
          this.ruleForm = {
            outCode: outCode || "",
            send_num: send_num || "",
            post_name: post_name ? [post_name] : [],
            post_code: post_code || "",
            post_fee: post_fee || "",
            sendtime: "",
          };
        }
      });
    },
    // 保存更改
    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));
          // model.post_name = model.post_name.toString();
          // delete model["send_num"];
          // delete model["page"];
          // const res = await asyncRequest.salezxout(model);
          // 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;
        }
      });
    },
    handleCompany(e) {
      console.log(e);
      if (e && e.code) {
        this.ruleForm.post_name = [e.shortName];
        this.ruleForm.page = 1;
      }
      console.log(this.ruleForm.express_company);
      this.$refs.ruleForm.validateField("post_name");
    },
  },
};
</script>

<style lang="scss" scoped>
.account {
}
</style>
