<template>
  <!-- 选择人员对话框  -->
  <el-dialog
    :title="this.title"
    :visible.sync="childOpen"
    width="1100px"
    append-to-body
    :destroy-on-close="true"
  >
    <SelectRen
      v-if="this.open"
      ref="renlist"
      :userIds="receiverAccount"
      @fatherFunction="selectChange"
      @removeFunction="removeSelectuser"
    />

    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submitRen">选 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import SelectRen from "../SelectRen/index.vue";

export default {
  name: "selectUserByDept",
  components: {
    SelectRen,
  },
  props: ["title", "open", "inputAccount", "inputName"],
  data() {
    return {
      childOpen: false,
      receiverName: "", // 收件人的昵称
      receiverAccount: "", // 收件人的账号
    };
  },
  created() {
    this.receiverAccount = this.inputAccount;
    this.receiverName = this.inputName;
  },
  methods: {
    // 人员选择器----取消按钮
    cancel() {
      this.filterText = "";
      this.receiverAccount = this.inputAccount;
      this.receiverName = this.inputName;
      //this.open = false;
      this.childOpen = false;
    },
    /** 人员选择器中的 选定按钮操作 */
    submitRen() {
      this.$emit("ok", this.receiverAccount, this.receiverName);
      this.childOpen = false;
    },
    /** 人员选中  */
    selectChange(zh, xm) {
      let zhArr = [];
      let xmArr = [];
      if (this.receiverAccount != "") zhArr = this.receiverAccount.split(",");
      if (this.receiverName != "") xmArr = this.receiverName.split(",");
      let selzhArr = [];
      let selxmArr = [];
      if (zh != "") selzhArr = zh.split(",");
      if (xm != "") selxmArr = xm.split(",");
      for (let i = 0, len = selzhArr.length; i < len; i++) {
        if (!zhArr.includes(selzhArr[i])) {
          //放一起的目的是 解决重名引起的 账号和名称不对应 问题
          zhArr.push(selzhArr[i]);
          xmArr.push(selxmArr[i]);
        }
      }
      this.receiverAccount = zhArr.join(",");
      this.receiverName = xmArr.join(",");
    },
    /**  人员取消选择  */
    removeSelectuser(zh, xm) {
      let zhArr = this.receiverAccount.split(",");
      let xmArr = this.receiverName.split(",");

      for (var i = 0; i < zhArr.length; i++) {
        if (zhArr[i] == zh) {
          //放一起的目的是 解决重名引起的 账号和名称不对应 问题
          zhArr.splice(i, 1);
          xmArr.splice(i, 1);
          break;
        }
      }
      this.receiverAccount = zhArr.join(",");
      this.receiverName = xmArr.join(",");
    },
  },
  watch: {
    open(val) {
      this.childOpen = this.open;
    },
    inputAccount(val) {
      this.receiverAccount = this.inputAccount;
      this.receiverName = this.inputName;
    },
  },
};
</script>
