<template>
  <el-row :gutter="10" :loading="loading">
    <el-col :span="24">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        status-icon
        :rules="rulesThis"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="离职人" prop="resign_uid">
          <!-- :disabled="parmValue1.type == 1" -->
          <search-account
            :disabled="isDetail"
            :is-detail="isDetail"
            :value="ruleForm.resign_uid"
            :size="searchSize"
            :names="ruleForm.resign_name"
            :placeholder="'离职人名称'"
            @searchChange="handleResignName"
          />
        </el-form-item>
        <el-form-item label="接受人" prop="hand_uid">
          <!-- 
             -->
          <search-account
            :disabled="isDetail"
            :is-detail="isDetail"
            :value="ruleForm.hand_uid"
            :size="searchSize"
            :names="ruleForm.hand_name"
            :placeholder="'接受人名称'"
            @searchChange="handleHandoverName"
          />
        </el-form-item>
        12{{ isDetail }}id:{{ id }}
      </el-form>
    </el-col>
    <el-col :span="24" style="text-align: right">
      <el-button type="primary" @click="submitForm" v-if="id === 'add'">{{
        id === "add" ? "保 存" : "关 闭"
      }}</el-button>
      <!-- <el-button type="primary" @click="submitForm('flow')" v-if="isBtn"
        >保 存 并 发 起 流 程
      </el-button> -->
      <!-- <el-button @click="closeAddEdit" v-if="isBtn"
        >取 消 审 核 流 程</el-button
      > -->
      <el-button @click="closeAdd"> 关 闭</el-button>
      type:{{ parmValue1 }}
    </el-col>
  </el-row>
</template>
<script>
import asyncRequest from "@/apis/service/interest/handover";
import resToken from "@/mixins/resToken";
export default {
  name: "handover",
  props: ["id", "isDetail", "showModelThis", "parmValue1"],
  mixins: [resToken],
  data() {
    return {
      isBtn: false,
      loading: false,
      status: "", //存储详情接口返的状态
      showModelThis: this.showModel,
      resign_name: "", //离职人
      hand_name: "", //交接人
      ruleForm: {
        hand_uid: [],
        resign_uid: [],
      },
      rulesThis: this.rules,
      // 验证规则
      rules: {
        resign_uid: [
          {
            type: "array",
            required: true,
            message: "请选择离职人",
            trigger: "change",
          },
        ],
        hand_uid: [
          {
            type: "array",
            required: true,
            message: "请选择接受人",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    console.log(this.isDetail);
    this.initForm();
  },
  methods: {
    //   关闭弹窗,直接隐藏表单
    async closeAddEdit() {
      this.$emit("closeModel", false); //抛出一个事件，关闭弹窗
      const res = await asyncRequest.delete({ id: this.parmValue1.id });
      console.log(res);
      if (res && res.code === 0) {
        window.vm.$router.push({
          path: "handover",
        });
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    closeAdd() {
      this.$emit("closeModel", false);
    },
    async initForm() {
      this.loading = true;
      if (this.id === "add") {
        console.log(this.isBtn);
        this.isBtn = true;
        console.log("添加");
        this.isDetail = false;
        this.rulesThis = this.rules;
        await this.resetForm();
      } else {
        if (this.isDetail) {
          this.rulesThis = {};
          await this.initData();
        } else {
          this.rulesThis = this.rules;
          await this.resetForm();
          await this.initData();
        }
      }
      this.loading = false;
    },
    async initData() {
      const res = await asyncRequest.detail({ id: this.id });
      if (res && res.code === 0 && res.data) {
        let { hand_name, resign_name, hand_uid, resign_uid, id, status } =
          res.data;
        this.ruleForm = {
          resign_name,
          hand_name,
          hand_uid: hand_uid.split(","),
          resign_uid: resign_uid.split(","),
          type: status,
          id,
        };
        // this.$emit("refreshList");
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            resign_name: "", //离职人
            hand_name: "", //交接人
            hand_uid: [],
            resign_uid: [],
          };
        }
      });
    },
    async submitForm(flow) {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let resign_uid = this.ruleForm.resign_uid;
          let hand_uid = this.ruleForm.hand_uid;
          if (resign_uid.toString() !== hand_uid.toString()) {
            this.loading = true;
            const model = {
              id: this.ruleForm.id,
              resign_uid: resign_uid.toString(),
              hand_uid: hand_uid.toString(),
            };
            let res = {};
            if (this.id === "add") {
              delete model["id"];
              res = await asyncRequest.add(model);
            } else {
              res = await asyncRequest.update(model);
              if (flow == "flow") {
                this.updateStatus();
              }
            }
            window.vm.$router.push({
              path: "handover",
            });
            this.loading = false;
            if (res && res.code === 0) {
              const title = this.id === "add" ? "添加成功！" : "修改成功！";
              this.$notify.success({
                title,
                message: "",
              });
              this.showModelThis = false;
              // 刷新
              this.$emit("refresh", false);
            } else if (res && res.code >= 100 && res.code <= 104) {
              await this.logout();
            } else {
              this.$message.warning(res.message);
            }
          } else {
            this.$message.error("离职人和接收人不能相同");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async updateStatus() {
      let { id, type } = this.ruleForm;
      let model = {
        id: id,
        remark: "123",
        status: "1",
      };
      console.log(model);
      const res = await asyncRequest.updateStatus(model);
      if (res && res.code === 0) {
        const title = this.id === "add" ? "添加成功！" : "修改成功！";
        this.$notify.success({
          title,
          message: "",
        });
        this.showModelThis = false;
        // 刷新
        this.$emit("refresh", false);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    handleResignName(e) {
      if (e && e.id) {
        this.ruleForm.resign_uid = [e.id];
      } else {
        this.ruleForm.resign_uid = [];
      }
      this.$refs.ruleForm.validateField("resign_uid");
    },
    handleHandoverName(e) {
      if (e && e.id) {
        this.ruleForm.hand_uid = [e.id];
      } else {
        this.ruleForm.hand_uid = [];
      }
      this.$refs.ruleForm.validateField("hand_uid");
    },
  },
};
</script>

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