<template>
  <div class="components-input-demo-presuffix">
    <!---->
    <a-input @click="openModal" :placeholder="placeholder || $locale('m.components.label.qing-dian-ji-xuan-ze-yong-hu')" v-model="userNames" readOnly :disabled="disabled || readOnly">
      <ych-icon slot="prefix" type="user" :title="$locale('m.components.label.yonghuxuanzekongjian')"/>
      <ych-icon v-if="userIds && !disabled && !readOnly" slot="suffix" type="close-circle" @click="handleEmpty" :title="$locale('m.common.button.clear')"/>
    </a-input>

    <ych-select-user-by-dept-modal
      ref="selectModal"
      :modal-width="modalWidth"
      :multi="multi"
      @ok="handleOK"
      :customReturnField="customReturnField"
      :user-ids="userIds"
      :currentOrgAsRoot="currentOrgAsRoot"
      :filterAuth="filterAuth"
      :isResetOrg="isResetOrg"
      :orgTreeShowDeptAbbr="orgTreeShowDeptAbbr"
      :dept-id="customDeptId || linkageValue"
      :customOrgOrderSql="customOrgOrderSql"
      :customOrgFilterSql="customOrgFilterSql"
      @initComp="initComp"/>

  </div>
</template>

<script>
  import YchSelectUserByDeptModal from './YchSelectUserByDeptModal'
  import LocleUtil from "../../../utils/LocleUtil";

  export default {
    name: 'YchSelectUser',
    components: {
      YchSelectUserByDeptModal
    },
    props: {
      modalWidth: {
        type: Number,
        default: 1000,
        required: false
      },
      multi: {
        type: Boolean|String,
        default: false,
        required: false
      },
      value: {
        type: String,
        required: false
      },
      disabled: {
        type: Boolean,
        required: false,
        default: false
      },
      readOnly: {
        type: Boolean,
        required: false,
        default: false
      },
      // 自定义返回字段，默认返回 id
      customReturnField: {
        type: String,
        default: 'id'
      },
      customOrgOrderSql: String, // 自定义组织树排序Sql
      customOrgFilterSql: String, // 自定义组织树过滤Sql
      placeholder: {
        type: String,
      },
      currentOrgAsRoot:{
        type:Boolean,
        default:false
      },
      filterAuth:{
        type:Boolean,
        default:false
      },
      orgTreeShowDeptAbbr:{
        type:Boolean,
        default: false
      },
      linkage: String,
      linkageMap: {
        type: Object,
        default: () => {
        }
      },
    },
    data() {
      return {
        visible: false,
        confirmLoading: false,
        userNames: "",
        userIds: '',
        isResetOrg:false,
        customDeptId: null,
      }
    },
    computed: {
      linkageValue() {
        if (this.linkage && this.linkageMap) {
          const value = this.linkageMap[this.linkage]
          return value
        }
        return null
      }
    },
    mounted() {
      this.userIds = this.value
    },
    watch: {
      value(val) {
        // if (this.customReturnField === 'id') {
          this.userIds = val
        // }
      },
      linkageValue: {
        handler(newValue, oldValue) {
          if (oldValue) {
            // 清空选项
            this.handleEmpty()
          }
        }
      }
    },
    methods: {
      $locale(params) {
        return LocleUtil.getLocale(params)
      },
      setCustomDeptId(customDeptId) {
        this.customDeptId = customDeptId
      },
      initComp(userNames) {
        this.userNames = userNames

        this.$emit("setName", this.userNames)
      },
      openModal() {
        if (this.linkage && !this.linkageValue) {
          this.$message.warning(this.$locale('m.eform.formDesign.label.select-dept-tip'))
          return
        }
        this.isResetOrg = true
        this.$refs.selectModal.showModal()
      },
      handleOK(rows, idstr) {
        let value = ''
        if (!rows || rows.length === 0) {
          this.userNames = ''
          this.userIds = ''
        } else {
          value = rows.map(row => row[this.customReturnField]).join(',')
          this.userNames = rows.map(row => row['realname']).join(',')
          this.userIds = idstr
        }
        this.$emit("change", value)

        this.$emit("setName", this.userNames)
        this.$emit("setSelectedRows", rows)
        this.isResetOrg = false
      },
      handleEmpty() {
        this.handleOK('')
      }
    },
    model: {
      prop: 'value',
      event: 'change'
    }
  }
</script>

<style lang="less" scoped>
  .components-input-demo-presuffix .anticon-close-circle {
    cursor: pointer;
    color: #ccc;
    transition: color 0.3s;
    font-size: 12px;
  }

  .components-input-demo-presuffix .anticon-close-circle:hover {
    color: #f5222d;
  }

  .components-input-demo-presuffix .anticon-close-circle:active {
    color: #666;
  }
</style>
