<template>
  <el-form
    v-loading="loading"
    :model="ruleForm"
    :rules="rules"
    :size="'mini'"
    status-icon
    ref="ruleForm"
    label-width="90px"
    class="demo-ruleForm"
  >
    <el-row>
      <el-col :span="8">
        <el-form-item label="发货总数" prop="send_num" required>
          <el-input
            disabled
            placeholder="发货总数"
            v-model="ruleForm.send_num"
            maxlength="100"
          />
        </el-form-item>
        <el-form-item label="物流公司" prop="post_name">
          <search-express
            :value="ruleForm.post_name"
            :placeholder="'物流公司'"
            :names="''"
            :size="'mini'"
            :is-detail="false"
            @searchChange="handleCompany"
          />
        </el-form-item>
      </el-col>

      <el-col :span="8">
        <el-form-item label="物流费用" prop="post_fee">
          <digital-input
            :values="ruleForm.post_fee"
            :placeholder="'物流费用'"
            :min="0"
            :disabled="type === 'view' || type === 'post_fee'"
            :max="100000000000"
            :position="'right'"
            :precision="2"
            :size="'mini'"
            :controls="false"
            :append="'元'"
            @reschange="number_change($event, 'post_fee')"
          />
        </el-form-item>
        <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="16">
          <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            placeholder="备注"
            v-model="ruleForm.remark"
            maxlength="100"
          />
        </el-form-item> 
      </el-col>

      <el-col :span="8" style="text-align: right">
        <el-button type="primary" :size="'mini'" @click="submitForm"
          >保 存
        </el-button>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/sellOutOrder";
import resToken from "@/mixins/resToken";
import { isnumber, isnumber2, isAlphanumeric } from "@/utils/validate";
export default {
  name: "wsmInOrderAdd",
  props: ["id", "sitem", "newTime"],
  mixins: [resToken],
  data() {
    const validate_num = (rule, value, callback) => {
      const { required } = rule;
      if (required && 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,
      ruleForm: {
        outCode: "",
        send_num: "",
        post_name: [],
        post_code: "",
        post_fee: "",
        remark:"",
        // sendtime: "",
      },
      rules: {
        post_name: {
          type: "array",
          required: true,
          trigger: "change",
          message: "请输入物流公司",
        },
        post_code: {
          required: true,
          validator: validateCode,
          trigger: "blur",
        },
        post_fee: {
          required: true,
          validator: validate_num,
          trigger: "blur",
        },
        remark:{
          required: true,
          validator: validate_num,
          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 ,remark} =
            this.sitem;
          this.ruleForm = {
            outCode: outCode || "",
            send_num: send_num || "",
            post_name: post_name ? [post_name] : [],
            post_code: post_code || "",
            post_fee: post_fee || "",
            remark:remark || "",
            // sendtime: "",
          };
        }
      });
    },
    // 保存更改
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          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.saleoutsend(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) {
      const { code, id, label } = e;
      this.ruleForm.post_name = label ? [label] : [];
      this.$refs.ruleForm.validateField("post_name");
    },
    number_change(e, key) {
      this.ruleForm[key] = e + "" || "0";
      this.$refs.ruleForm.validateField(key);
    },
  },
};
</script>

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