<template>
  <div class="ps-transfer-panel">
    <p class="ps-transfer-panel__header">
      <ps-checkbox
        v-model="allChecked"
        :indeterminate="isIndeterminate"
        @change="handleAllCheckedChange"
      >
        {{ title }}
        <span>{{ checkedSummary }}</span>
      </ps-checkbox>
    </p>

    <div
      :class="['ps-transfer-panel__body', hasFooter ? 'is-with-footer' : '']"
    >
      <ps-input
        v-if="filterable"
        v-model="query"
        class="ps-transfer-panel__filter"
        size="small"
        :placeholder="placeholder"
        @mouseenter="inputHover = true"
        @mouseleave="inputHover = false"
      >
        <template #prefix>
          <i
            :class="['ps-input__icon', 'ps-icon-' + inputIcon]"
            @click="clearQuery"
          ></i>
        </template>
      </ps-input>
      <ps-checkbox-group
        v-show="!hasNoMatch && data.length > 0"
        v-model="checked"
        :class="{ 'is-filterable': filterable }"
        class="ps-transfer-panel__list"
      >
        <ps-checkbox
          v-for="item in filteredData"
          :key="item[keyProp]"
          class="ps-transfer-panel__item"
          :label="item[keyProp]"
          :disabled="item[disabledProp]"
        >
          <option-content
            :option="optionRender(item)"
          />
        </ps-checkbox>
      </ps-checkbox-group>
      <p v-show="hasNoMatch || data.length === 0" class="ps-transfer-panel__empty">
        {{ hasNoMatch ? t('ps.transfer.noMatch') : t('ps.transfer.noData') }}
      </p>
    </div>
    <p v-if="hasFooter" class="ps-transfer-panel__footer">
      <slot></slot>
    </p>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, reactive, toRefs } from 'vue'
import { t } from '@present-ui/locale'
import PsCheckbox from '@present-ui/checkbox'
import PsCheckboxGroup from '@present-ui/checkbox-group'
import PsInput from '@present-ui/input'
import { useCheck, CHECKED_CHANGE_EVENT } from './useCheck'

export default defineComponent({
  name: 'PsTransferPanel',

  components: {
    PsCheckboxGroup,
    PsCheckbox,
    PsInput,
    OptionContent: ({ option }) => option,
  },

  props: {
    data: {
      type: Array,
      default() {
        return []
      },
    },
    optionRender: Function,
    placeholder: String,
    title: String,
    filterable: Boolean,
    format: Object,
    filterMethod: Function,
    defaultChecked: Array,
    props: Object,
  },

  emits: [CHECKED_CHANGE_EVENT],

  setup(props, { emit, slots }) {
    const panelState = reactive({
      checked: [],
      allChecked: false,
      query: '',
      inputHover: false,
      checkChangeByUser: true,
    })

    const {
      labelProp,
      keyProp,
      disabledProp,
      filteredData,
      checkedSummary,
      isIndeterminate,
      handleAllCheckedChange,
    } = useCheck(props, panelState, emit)

    const hasNoMatch = computed(() => {
      return panelState.query.length > 0 && filteredData.value.length === 0
    })

    const inputIcon = computed(() => {
      return panelState.query.length > 0 && panelState.inputHover
        ? 'circle-close'
        : 'search'
    })

    const hasFooter = computed(() => !!slots.default()[0].children.length)

    const clearQuery = () => {
      if (inputIcon.value === 'circle-close') {
        panelState.query = ''
      }
    }

    const {
      checked,
      allChecked,
      query,
      inputHover,
      checkChangeByUser,
    } = toRefs(panelState)

    return {
      labelProp,
      keyProp,
      disabledProp,
      filteredData,
      checkedSummary,
      isIndeterminate,
      handleAllCheckedChange,

      checked,
      allChecked,
      query,
      inputHover,
      checkChangeByUser,

      hasNoMatch,
      inputIcon,
      hasFooter,
      clearQuery,

      t,
    }
  },
})
</script>
