<template>
  <PageWrapper>
    <el-card :bordered="false" class="pt-3 mb-3 proCard">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
        <template #statusSlot="{ model, field }">
          <el-input v-model="model[field]" />
        </template>
      </BasicForm>
    </el-card>
    <el-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="basicTableRef"
        :actionColumn="actionColumn"
        @selection-change="onSelectionChange"
        scroll-x="1200"
        virtual-scroll
      >
        <template #tableTitle>
          <el-space>
            <el-button type="primary" @click="addTenant">
              <template #icon>
                <el-icon style="vertical-align: middle">
                  <plus />
                </el-icon>
              </template>
              新建
            </el-button>

            <el-button type="danger" @click="handleDelete()" :disabled="!selectionData.length">
              <template #icon>
                <el-icon class="el-input__icon">
                  <Delete />
                </el-icon>
              </template>
              删除
            </el-button>
          </el-space>
        </template>
      </BasicTable>
    </el-card>
    <editDialog
        v-if="editVisible"
        :tenantId="tenantId"
        v-model:visible="editVisible"
        @success="reloadTable"
    >
    </editDialog>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { h, nextTick, reactive, ref ,defineAsyncComponent} from 'vue';
  import { ColProps } from 'element-plus';
  import { TableAction } from '@/components/Table';
  import { useForm } from '@/components/Form/index';
  import { getTenantList,tenantDelete,tenantBatchDelete } from '@/api/system/tenant';
  import {message,confirm} from "@/utils/auth";
  import { columns } from './columns';
  import { schemas } from './querySchemas';
  const tenantId =  ref(0);
  const basicTableRef = ref();
  const editVisible = ref(false)
  const selectionData = ref([])
  const editDialog = defineAsyncComponent(() =>
    import('./edit.vue'))
  const formParams = reactive({
    name: '',
    status:'',
  });


  const actionColumn = reactive({
    width: 250,
    label: '操作',
    prop: 'action',
    fixed: 'right',
    align: 'center',
    render(record) {
      return h(TableAction as any, {
        style: 'text',
        actions: [
          {
            label: '删除',
            type: 'danger',
            onClick: handleDelete.bind(null, record),
          },
          {
            label: '编辑',
            type: 'warning',
            onClick: handleEdit.bind(null, record),
          },
        ],
        // dropDownActions: [
        //   {
        //     label: '启用',
        //     key: 'enabled',
        //   },
        //   {
        //     label: '禁用',
        //     key: 'disabled',
        //   },
        // ],
        // select: (key) => {
        //   ElMessage.info(`您点击了，${key} 按钮`);
        // },
      });
    },
  });

  const loadDataTable = async (res) => {
    const result = await getTenantList({ ...formParams, ...res });
    return result;
  };

  function reloadTable() {
    basicTableRef.value.reload({pageNo:1});
  }

  async function handleEdit(record: Recordable) {
    tenantId.value=record.row.id
    await nextTick();
    editVisible.value=true
  }

  async function handleDelete(record: Recordable) {
    let ids = []
    if(!record){
      ids = selectionData.value.map(({id}) => id);
    }
    await confirm('确定要删除？');
    record? await tenantDelete(record.row.id):await tenantBatchDelete(ids);
    message("删除成功");
    reloadTable()
  }
  
  function handleSubmit(values: Recordable) {
    for (const key in formParams) {
      if (values[key] != null && values[key] != undefined) {
        formParams[key] = values[key];
      }
    }
    reloadTable();
  }

  function handleReset(values: Recordable) {
    for (const key in formParams) {
        formParams[key] ='';
    }
  }
  function onSelectionChange(value){
    selectionData.value = value
  }
  const [register, {}] = useForm({
    labelWidth: 80,
    layout: 'horizontal',
    colProps: { span: 6 } as ColProps,
    submitOnReset:true,
    schemas
  });
  //添加
  function addTenant() {
    tenantId.value =0
    editVisible.value  = true
  }

</script>

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