<template>
  <PageWrapper>
    <BasicTable
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row) => row.id"
      ref="tableRef"
      :actionColumn="actionColumn"
      @selection-change="onSelectionChange"
    >
      <template #tableTitle>
        <el-space>
          <el-input type="text" v-model="params.name" placeholder="请输入配置项名称">
            <template #prefix>
              <el-icon class="el-input__icon">
                <SearchOutlined />
              </el-icon>
            </template>
          </el-input>
          <el-button type="primary" icon="Search" @click="reloadTable"> 查询 </el-button>
          <el-button type="primary" icon="Plus" @click="handleAdd">新建</el-button>
          <el-button
            type="danger"
            icon="Delete"
            :disabled="!selectionData.length"
            @click="handleDelete()"
            >删除</el-button
          >
        </el-space>
      </template>
    </BasicTable>
    <editDialog
      v-if="editVisible"
      :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 { SearchOutlined } from '@vicons/antd';
  import { TableAction } from '@/components/Table';
  import { getConfigItemList, configItemDelete, configItemBatchDelete } from '@/api/data/config';
  import { columns } from './columnsItem';
  import { message, confirm } from '@/utils/auth';
  const editDialog = defineAsyncComponent(() => import('./editItem.vue'));
  const tableRef = ref();
  const editVisible = ref(false);
  const selectionData = ref([]);
  const configItemId = ref(0);

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

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

  /**
   * 定义侦听器
   */
  watch(
    () => props.configId,
    async (value) => {
      if (value) {
        await nextTick();
        reloadTable();
      }
    },
  );

  /**
   * 定义操作栏
   */
  const actionColumn = reactive({
    width: 200,
    lable: '操作',
    prop: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'text',
        actions: [
          {
            label: '编辑',
            icon: 'Edit',
            type: 'warning',
            onClick: handleEdit.bind(null, record),
          },
          {
            label: '删除',
            icon: 'Delete',
            type: 'danger',
            onClick: handleDelete.bind(null, record),
          },
        ],
      });
    },
  });

  /**
   * 刷新配置项值列表
   * @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;
    await nextTick();
    editVisible.value = true;
  };

  /**
   * 执行编辑
   * @param record 参数
   */
  const handleEdit = async (record: Recordable) => {
    configItemId.value = record.row.id;
    await nextTick();
    editVisible.value = true;
  };

  /**
   * 执行删除
   * @param record 参数
   */
  async function handleDelete(record: Recordable) {
    let ids = [];
    if (!record) {
      ids = selectionData.value.map(({ id }) => id);
    }
    await confirm('确定要删除？');
    record ? await configItemDelete(record.row.id) : await configItemBatchDelete(ids);
    message('删除成功');
    reloadTable();
  }

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