<template>
  <el-form
    ref="ruleForm"
    :loading="loading"
    :model="ruleForm"
    status-icon
    :rules="rulesThis"
    label-width="100px"
    style="width: 100%"
    class="demo-ruleForm"
  >
    {{ returnCode }}
    <el-row>
      <el-col :span="12">
        <el-form-item label="费用承担方" prop="express_bear">
          <el-select
            clearable
            @change="handleChange"
            v-model="ruleForm.express_bear"
            placeholder="请选择"
            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="express_company">
          <search-express
            :value="ruleForm.express_company"
            :placeholder="'请选择退货物流'"
            :names="''"
            :disabled="false"
            :is-detail="false"
            @searchChange="handleCompany"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="物流单号" prop="express_numbres">
          <el-input
            placeholder="请输入物流单号"
            v-model="ruleForm.express_numbres"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12" v-if="isShow">
        <el-form-item label="物流费用" prop="express_fee">
          <el-input placeholder="请输入物流费用" v-model="ruleForm.express_fee">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24" style="text-align: right">
        <el-button type="primary" @click="submitForm" v-if="true"
          >保 存</el-button
        >
        <!-- id === 'add' ||
            (status === '0' && powers.some((item) => item == '005')) -->
        <el-button
          type="primary"
          @click="statusConfirm('1', '发起审核流程')"
          v-if="status === '0' && powers.some((item) => item == '012')"
          >发起审核流程
        </el-button>
        <el-button
          @click="statusConfirm('0', '取消审核流程')"
          plain
          v-if="status === '1' && powers.some((item) => item == '014')"
          >取消审核流程</el-button
        >
        <el-button
          @click="statusConfirm('-1', '作废该条信息')"
          type="danger"
          plain
          v-if="
            (status === '0' || status === '1') &&
            powers.some((item) => item == '015')
          "
          >作废该条信息</el-button
        >
        <el-button
          @click="statusConfirm('2', '通过审核')"
          type="primary"
          v-if="status === '1' && powers.some((item) => item == '016')"
          >通过审核</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 } from "@/utils/validate.js";
export default {
  name: "purchase_check",
  props: ["id", "showModelThis", "newTime", "returnCode"],
  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(new Error("物流费用只能为数字！"));
        } else {
          callback();
        }
      }
    };
    return {
      options: [
        {
          value: "1",
          label: "个人承担",
        },
        {
          value: "2",
          label: "公司承担",
        },
        {
          value: "3",
          label: "供应商承担",
        },
      ],
      code: "",
      isShow: false,
      loading: false,
      status: "", //存储详情接口返的状态
      showModelThis: this.showModel,
      resign_name: "", //离职人
      hand_name: "", //交接人
      ruleForm: {
        post_company: "", //入参快递公司

        express_bear: "", //物流费用承担方
        express_company: [], //物流公司
        express_numbres: "", //物流单号
        express_fee: 0, //物流费用
        page: 1,
        size: 15,
      },
      rulesThis: this.rules,
      // 验证规则
      rules: {
        express_bear: [
          {
            // type: "array",
            required: true,
            trigger: "change",
            message: "请选择物流费用承担方",
          },
        ],
        express_company: [
          {
            type: "array",
            required: true,
            trigger: "change",
            message: "请选择退回仓库",
          },
        ],
        express_numbres: [
          {
            required: true,
            trigger: "blur",
            validator: validateexpress,
          },
        ],
        express_fee: [
          {
            required: true,
            trigger: "blur",
            validator: validateexpressFee,
          },
        ],
      },
    };
  },
  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "purchase_check"
        ) || {};
      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;
      if (this.id === "add") {
        this.status = "";
        this.rulesThis = this.rules;
        await this.resetForm();
      } else {
        this.rulesThis = this.rules;
        await this.resetForm();
        // await this.initData();
      }
      this.loading = false;
    },

    async resetForm() {
      this.resign_name = "";
      this.hand_name = "";
      this.status = "";
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.code = this.returnCode;
          this.ruleForm = {
            express_bear: "",
            post_company: "", //入参快递公司
            express_company: [], //物流公司
            express_numbres: "", //物流单号
            express_fee: 0, //物流费用
            type: "",
            name: "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          let _model = {
            returnCode: this.code, //退货单号
            own: model.express_bear, //费用承担方
            company: model.post_company, //快递公司
            post_code: model.express_numbres, //快递编号
            post_fee: model.express_fee || 0, //快递费用
          };
          const res = await asyncRequest.afterPost(_model);
          this.loading = false;
          if (res && res.code === 0) {
            // const title = this.id === "add" ? "添加成功！" : "修改成功！";
            this.$notify.success({
              title: res.message,
              message: "",
            });
            console.log("抛出事件");
            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.post_company = e.shortName;
        this.ruleForm.express_company = [e.code];
        this.ruleForm.page = 1;
      }
    },
    handleChange(e) {
      if (e && e == "2") {
        this.isShow = true;
      } else {
        this.isShow = false;
      }
    },
  },
};
</script>

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