<template>
  <PageWrapper>
    <BasicTable
      :columns="columns"
      :actionColumn="actionColumn"
      :request="loadDataTable"
      :row-key="(row) => row.id"
      ref="tableRef"
      @update:checked-row-keys="onSelectionChange"
    >
      <template #tableTitle>
        <n-space>
          <n-input
            type="text"
            v-model:value="params.name"
            placeholder="请输入配置项名称"
            clearable
          />
          <n-button type="primary" @click="reloadTable">
            <template #icon>
              <n-icon> <SearchOutlined /> </n-icon></template
            >查询
          </n-button>
          <n-button type="primary" @click="handleAdd">
            <template #icon>
              <n-icon> <PlusOutlined /> </n-icon></template
            >新建</n-button
          >
          <n-button type="error" :disabled="!selectionData.length" @click="handleDelete()">
            <template #icon>
              <n-icon> <DeleteOutlined /> </n-icon></template
            >删除</n-button
          >
        </n-space>
      </template>
    </BasicTable>
    <editDialog
      v-if="editVisible"
      ref="configItemRef"
      :configId="configId"
      :configItemId="configItemId"
      v-model:visible="editVisible"
      @success="reloadTable('noRefresh')"
    />
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { ref, reactive, h, nextTick, watch, defineAsyncComponent } from 'vue';
  import { PlusOutlined, FormOutlined, DeleteOutlined, SearchOutlined } from '@vicons/antd';
  import { getConfigItemList, configItemDelete, configItemBatchDelete } from '@/api/data/config';
  import { columns } from './columnsItem';
  import { useMessage, useDialog } from 'naive-ui';
  import { TableAction } from '@/components/Table';
  import editDialog from './editItem.vue';
  import { renderIcon } from '@/utils';

  /**
   * 定义参数变量
   */
  const tableRef = ref();
  const configItemRef = ref();
  const editVisible = ref(false);
  const selectionData = ref([]);
  const configItemId = ref(0);
  const message = useMessage();
  const dialog = useDialog();

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

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    configId: {
      type: Number,
      required: true,
      default: 0,
    },
  });

  /**
   * 侦听器
   */
  watch(
    () => props.configId,
    async (value) => {
      if (value) {
        await nextTick();
        console.log(value);
        reloadTable();
      }
    },
  );
  const actionColumn = reactive({
    width: 200,
    title: '操作',
    align: 'center',
    key: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '编辑',
            icon: renderIcon(FormOutlined),
            type: 'warning',
            onClick: handleEdit.bind(null, record),
          },
          {
            label: '删除',
            icon: renderIcon(DeleteOutlined),
            type: 'error',
            onClick: handleDelete.bind(null, record),
          },
        ],
        select: (key) => {
          message.info(`您点击了，${key} 按钮`);
        },
      });
    },
  });

  /**
   * 刷新配置项值列表
   * @param noRefresh 参数
   */
  function reloadTable(noRefresh = '') {
    tableRef.value.reload(noRefresh ? {} : { pageNo: 1 });
  }

  /**
   * 加载配置项值列表
   * @param res 参数
   */
  const loadDataTable = async (res) => {
    const result = await getConfigItemList({ ...params.value, configId: props.configId, ...res });
    return result;
  };

  /**
   * 新建配置
   */
  const handleAdd = async () => {
    configItemId.value = 0;
    editVisible.value = true;
    await nextTick();
    configItemRef.value.openModal();
  };

  /**
   * 编辑配置
   * @param id 参数
   */
  const handleEdit = async (row) => {
    configItemId.value = row.id;
    editVisible.value = true;
    await nextTick();
    configItemRef.value.openModal();
  };

  /**
   * 删除配置项
   * @param id 参数
   */
  async function handleDelete(row) {
    dialog.warning({
      title: '提示',
      content: '确定要删除？',
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: async () => {
        row ? await configItemDelete(row.id) : await configItemBatchDelete(selectionData.value);
        message.success('删除成功');
        reloadTable();
      },
    });
  }

  /**
   * 选项发生变化
   * @param value 参数
   */
  function onSelectionChange(value) {
    selectionData.value = value;
  }
</script>
<style lang="less" scoped>
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
</style>
