<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-form-item label="退回仓库" prop="return_stock">
        <el-col :span="4" style="width: 370px; padding: 0 0 0 10px">
          <search-supplier
            :size="searchSize"
            :value="wsm_supplierNo"
            :placeholder="'入库公司名称'"
            :disabled="false"
            :isDetail="false"
            @searchChange="supplierChange"
          />
        </el-col>
        <el-col :span="4" style="width: 220px; padding: 0 0 0 10px">
          <search-stock
            :size="searchSize"
            :value="wsm_code"
            :placeholder="'入库仓库名称'"
            :disabled="false"
            :isDetail="false"
            :isRelation="true"
            :companyCode="ruleForm.wsm_supplierNo"
            @searchChange="stockChange"
          />
        </el-col>
      </el-form-item>

      <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((i) => i == '005')) -->
        <el-button
          type="primary"
          @click="statusConfirm('1', '发起审核流程')"
          v-if="status === '0' && powers.some((i) => i == '012')"
          >发起审核流程
        </el-button>
        <el-button
          @click="statusConfirm('0', '取消审核流程')"
          plain
          v-if="status === '1' && powers.some((i) => i == '014')"
          >取消审核流程</el-button
        >
        <el-button
          @click="statusConfirm('-1', '作废该条信息')"
          type="danger"
          plain
          v-if="
            (status === '0' || status === '1') &&
            powers.some((i) => i == '015')
          "
          >作废该条信息</el-button
        >
        <el-button
          @click="statusConfirm('2', '通过审核')"
          type="primary"
          v-if="status === '1' && powers.some((i) => i == '016')"
          >通过审核</el-button
        >
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { isMobile } from "@/utils/validate.js";
import asyncRequest from "@/apis/service/sellOut/sellAfterApply";
import resToken from "@/mixins/resToken";
import { isnumber } from "@/utils/validate.js";
export default {
  name: "purchase_check",
  props: ["id", "showModelThis", "newTime", "returnCode"],
  mixins: [resToken],
  data() {
    const validatemobile = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("手机号不能为空！"));
      } else {
        if (!isMobile(value)) {
          callback(new Error("手机号格式不正确！"));
        } else {
          callback();
        }
      }
    };
    return {
      wsm_code: [],
      wsm_supplierNo: [],
      loading: false,
      status: "", //存储详情接口返的状态
      showModelThis: this.showModel,
      resign_name: "", //离职人
      hand_name: "", //交接人
      ruleForm: {
        wsm_supplierNo: "",
        wsm_code: "",
      },
      rulesThis: this.rules,
      // 验证规则
      rules: {
        return_stock: [
          {
            required: true,
            trigger: "change",
            message: "",
          },
        ],
      },
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
    };
  },
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "purchase_check"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },

  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.wsm_supplierNo = [];
          this.ruleForm = {
            return_store: "", //退回仓库
            return_goods_addr: [], //退货地址
            addr_detail: "", //退货详细地址
            addressee: "", //收件人
            addressee_phone: "", //收件人电话
            express_require: "", //物流要求
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          let model = {
            returnCode: this.returnCode,
            wsm_code: this.ruleForm.wsm_code,
          };
          const res = await asyncRequest.afterFeed(model);

          this.loading = false;
          if (res && res.code === 0) {
            this.$notify.success({
              title: "物流已反馈",
              message: "",
            });
            this.$emit("refresh", false);
            //   this.initForm();
          } 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;
        }
      });
    },
    handleStore(e) {
      this.return_store_options.find((item) => {
        if (item.value === e) {
          this.ruleForm.return_goods_addr = item.return_goods_addr;
          this.ruleForm.addr_detail = item.addr_detail;
          this.ruleForm.addressee = item.addressee;
          this.ruleForm.addressee_phone = item.addressee_phone;
          this.$refs.ruleForm.validateField("addressee");
          this.$refs.ruleForm.validateField("return_goods_addr");
          this.$refs.ruleForm.validateField("addr_detail");
          this.$refs.ruleForm.validateField("addressee_phone");
        }
      });
      this.ruleForm.return_store = e;
    },
    async supplierChange(e) {
      if (e && e.id) {
        this.ruleForm.wsm_supplierNo = e.code;
        this.wsm_supplierNo = [e.code];
      } else {
        this.ruleForm.wsm_supplierNo = "";
        this.wsm_supplierNo = [];
      }
      this.ruleForm.wsm_code = "";
      this.wsm_code = [];
      // this.ruleForm.page = 1;
      // this.pageInfo.curr = 1;
      // await this.searchList();
    },
    async stockChange(e) {
      if (e && e.id) {
        this.ruleForm.wsm_code = e.code;
        this.wsm_code = [e.code];
      } else {
        this.ruleForm.wsm_code = "";
        this.wsm_code = [];
      }
      console.log(e);
      // this.ruleForm.page = 1;
      // this.pageInfo.curr = 1;
      // await this.searchList();
    },
  },
};
</script>

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