<template>
  <el-form
    ref="ruleForm"
    :loading="loading"
    :model="ruleForm"
    status-icon
    :rules="rulesThis"
    label-width="100px"
    style="width: 100%"
    class="demo-ruleForm"
  >
    <el-row>
      <el-col :span="12">
        <el-form-item label="物流承担方" prop="own">
          <el-select
            v-model="ruleForm.own"
            placeholder="物流承担方"
            :disabled="!(status == '3' && powers.some((item) => item == '045'))"
            style="width: 100%"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="物流公司" prop="company">
          <search-express
            :value="ruleForm.company"
            :placeholder="'物流公司'"
            :names="post_company"
            :disabled="!(status == '3' && powers.some((item) => item == '045'))"
            :is-detail="
              !(status == '3' && powers.some((item) => item == '045'))
            "
            @searchChange="handleCompany"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="物流单号" prop="post_code">
          <el-input
            placeholder="物流单号"
             :disabled="
              !(status == '3' && powers.some((item) => item == '045'))
            "
            v-model="ruleForm.post_code"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12" v-if="ruleForm.own === '2' || ruleForm.own === '3'">
        <el-form-item label="物流费用" prop="post_fee">
          <el-input
            :disabled="!(status == '3' && powers.some((item) => item == '045'))"
            placeholder="物流费用"
            v-model="ruleForm.post_fee"
          >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </el-col>
      <el-col
        :span="24"
        style="text-align: right; padding: 0 0 20px 0"
        v-if="status == '3' && powers.some((item) => item == '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/zixunOrder";
import resToken from "@/mixins/resToken";
import { isAlphanumeric, isnumber } from "@/utils/validate";
export default {
  name: "zixunOrderDetail",
  props: ["newTime", "sitem"],
  mixins: [resToken],
  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "zixunOrderDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },

  watch: {
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },
  data() {
    const validateexpress = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("物流单号不能为空！"));
      } else {
        if (!isAlphanumeric(value)) {
          callback(new Error("物流单号格式不正确！"));
        } else {
          callback();
        }
      }
    };
    const validateexpressFee = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("物流费用不能为空！"));
      } else {
        if (isnumber(value)) {
          callback();
        } else if (isnumber2(value)) {
          callback();
        } else {
          callback(new Error("物流费用只能为数字！"));
        }
      }
    };
    return {
      options: [],
      post_company:"",
      smodel: {
        value: "3",
        label: "供应商承担",
      },
      code: "",
      isShow: false,
      loading: false,
      status: "", //存储详情接口返的状态
      showModelThis: this.showModel,
      resign_name: "", //离职人
      hand_name: "", //交接人
      ruleForm: {
        company: "", //入参快递公司
        own: "1", //物流费用承担方
        company: [], //物流公司
        post_code: "", //物流单号
        post_fee: 0, //物流费用
      },
      rulesThis: this.rules,
      // 验证规则
      rules: {
        own: [
          {
            required: true,
            trigger: "change",
            message: "请选择物流费用承担方",
          },
        ],
        company: [
          {
            type: "array",
            required: true,
            trigger: "change",
            message: "请选择退回仓库",
          },
        ],
        post_code: [
          {
            required: true,
            trigger: "blur",
            validator: validateexpress,
          },
        ],
        post_fee: [
          {
            required: true,
            trigger: "blur",
            validator: validateexpressFee,
          },
        ],
      },
    };
  },

  mounted() {
    this.initForm();
  },
  methods: {
    closeAdd() {
      this.showModelThis = false;
    },
    async initForm() {
      this.loading = true;
      let list = [
        {
          value: "1",
          label: "个人承担",
        },
        {
          value: "2",
          label: "公司承担",
        },
      ];
      this.options = [];
      this.options.push(...list);
      this.rulesThis = this.rules;
      await this.resetForm();
      this.loading = false;
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const {
            post_company,
            post_code,
            post_fee,
            returnCode,
            own,
            is_th,
            status,
          } = this.sitem;
          this.status = status;
          this.code = this.returnCode;
          this.post_company=post_company
          this.ruleForm = {
            returnCode: returnCode || "",
            own: own || "1", //入参快递公司
            company: post_company ? [post_company] : [], //物流公司
            post_code: post_code || "", //物流单号
            post_fee: post_fee || "0", //物流费用
          };
          if (is_th === "1") {
            this.options.push(this.smodel);
          }
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          model.company = model.company.toString();
          const res = await asyncRequest.afterPost(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) {
      if (e && e.label) {
        this.ruleForm.company = [e.label];
      } else {
        this.ruleForm.company = [];
      }
      this.$refs.ruleForm.validateField("company");
    },
  },
};
</script>

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