<template>
  <el-form
    v-loading="loading"
    :model="ruleForm"
    :rules="rules"
    status-icon
    ref="ruleForm"
    label-width="85px"
    class="demo-ruleForm"
  >
    <el-row>
      <el-col :span="id === 'add' ? 24 : 12">
        <el-form-item label="可入库总数" prop="wsend_num">
          <el-input
            disabled
            placeholder="可入库总数"
            v-model="ruleForm.wsend_num"
            maxlength="100"
          />
        </el-form-item>
      </el-col>
      <el-col :span="id === 'add' ? 24 : 12">
        <el-form-item label="入库数量" prop="send_num">
          <el-input
            :disabled="id !== 'add'"
            placeholder="入库数量"
            v-model="ruleForm.send_num"
            maxlength="100"
          />
        </el-form-item>
      </el-col>
      <el-col :span="id === 'add' ? 24 : 12">
        <el-form-item label="物流公司" prop="post_company">
          <search-express
            :disabled="id !== 'add'"
            :value="ruleForm.post_company"
            :placeholder="'物流公司'"
            :names="''"
            :is-detail="false"
            @searchChange="handleCompany"
          />
        </el-form-item>
      </el-col>
      <el-col :span="id === 'add' ? 24 : 12">
        <el-form-item label="物流单号" prop="post_code">
          <el-input
            :disabled="id !== 'add'"
            placeholder="物流单号"
            v-model="ruleForm.post_code"
            maxlength="100"
          />
        </el-form-item>
      </el-col>
      <el-col :span="id === 'add' ? 24 : 12">
        <el-form-item label="物流费用" prop="post_fee">
          <el-input
            :disabled="id !== 'add'"
            placeholder="物流费用"
            v-model="ruleForm.post_fee"
            maxlength="100"
          >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="id === 'add' ? 24 : 12">
        <el-form-item label="发货时间" prop="post_time">
          <el-date-picker
            :disabled="id !== 'add'"
            v-model="ruleForm.post_time"
            type="datetime"
            style="width: 100%"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="发货时间"
            :picker-options="pickerOptions"
            @change="selectTime"
          >
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="24" style="text-align: right" v-if="id === 'add'">
        <el-button type="primary" :size="'mini'" @click="submitForm"
          >保 存
        </el-button>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import asyncRequest from "@/apis/service/purchaseIn/purchaseOrder";
import resToken from "@/mixins/resToken";
import { isnumber, isnumber2, isAlphanumeric } from "@/utils/validate";
export default {
  name: "wsmInOrderAdd",
  props: ["id", "sitem", "newTime", "modelSitem"],
  mixins: [resToken],
  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 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,
      ruleForm: {
        wsm_mobile: "",
        wsm_contactor: "",
        wsm_addr: "",
        cgdNo: "",
        wsm_code: "",
        post_company: [],
        post_code: "",
        post_fee: "",
        post_time: "",
        send_num: "",
        status: "1",
      },
      rules: {
        send_num: {
          required: true,
          validator: validateWeight,
          trigger: "blur",
        },
        post_company: {
          type: "array",
          required: true,
          trigger: "change",
          message: "请输入物流公司",
        },
        post_code: {
          required: true,
          validator: validateCode,
          trigger: "blur",
        },
        post_fee: {
          required: true,
          validator: validatePrice,
          trigger: "blur",
        },
        post_time: {
          required: true,
          message: "请选择发货时间",
          trigger: "change",
        },
      },
    };
  },
  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();
          if (this.id === "add") {
            const {
              post_company,
              post_code,
              post_fee,
              sendtime,
              cgdNo,
              wsm_code,
              send_num,
              wsend_num,
            } = this.sitem;
            this.ruleForm = {
              // wsm_mobile,
              // wsm_contactor,
              // wsm_addr,
              wsend_num: wsend_num || "",
              cgdNo: cgdNo || "",
              wsm_code: wsm_code || "",
              post_company: post_company ? [post_company] : [],
              post_code: post_code || "",
              post_fee: post_fee || "",
              post_time: sendtime || "",
              send_num: send_num || "",
              status: "1",
            };
          }
          console.log(this.sitem);

          // const {
          //   wsm_mobile,
          //   wsm_contactor,
          //   wsm_addr,
          //   wsend_num,
          //   cgdNo,
          //   wsm_code,
          // } = this.modelSitem;
          // this.ruleForm = {
          //   wsm_mobile,
          //   wsm_contactor,
          //   wsm_addr,
          //   wsend_num,
          //   cgdNo: cgdNo || "",
          //   wsm_code: wsm_code || "",
          //   post_company: [post_company] || [],
          //   post_code: post_code || "",
          //   post_fee: post_fee || "",
          //   post_time: sendtime || "",
          //   send_num: send_num || "",
          //   status: "1",
          // };
        }
      });
    },
    // 保存更改
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          model.post_company = model.post_company.toString();

          const res = await asyncRequest.orderinadd(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);
      this.ruleForm.post_company = e && e.code ? [e.label] : [];
      this.$refs.ruleForm.validateField("post_company");
    },
  },
};
</script>

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