<template>
  <PageWrapper>
    <a-row :gutter="10" class="mt-3">
      <a-col :xs="24" :sm="24" :md="8" :lg="8" :xl="8">
        <a-card shadow="hover" class="border-0">
            <a-space>
                <a-input
                  type="text"
                  v-model:value="params.name"
                  placeholder="请输入配置名称"
                  allow-clear
                />
                <a-button
                  type="primary"
                  @click="reloadTable"
                >
                  <template #icon> <SearchOutlined /> </template>查询
                </a-button>
            </a-space>
            <div style="margin-top: 15px">
              <a-space>
                <a-button type="primary" @click="handleAdd" v-perm="['sys:config:add']">
                  <template #icon>
                    <PlusOutlined />
                  </template>
                  新建
                </a-button>
                <a-button type="danger" @click="handleDelete()" v-perm="['sys:config:delete']" :disabled="!selectionData.length">
                  <template #icon> <DeleteOutlined /> </template>删除
                </a-button>
              </a-space>
            </div>
          <BasicTable
          :columns="columns"
          :request="loadDataTable"
          :row-key="(row) => row.id"
          :showTableSetting="false" 
          ref="tableRef"
          :row-selection="{ onChange: onSelectionChange }"
          :customRow="rowClick"
          :row-class-name="setRowClassName"
          :pagination="{showQuickJumper:false,showSizeChanger:false}"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'action'">
              <a-space>
                <a-button type="primary" @click="handleEdit(record.id)" v-perm="['sys:dict:update']">
                  <template #icon><EditOutlined /></template>
                  编辑
                </a-button>
                <a-button type="primary" danger @click="handleDelete(record.id)" v-perm="['sys:dict:delete']">
                  <template #icon><DeleteOutlined /></template>
                  删除
                </a-button>
              </a-space>
            </template>
          </template>
        </BasicTable>
        </a-card>
      </a-col>
      <a-col :xs="24" :sm="24" :md="16" :lg="16" :xl="16">
        <a-card shadow="hover" class="mb-4 border-0 proCard">
          <configItem :configId="configId" v-if="configItemShow" />
        </a-card>
      </a-col>
    </a-row>
    <editDialog
      v-if="editVisible"
      :configId="configId"
      v-model:visible="editVisible"
      @success="reloadTable('noRefresh')"
    />
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { ref, nextTick, defineAsyncComponent, onMounted } from 'vue';
  import { SearchOutlined } from '@ant-design/icons-vue';
  import { getConfigList, configDelete } from '@/api/data/config';
  import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons-vue';
  import configItem from './configItem.vue';
  import { columns } from './columns';
  import { Modal, message } from 'ant-design-vue';

  /**
   * 导入组件
   */
  const editDialog = defineAsyncComponent(() => import('./edit.vue'));

  /**
   * 定义参数变量
   */
  const configId = ref(0);
  const configItemShow = ref(false);
  const tableRef = ref();
  const editVisible = ref(false);
  const selectionData = ref([]);

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


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

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

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

    /**
   * 刷新字典项值列表
   */
   function reloadTable(noRefresh = '') {
    tableRef.value.reload(noRefresh ? {} : { pageNo: 1 });
  }
  /**
   * 加载数据列表
   */
  const loadDataTable = async (res) => {
    const result = await getConfigList({ ...params.value, ...res });
    configId.value = result?.records[0]?.id
    configItemShow.value = true
    return result;
  };

  /**
   * 执行删除
   */
  async function handleDelete(id) {
    Modal.confirm({
      title: '提示',
      content: '确定要删除？',
      onOk: async () => {
        id ? await configDelete(id) : await configBatchDelete(selectionData.value);
        message.success('删除成功');
        reloadTable();
      },
    });
  }
    /**
   * 选项发生变化
   * @param value 参数
   */
   function onSelectionChange(value) {
    selectionData.value = value;
  }
    /**
   * 数据行点击事件
   * @param record 参数
   */
   const rowClick = (record) => {
    return {
      onClick: () => {
        configId.value = record.id;
      },
    };
  };
    /**
   * 设置选项行类名
   * @param record 参数
   */
   const setRowClassName = (record) => {
    return record.id === configId.value ? 'clickRowStyle' : '';
  };

  /**
   * 钩子函数
   */
  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>
<style lang="less">
  .ant-table-tbody {
    .ant-table-row.clickRowStyle,
    .ant-table-cell-row.clickRowStyle {
      td {
        background-color: #e7eeff !important;
      }
      &:hover {
        td {
          background-color: #e7eeff !important;
        }
      }
    }
  }
</style>