<template>
  <div>
    <a-form-item-rest>
      <div class="shuttleFrame-form-input" @click="openModal" v-if="openMode == 'input'">
        <div class="shuttleFrame-form-input-field_select">
          <span class="shuttleFrame-deep-select">
            <span class="shuttleFrame-deep-select-input">
              <span class="shuttleFrame-deep-select-typeAlert">
                <button class="shuttleFrame-deep-select-button">
                  <plus-circle-outlined />
                  {{ searchType == 'dept' ? '选择部门' : '选择人员' }}
                </button>
              </span>
              <span v-if="selectedValueAll.length > 0" class="shuttleFrame-deep-select-text">
                <span
                  class="shuttleFrame-deep-select-text-name"
                  v-for="(item, index) in selectedValueAll"
                  :key="index">
                  {{ item.name }}
                </span>
              </span>
            </span>
          </span>
        </div>
      </div>
      <div v-else>
        <button @click="openModal">权限</button>
      </div>
      <ns-modal
        v-model:visible="pageFormVisible"
        :maskClosable="false"
        style="width: 80%"
        :keyboard="false"
        :title="title"
        :ok-button-props="{ disabled: emitData.length <= 0 }"
        @ok="pageFormOk"
        :destroyOnClose="true"
        okText="确认"
        cancelText="取消"
        @cancel="pageFormCancel">
        <NsShuttleFrameOuter ref="nsShuttleFrameOuter" :tabs="tabs" />
      </ns-modal>
    </a-form-item-rest>
  </div>
</template>
<script lang="ts">
  import { defineComponent, inject, provide, reactive, ref, unref, watch } from 'vue';
  import { PlusCircleOutlined } from '@ant-design/icons-vue';
  import mitt from 'mitt';

  type selectedObj = {
    id?: number;
    name?: string;
    type?: string;
  };
  export default defineComponent({
    name: 'NsShuttleFrame',
    components: { PlusCircleOutlined },
    props: {
      openMode: {
        type: String,
        default: 'input',
      },
      searchType: {
        type: String,
        default: 'dept',
      },
      tabs: {
        type: Array,
      },
      title: {
        type: String,
        default: '设置成员',
      },
      value: [Array, String],
    },
    emits: ['change'],
    setup(props, ctx) {
      console.log('props', props);
      const lmEmitter = mitt();
      provide('lmEmitter', lmEmitter);

      const emitData = ref([]);
      const selectedValueAll = ref<selectedObj[]>([]);
      lmEmitter.on('setEmitData', (val: []) => {
        emitData.value = val;
      });

      const pageFormVisible = ref(false);
      const modalTypeTitle = ref('');
      const pageFormRef = ref();

      const pageFormOk = () => {
        pageFormVisible.value = false;
        selectedValueAll.value = JSON.parse(JSON.stringify(emitData.value));
        let data;
        if (props.openMode == 'input') {
          if (props.tabs[0].filterMultiple) {
            data = [];
            selectedValueAll.value.map((item) => {
              data.push(item.name);
            });
          } else {
            data = selectedValueAll.value[0].name;
          }
          ctx.emit('change', data);
        } else {
          ctx.emit('change', selectedValueAll.value);
        }
      };

      const pageFormCancel = () => {};

      const nsShuttleFrameOuter = ref(null);
      const openModal = () => {
        pageFormVisible.value = true;
        let sendData = JSON.parse(JSON.stringify(selectedValueAll.value));
        setTimeout(() => {
          lmEmitter.emit('setDefaultDataMAD', sendData);
          lmEmitter.emit('setDefaultDataMOD', sendData);
        }, 100);
      };

      watch(
        () => props.value,
        (val) => {
          selectedValueAll.value = [];
          if (val) {
            if (props.openMode == 'input') {
              if (!props.tabs[0].filterMultiple) {
                selectedValueAll.value = [
                  {
                    name: val,
                    type: props.searchType,
                  },
                ];
              } else {
                val.map((item) => {
                  selectedValueAll.value.push({
                    name: item,
                    type: props.searchType,
                  });
                });
              }
            }
          }
        },
        { immediate: true, deep: true },
      );

      return {
        pageFormVisible,
        pageFormRef,
        pageFormOk,
        modalTypeTitle,
        pageFormCancel,
        openModal,
        emitData,
        selectedValueAll,
        nsShuttleFrameOuter,
      };
    },
  });
</script>
<style lang="less" scoped>
  .shuttleFrame-form-input {
    display: flex;
    -webkit-box-align: start;
    align-items: flex-start;

    &-field_select {
      flex: 1;
      overflow: hidden;
    }

    .shuttleFrame-deep-select {
      width: 100%;
      display: inline-block;

      .icon-add {
        transform: scale(1);
        margin-left: 0;
        margin-right: 4px;
        display: inline-block;
        vertical-align: middle;
        font-family: NextIcon;
        font-style: normal;
        font-weight: 400;
        text-transform: none;
        -webkit-font-smoothing: antialiased;
        &::before {
          width: 12px;
          font-size: 12px;
          line-height: inherit;
          content: '\e8b5';
          display: inline-block;
          vertical-align: middle;
          text-align: center;
          color: #171a1d;
        }
      }

      &-input {
        display: inline-flex;
        -webkit-box-align: baseline;
        white-space: nowrap;
        align-items: baseline;
        height: auto;
        padding: 3px 8px 0 3px;
        width: 100%;
        min-width: 100px;
        outline: 0;
        vertical-align: middle;
        border-collapse: separate;
        font-size: 0;
        line-height: 1;
        border-spacing: 0;
        transition: all 0.1s linear;
        border: 1px solid #e5e6e8;
        border-radius: 6px;
        cursor: pointer;
      }

      &-typeAlert {
        display: inline-block;
        line-height: inherit;
        vertical-align: top;
        margin-bottom: 3px;
        width: auto;
        font-size: 14px;
        white-space: nowrap;

        button {
          border-radius: 6px;
          padding: 0 8px;
          height: 28px;
          font-size: 12px;
          border-width: 1px;
          border-style: solid;
          background: #fff;
          border-color: #e5e6e8;
          cursor: pointer;
        }
      }

      &-button:hover {
        border: 1px solid @primary-color;
        color: @primary-color;
      }

      &-text {
        line-height: 22px;
        height: 22px;
        display: inline !important;
        font-size: 14px;
        margin-left: 6px;

        &-name {
          vertical-align: middle;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          display: inline-block;
          margin-right: 6px;
        }
      }
    }
  }
</style>
