<template>
  <PageWrapper>
    <n-grid x-gap="12" cols="4">
      <n-grid-item span="1">
        <n-card shadow="hover" class="border-0">
          <template #header>
            <n-grid cols="24">
              <n-gi span="18">
                <n-input
                  type="text"
                  v-model:value="params.name"
                  placeholder="请输入字典名称"
                  clearable
                />
              </n-gi>
              <n-gi span="6" style="text-align: right">
                <n-button
                  type="primary"
                  @click="
                    pager.page = 1;
                    loadDataTable();
                  "
                >
                  <template #icon>
                    <n-icon>
                      <SearchOutlined />
                    </n-icon> </template
                  >查询
                </n-button>
              </n-gi>
            </n-grid>
            <div style="margin-top: 15px">
              <n-space>
                <n-button type="primary" @click="handleAdd" v-perm="['sys:dict:add']">
                  <template #icon>
                    <n-icon>
                      <PlusOutlined />
                    </n-icon>
                  </template>
                  新建
                </n-button>
                <n-button type="warning" @click="handleEdit" v-perm="['sys:dict:edit']">
                  <template #icon>
                    <n-icon>
                      <FormOutlined />
                    </n-icon> </template
                  >编辑
                </n-button>
                <n-button type="error" @click="handleDelete()" v-perm="['sys:dict:delete']">
                  <template #icon>
                    <n-icon>
                      <DeleteOutlined />
                    </n-icon> </template
                  >删除
                </n-button>
              </n-space>
            </div>
          </template>
          <template #default>
            <div :style="{ height: fwbHeight + 'px' }" class="dict-list-box">
              <div
                v-for="(item, index) in dictDataList"
                :key="index"
                @click="onCheckedRow(item)"
                class="dict-item"
                :class="item.id == dictId ? 'active' : ''"
              >
                <span class="t1"
                  >{{ item.name }}<span class="t2">({{ item.code }})</span></span
                >
              </div>
            </div>
            <pagination
              style="justify-content: flex-end"
              class="mt-10 flex"
              @change="loadDataTable"
              v-model="pager"
            />
          </template>
        </n-card>
      </n-grid-item>
      <n-grid-item span="3">
        <n-card shadow="hover" class="mb-4 border-0 proCard">
          <dictItem :dictId="dictId" v-if="dictItemShow" />
        </n-card>
      </n-grid-item>
    </n-grid>
    <editDialog
      v-if="editVisible"
      :dictId="dictId"
      v-model:visible="editVisible"
      ref="createModalRef"
      @success="loadDataTable()"
    />
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { ref, nextTick, defineAsyncComponent, onMounted } from 'vue';
  import { getDictList, dictDelete } from '@/api/data/dictionary';
  import { PlusOutlined, FormOutlined, DeleteOutlined, SearchOutlined } from '@vicons/antd';
  import editDialog from './edit.vue';
  import dictItem from './dictItem.vue';
  import { basicModal, useModal } from '@/components/Modal';
  import { useMessage, useDialog } from 'naive-ui';

  /**
   * 定义参数变量
   */
  const dictId = ref(0);
  const createModalRef = ref();
  const dictItemShow = ref(false);
  const editVisible = ref(false);
  const message = useMessage();
  const dialog = useDialog();

  /**
   * 定义查询参数
   */
  const params = ref({
    name: '',
  });
  const dictDataList = ref([]);

  /**
   * 定义分页参数
   */
  const pager = ref({
    page: 1,
    size: 10,
    count: dictDataList.value.length,
  });
  const fwbHeight = document.body.clientHeight - 370;

  /**
   * 执行添加
   */
  const handleAdd = async () => {
    dictId.value = 0;
    editVisible.value = true;
    await nextTick();
    createModalRef.value.openModal();
  };

  /**
   * 执行编辑
   */
  const handleEdit = async () => {
    editVisible.value = true;
    await nextTick();
    createModalRef.value.openModal();
  };

  /**
   * 数据行选中事件
   * @param row 参数
   */
  function onCheckedRow(row) {
    dictId.value = row.id;
  }

  /**
   * 加载数据列表
   */
  const loadDataTable = async () => {
    let result = await getDictList({
      ...params.value,
      pageNo: pager.value.page,
      pageSize: pager.value.size,
    });
    dictId.value = result?.records[0]?.id;
    dictItemShow.value = true;
    dictDataList.value = result.records;
    pager.value.count = result.total;
  };

  /**
   * 执行删除
   */
  async function handleDelete() {
    dialog.warning({
      title: '提示',
      content: '确定要删除？',
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: async () => {
        dictDelete(dictId.value);
        message.success('删除成功');
        pager.value.page = 1;
        loadDataTable();
      },
    });
  }

  /**
   * 钩子函数
   */
  onMounted(() => {
    loadDataTable();
  });
</script>
<style lang="less" scoped>
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .dict-list-box {
    overflow: auto;

    .dict-item {
      height: 40px;
      line-height: 40px;
      padding: 0 6px;
      cursor: pointer;
      display: flex;
      justify-content: space-between;

      .t1 {
        font-size: 14px;
        display: block;
        font-weight: 700;

        .t2 {
          font-size: 12px;
          font-weight: normal;
        }
      }

      &.active {
        background-color: #e8f1ff;
        border-radius: 3px;

        .t1 {
          color: #1677ff;
        }

        .t2 {
          color: rgb(22, 119, 255, 0.8);
        }
      }

      .ant-badge__content.is-fixed {
        top: 20px;
        right: calc(-10px + var(--ant-badge-size) / 2);
      }
    }
  }
</style>
