<template>
  <page-wrapper>
    <el-card :bordered="false" class="mb-3 proCard">
      <el-space align="center">
        <el-input
          :style="{ width: '320px' }"
          v-model="params.tenantName"
          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"
        :dataSource="tenantItems"
        :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 { tenantList, deleteTenant } from '@/api/tenant/index';
  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 tenantItems = ref<any[]>([]);

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

  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,
    };
    const list = await tenantList(_params);
    tenantItems.value = list;
  };

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

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

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

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

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

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

<style lang="scss" scoped></style>
