<template>
  <el-row :gutter="10" :loading="loading">
    <el-col :span="24">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="离职人" prop="resign_name">
          <search-account
            :value="ruleForm.resign_uid"
            :size="searchSize"
            :names="ruleForm.resign_name"
            :is-detail="isDetail"
            :disabled="!isDetail"
            :placeholder="'离职人名称'"
            @searchChange="handleResignName"
          />
        </el-form-item>
        <el-form-item label="接受人" prop="hand_name">
          <search-account
            :value="ruleForm.hand_uid"
            :size="searchSize"
            :names="ruleForm.hand_name"
            :is-detail="isDetail"
            :disabled="!isDetail"
            :placeholder="'接受人名称'"
            @searchChange="handleHandoverName"
          />
        </el-form-item>
        12{{ edit_form }}
      </el-form>
    </el-col>
    <el-col :span="24" style="text-align: right">
      <el-button v-if="isDetail" type="primary" @click="submitForm"
        >保 存
      </el-button>

      <el-button v-if="isDetail" type="primary" @click="submitForm12"
        >保 存 并 发 起 流 程
      </el-button>
      <el-button @click="closeAddEdit" v-if="isDetail">{{
        isDetail ? "关 闭" : "取 消"
      }}</el-button>
    </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", "edit_form"],
  mixins: [resToken],
  data() {
    return {
      parmValue: "parmValue",
      roleList: [],
      loading: false,
      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() {
    this.initForm();
    console.log(this.ruleForm);
    console.log(this.parmValue1);
  },
  // watch: {
  //   parmValue: function (newV, oldV) {
  //     console.log(newV);
  //     console.log(oldV);
  //   },
  // },
  methods: {
    //   关闭弹窗,直接隐藏表单
    closeAddEdit() {
      this.isShowForm = false;
      this.$emit("closeModel", false); //抛出一个事件，关闭弹窗
    },

    async initForm() {
      this.loading = true;
      // if (this.id === "add") {
      //   // this.title = "添加离职交接";
      //   this.rulesThis = this.rules;
      //   await this.resetForm();
      // } else {
      //   if (this.isDetail) {
      //     this.rulesThis = {};
      //   }
      //   await this.resetForm();
      //   await this.initData();
      // }
      console.log("initform");
      if (this.isDetail) {
        this.rulesThis = {};
      }
      await this.resetForm();
      await this.initData();
      this.loading = false;
    },
    async initData() {
      console.log(this.parmValue1);
      const res = await asyncRequest.detail({ id: this.edit_form.id });
      if (res && res.code === 0 && res.data) {
        console.log(res);
        let { hand_name, resign_name, hand_uid, resign_uid } = res.data;
        this.ruleForm = {
          resign_name,
          hand_name,
          hand_uid: hand_uid.split(","),
          resign_uid: resign_uid.split(","),
        };
        console.log(this.ruleForm);
        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() {
      await this.$refs.ruleForm.validate(async (valid) => {
        console.log(valid);
        if (valid) {
          let resign_name1 = this.ruleForm.resign_uid;
          let hand_name1 = this.ruleForm.hand_uid;
          if (resign_name1.toString() !== hand_name1.toString()) {
            this.loading = true;
            const model = {
              id: this.id,
              resign_uid: resign_name1.toString(),
              hand_uid: hand_name1.toString(),
            };
            let res = {};
            // if (this.id === "add") {
            //   delete model["id"];
            //   res = await asyncRequest.add(model);
            //   console.log(res);
            // } else {
            res = await asyncRequest.update(model);
            // }
            console.log(res);
            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 {
            console.log("12");

            this.$message.error("离职人和接收人不能相同");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    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) {
      console.log(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>
.account {
}
</style>
