<template>
  <PageWrapper>
    <el-card :bordered="false" class="mb-3 proCard">
      <el-space align="center">
        <el-input
          :style="{ width: '320px' }"
          v-model="params.postName"
          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.postId"
        ref="basicTableRef"
        :actionColumn="actionColumn"
        @update:checked-row-keys="onCheckedRow"
        virtual-scroll
      >
        <template #tableTitle>
          <el-space align="center">
            <el-button type="primary" @click="openCreateDrawer">
              <template #icon>
                <el-icon>
                  <FileAddOutlined />
                </el-icon>
              </template>
              添加
            </el-button>

            <!-- <el-button type="error" @click="openRemoveModal" :disabled="!rowKeys.length">
              <template #icon>
                <el-icon>
                  <DeleteOutlined />
                </el-icon>
              </template>
              删除
            </el-button> -->
          </el-space>
        </template>
      </BasicTable>
    </el-card>

    <CreateDrawer
      ref="createDrawerRef"
      :title="drawerTitle"
      :roleList="roleData"
      @change="reloadTable"
    />
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { ElMessage } from 'element-plus';
  import { BasicTable, TableAction, BasicColumn } from '@/components/Table';
  import { postList, deletePost } from '@/api/auth/post';
  import { FileAddOutlined, SearchOutlined } from '@vicons/antd';
  import CreateDrawer from './CreateDrawer.vue';
  import { columns } from './columns';

  const message = ElMessage;
  const basicTableRef = ref();
  const rowKeys = ref([]);
  const rowKeysName = ref([]);
  const tableData = ref();
  const createDrawerRef = ref();
  const drawerTitle = ref('添加岗位');
  const roleData = ref([]);

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

  const actionColumn: BasicColumn = reactive({
    width: 150,
    title: '操作',
    prop: 'action',
    fixed: 'right',
    align: 'center',
    render(record) {
      return h(TableAction as any, {
        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) => {
    const result = await postList({ ...params, ...res });
    tableData.value = result.list;
    return result;
  };

  function onCheckedRow(keys) {
    rowKeys.value = keys;
    rowKeysName.value = tableData.value
      .filter((item) => {
        return keys.includes(item.id);
      })
      .map((item) => {
        return item.username;
      })
      .join('，');
  }

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

  function openCreateDrawer() {
    drawerTitle.value = '添加岗位';
    const { openDrawer } = createDrawerRef.value;
    openDrawer();
  }

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

  function handleDelete(record: Recordable) {
    deletePost({ id: record.postId }).then(() => {
      message.success('删除成功');
      reloadTable();
    });
  }
</script>
