<template>
  <el-form
    :model="ruleForm"
    :rules="rules"
    ref="ruleForm"
    :size="'mini'"
    label-width="90px"
  >
    <el-row>
      <el-col :span="12"
        ><el-form-item label="物流公司" prop="post_name">
          <search-express
            :value="ruleForm.post_name"
            :placeholder="'请输入物流公司'"
            :names="''"
            :size="'mini'"
            :disabled="!(status == '2' && powers.some((item) => item == '021'))"
            :is-detail="
              !(status == '2' && powers.some((item) => item == '021'))
            "
            @searchChange="handleCompany"
          />
        </el-form-item>
        <el-form-item label="物流费用" prop="post_fee">
          <digital-input
            :values="ruleForm.post_fee"
            :placeholder="'物流费用'"
            :min="0"
            :disabled="!(status == '2' && powers.some((item) => item == '021'))"
            :max="100000000000"
            :position="'right'"
            :precision="2"
            :size="'mini'"
            :controls="false"
            :append="'元'"
            @reschange="number_change($event, 'post_fee')"
          />
        </el-form-item>
      </el-col>

      <el-col :span="12">
        <el-form-item label="物流单号" prop="post_code">
          <el-input
            placeholder="请输入物流单号"
            maxlength="100"
            :size="'mini'"
            :disabled="!(status == '2' && powers.some((item) => item == '021'))"
            v-model="ruleForm.post_code"
            clearable
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="status == '2' && powers.some((item) => item == '021')"
            type="primary"
            class="fr"
            :size="'medium'"
            @click="submitForm"
            >保 存
          </el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
   <script>
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/stock/allot/detail";
import { isAlphanumeric, isnumber2 } from "@/utils/validate";
export default {
  name: "allotFlow",
  mixins: [resToken],
  props: ["id", "sitem"],
  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "allotDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    const validateExpressSn = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("物流单号不能为空！"));
      } else {
        if (!isAlphanumeric(value)) {
          callback(new Error("请输入正确的物流单号"));
        } else {
          callback();
        }
      }
    };
    const validate_post_fee = (rule, value, callback) => {
      const { required } = rule;
      if (required && value === "") {
        callback(new Error("不能为空！"));
      } else {
        callback();
      }
    };

    return {
      status: "",
      ruleForm: {
        post_name: [], //发货物流公司
        post_code: "", //物流单号
        post_fee: "",
      },
      rules: {
        post_name: [
          {
            type: "array",
            required: true,
            message: "请选择发货公司",
            trigger: "change",
          },
        ],
        post_fee: [
          {
            required: true,
            validator: validate_post_fee,
            trigger: "blur",
          },
        ],
        post_code: [
          {
            required: true,
            trigger: "blur",
            validator: validateExpressSn,
          },
        ],
      },
    };
  },
  mounted() {
    this.resetForm();
  },

  methods: {
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { post_code, post_fee, post_name, status } = this.sitem;
          this.status = status;

          this.ruleForm = {
            post_code,
            post_fee: post_fee + "",
            post_name: post_name ? [post_name] : [],
          };
        }
      });
    },
    async number_change(e, key) {
      this.ruleForm[key] = e + "" || "0";
      this.$refs.ruleForm.validateField(key);
    },
    // 商品保存提交
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const model = JSON.parse(JSON.stringify(this.ruleForm));
          model.allot_code = this.sitem.allot_code;
          model.post_name = model.post_name.toString();
          let res = await asyncRequest.allotgetont(model);
          this.loading = false;
          if (res && res.code === 0) {
            //
            this.showModelThis = false;
            // 刷新
            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_name = e && e.code ? [e.label] : [];
      this.$refs.ruleForm.validateField("post_name");
    },
  },
};
</script>
