<template>
  <page-wrapper>
    <el-card :bordered="false" class="mb-3 proCard">
      <el-space align="center">
        <el-input
          :style="{ width: '320px' }"
          v-model="params.taskName"
          clearable
          placeholder="请输入定时任务名称"
          @keyup.enter="reloadTable"
        />
        <el-button type="primary" @click="reloadTable">
          <template #icon>
            <el-icon>
              <SearchOutlined />
            </el-icon>
          </template>
          查询
        </el-button>
      </el-space>
    </el-card>
    <el-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="tableRef"
        :actionColumn="actionColumn"
        @update:checked-row-keys="onCheckedRow"
      >
        <template #tableTitle>
          <el-button type="primary" @click="openCreateDrawer">
            <template #icon>
              <el-icon>
                <FileAddOutlined />
              </el-icon>
            </template>
            添加配置
          </el-button>
        </template>

        <template #action>
          <TableAction />
        </template>
      </BasicTable>
    </el-card>

    <CreateDrawer
      ref="createDrawerRef"
      :title="drawerTitle"
      :permissionList="treeData"
      @change="reloadTable"
    />
  </page-wrapper>
</template>

<script lang="ts" setup>
  import { reactive, ref, unref, h, onMounted } from 'vue';
  import { ElMessage } from 'element-plus';
  import { BasicTable, TableAction, BasicColumn } from '@/components/Table';
  import { tasksList, deleteTasks } from '@/api/system/tasks';
  import { columns } from './columns';
  import { FileAddOutlined, SearchOutlined } from '@vicons/antd';
  import CreateDrawer from './CreateDrawer.vue';

  const message = ElMessage;
  const tableRef = ref();
  const createDrawerRef = ref();
  const drawerTitle = ref('添加定时任务');
  const treeData = ref([]);

  const params = reactive({
    taskName: '',
  });

  const actionColumn: BasicColumn = reactive({
    width: 150,
    title: '操作',
    prop: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '删除',
            isConfirm: true,
            popConfirm: {
              onConfirm: handleDelete.bind(null, record.row),
              title: '您确定要删除吗？',
              confirmButtonText: '确定',
              cancelButtonText: '取消',
            },
          },
          {
            label: '编辑',
            onClick: handleEdit.bind(null, record.row),
          },
        ],
      });
    },
  });

  const loadDataTable = async (res: any) => {
    let _params = {
      ...unref(params),
      ...res,
    };
    return await tasksList(_params);
  };

  function openCreateDrawer() {
    const { openDrawer } = createDrawerRef.value;
    openDrawer();
  }

  function onCheckedRow(rowKeys: any[]) {
    console.log(rowKeys);
  }

  function reloadTable() {
    tableRef.value.reload();
  }

  function handleEdit(record: Recordable) {
    console.log('点击了编辑', record);
    drawerTitle.value = '编辑任务';
    const { openDrawer } = createDrawerRef.value;
    openDrawer(record.jobId);
  }

  function handleDelete(record: Recordable) {
    deleteTasks({ jobId: record.jobId }).then(() => {
      message.success('删除成功');
      reloadTable();
    });
  }

  onMounted(async () => {});
</script>
