<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="id !== 'add' ? 12 : 24">
        <el-form-item label="离职人" prop="resign_uid">
          <search-account
            :disabled="
              !(
                id === 'add' ||
                (status === '0' && powers.some((i) => i == '005'))
              )
            "
            :is-detail="id !== 'add'"
            :value="ruleForm.resign_uid"
            :size="searchSize"
            :names="resign_name"
            :placeholder="'离职人名称'"
            @searchChange="handleResignName"
          />
        </el-form-item>
      </el-col>
      <el-col :span="id !== 'add' ? 12 : 24">
        <el-form-item label="接受人" prop="hand_uid">
          <search-account
            :disabled="
              !(
                id === 'add' ||
                (status === '0' && powers.some((i) => i == '005'))
              )
            "
            :is-detail="id !== 'add'"
            :value="ruleForm.hand_uid"
            :size="searchSize"
            :names="hand_name"
            :placeholder="'接受人名称'"
            @searchChange="handleHandoverName"
          />
        </el-form-item>
      </el-col>
      <el-col :span="id !== 'add' ? 12 : 24">
        <el-form-item label="生效时间" prop="expire_date">
          <el-date-picker
            v-model="ruleForm.expire_date"
            type="datetime"
            style="width: 100%"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="生效时间"
            :disabled="
              !(
                id === 'add' ||
                (status === '0' && powers.some((i) => i == '005'))
              )
            "
          >
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="审核备注" prop="remark">
          <el-input
            type="textarea"
            disabled
            :autosize="{ minRows: 4, maxRows: 4 }"
            placeholder="审核备注"
            maxlength="250"
            show-word-limit
            v-model="ruleForm.remark"
          />
        </el-form-item>
      </el-col>
      <el-col
        :span="id !== 'add' ? 12 : 24"
        style="text-align: right; padding: 0 0 20px 0"
        v-if="
          id === 'add' ||
          (status === '0' && powers.some((i) => i == '005'))
        "
      >
        <el-button type="primary" :size="'mini'" @click="submitForm"
          >保 存</el-button
        >
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import asyncRequest from "@/apis/service/interest/handover";
import resToken from "@/mixins/resToken";
export default {
  name: "handover",
  props: ["id", "newTime", "sitem"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      disabled: true,
      status: "", //存储详情接口返的状态
      resign_name: "", //离职人
      hand_name: "", //交接人
      ruleForm: {
        hand_uid: [],
        resign_uid: [],
        expire_date: "",
        remark: "",
      },
      rulesThis: this.rules,
      // 验证规则
      rules: {
        resign_uid: [
          {
            type: "array",
            required: true,
            message: "请选择离职人",
            trigger: "change",
          },
        ],
        hand_uid: [
          {
            type: "array",
            required: true,
            message: "请选择接受人",
            trigger: "change",
          },
        ],
        expire_date: [
          {
            required: true,
            message: "请选择生效时间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "handoverDetail"
        ) || {};
      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 initForm() {
      this.loading = true;
      this.status = "";
      this.rulesThis = this.rules;
      await this.resetForm();
      this.loading = false;
    },

    async resetForm() {
      this.resign_name = "";
      this.hand_name = "";
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          console.log(this.sitem);
          const {
            status,
            resign_name,
            hand_name,
            hand_uid,
            resign_uid,
            expire_date,
            remark,
          } = this.sitem;
          this.resign_name = resign_name || "";
          this.hand_name = hand_name || "";
          this.status = status || "";
          this.ruleForm = {
            hand_uid: hand_uid ? hand_uid.split(",") : [],
            resign_uid: resign_uid ? resign_uid.split(",") : [],
            expire_date: expire_date || "",
            remark: remark || "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if(!this.loading){

         
          const { resign_uid, hand_uid, expire_date } = this.ruleForm;
          let rUid = resign_uid.toString(),
            hUid = hand_uid.toString();
          if (rUid === hUid) {
            this.$message.error("离职人和接收人不能相同");
            return;
          }
          this.loading = true;
          const model = {
            id: this.id,
            resign_uid: rUid,
            hand_uid: hUid,
            expire_date: expire_date,
          };
          let res = {};
          if (this.id === "add") {
            delete model["id"];
            res = await asyncRequest.add(model);
          } else {
            res = await asyncRequest.update(model);
          }

          this.loading = false;
          if (res && res.code === 0) {
            const title = this.id === "add" ? "添加成功！" : "修改成功！";
            this.$notify.success({
              title,
              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;
        }
      });
    },

    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>
