<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
            @change="handleChange"
            v-model="ruleForm.own"
            placeholder="费用承担方"
            :disabled="
              !(status === '3' && powers.some((item) => item == '040'))
            "
            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-form-item label="物流单号" prop="post_code">
          <el-input
            placeholder="物流单号"
            :disabled="
              !(status === '3' && powers.some((item) => item == '040'))
            "
            v-model="ruleForm.post_code"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="物流公司" prop="company">
          <search-express
            :value="ruleForm.company"
            :placeholder="'物流公司'"
            :names="''"
            :disabled="
              !(status === '3' && powers.some((item) => item == '040'))
            "
            :is-detail="
              !(status === '3' && powers.some((item) => item == '040'))
            "
            @searchChange="handleCompany"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12" v-if="ruleForm.own === '2'">
        <el-form-item label="物流费用" prop="post_fee">
          <el-input
            placeholder="物流费用"
            :disabled="
              !(status === '3' && powers.some((item) => item == '040'))
            "
            v-model="ruleForm.post_fee"
          >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col
        :span="24"
        style="text-align: right; margin: 0 0 10px 0"
        v-if="status === '3' && powers.some((item) => item == '040')"
      >
        <el-button type="primary" @click="submitForm" size="mini"
          >保 存</el-button
        >
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/sellAfterApply";
import resToken from "@/mixins/resToken";
import { isAlphanumeric, isnumber, isnumber2 } from "@/utils/validate.js";
export default {
  name: "sellAfterApplyDetail",
  props: ["id", "newTime", "sitem"],
  mixins: [resToken],
  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: [
        {
          value: "1",
          label: "个人承担",
        },
        {
          value: "2",
          label: "购买方公司承担",
        },
      ],
      code: "",
      isShow: false,
      loading: false,
      status: "", //存储详情接口返的状态
      showModelThis: this.showModel,
      resign_name: "", //离职人
      hand_name: "", //交接人
      ruleForm: {
        returnCode: "",
        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,
          },
        ],
      },
    };
  },
  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "sellAfterApplyDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },

  mounted() {
    this.initForm();
  },
  watch: {
    id: function (val) {
      if (val) {
        this.initForm();
      }
    },
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },
  methods: {
    //   关闭弹窗,直接隐藏表单
    // async closeAddEdit() {
    //   this.$emit("closeModel", false); //抛出一个事件，关闭弹窗
    //
    // },
    closeAdd() {
      this.showModelThis = false;
    },
    async initForm() {
      this.loading = true;
      this.status = "";
      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();
          this.code = this.returnCode;
          const {
            returnCode,
            post_own,
            post_company,
            post_code,
            post_fee,
            status,
          } = this.sitem;
          this.status = status;
          this.ruleForm = {
            returnCode: returnCode || "",
            own: post_own || "1",
            company: post_company ? [post_company] : [], //物流公司
            post_code: post_code || "", //物流单号
            post_fee: post_fee || "0", //物流费用
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          // model.returnCode = this.code;
          // delete model["company"];
          model.company = model.company[0];
          const res = await asyncRequest.afterPost(model); //售后快递录入
          this.loading = false;
          if (res && res.code === 0) {
            this.$notify.success({
              title: res.message,
              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.company = e.shortName;
        this.ruleForm.company = [e.label];
      }
    },
    handleChange(e) {
      if (e && e == "2") {
        this.isShow = true;
      } else {
        this.isShow = false;
      }
    },
  },
};
</script>

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