<template>
  <div>
    <div class="tool-bar">
      <ElForm inline @submit.prevent="paging">
        <ElFormItem>
          <ElInput
            v-model="searchForm.dictName"
            clearable
            placeholder="请输入要搜索的文字"
            @clear="reset"/>
        </ElFormItem>
        <ElFormItem>
          <ElButton :icon="elIcons.Search" :loading="searching" native-type="submit" type="primary">搜索</ElButton>
          <ElButton :icon="elIcons.Plus" type="primary" @click="addHandler">新建</ElButton>
        </ElFormItem>
      </ElForm>
    </div>
    <ElTable v-loading="searching" :data="tableData"
      cell-class-name="table-cell"
      class="table-list"
      empty-text="暂无数据"
      header-row-class-name="table-header"
      row-key="id">
      <ElTableColumn label="字典标识" prop="dictKey" width="180"/>

      <ElTableColumn label="字典名称" prop="dictName"/>

      <ElTableColumn label="备注" prop="memo"/>

      <ElTableColumn label="操作" width="180">
        <template #default="scope">
          <div class="action-btn">
            <ElPopconfirm
              cancel-button-text="否"
              cancel-button-type="primary"
              confirm-button-text="是"
              confirm-button-type="danger"
              placement="top"
              title="是否删除当前数据?"
              width="180"
              @confirm="delHandler(scope)">
              <template #reference>
                <ElButton :loading="deling" text type="danger">删除</ElButton>
              </template>
            </ElPopconfirm>
            <ElButton text type="primary" @click="modifyHandler(scope)">修改</ElButton>
            <ElButton text type="primary" @click="selectDictHandle(scope)">字典项</ElButton>
          </div>
        </template>
      </ElTableColumn>
    </ElTable>
    <ElPagination
      v-model:current-page="searchForm.current"
      v-model:page-size="searchForm.size"
      :hide-on-single-page="false"
      :page-sizes="[10, 20, 50, 100, 500]"
      :teleported="false"
      :total="totalCount"
      background layout="total, prev, pager, next, sizes, jumper"
      @change="paging"/>
    <DictForm ref="dictForm" @edit-succ="paging"/>
  </div>
</template>
<script lang="ts" setup>
import DictApi from '@/pages/sys/dict/dict-api.ts'
import { elIcons } from '@/common/element/element.ts'
import DictForm from '@/pages/sys/dict/DictForm.vue'

const totalCount = ref(0)
const tableData = ref<DictTypes.SearchDictResult[]>([])
const searchForm = ref<DictTypes.SearchDictParam>({
  orders: 'dict_key,id',
  current: 1,
  size: 20,
})
const searching = ref(false)
const deling = ref(false)
const dictFormIns = useTemplateRef<InstanceType<typeof DictForm>>('dictForm')

function showDialog(data?: DictTypes.SearchDictResult) {
  dictFormIns.value?.open(data)
}

function delHandler({row}: { row: DictTypes.SearchDictResult }) {
  deling.value = true
  DictApi.del([ row.id! ])
    .then(() => {
      ElMessage.success('删除成功')
      paging()
    })
    .finally(() => {
      deling.value = false
    })
}

function modifyHandler({row}: { row: DictTypes.SearchDictResult }) {
  showDialog(row)
}

function addHandler() {
  showDialog()
}

const emits = defineEmits([ 'searchDict' ])

function selectDictHandle({row}: { row: DictTypes.SearchDictResult }) {
  emits('searchDict', row)
}

function reset() {
  searchForm.value = {
    orders: 'dict_key,id',
    current: 1,
    size: 20,
  }
  paging()
}

function paging() {
  searching.value = true
  DictApi.paging(searchForm.value)
    .then(res => {
      totalCount.value = res.data?.total ?? 0
      tableData.value = res.data?.records ?? []
    })
    .finally(() => {
      searching.value = false
    })
}

onMounted(() => {
  paging()
})
</script>
<style lang="stylus" scoped>
.table-list {
  flex 1;
  margin 0 0 20px 0
  width 100%

  :deep(.table-header) {
    color #454C59

    th {
      background-color #EDF1F7
      font-weight 500
      position relative

      & > div {
        display flex
        gap 5px
        align-items center
      }

      &:not(:first-child) > div::before {
        position: absolute;
        top: 50%;
        left: 1px;
        width: 1px;
        background-color: #D3D7DE;
        transform: translateY(-50%);
        content: "";
        height 50%
      }
    }
  }

  :deep(.table-cell) {
    color #2F3540
  }

  .action-btn {
    width 100%
    display flex
    flex-wrap wrap

    & > button {
      margin 0
    }
  }
}

.tool-bar {
  display flex
  justify-content space-between
  margin 0 0 20px 0
}

.pagination {
  justify-content: end;
  margin: 8px;
}
</style>
