<template>
  <PageWrapper dense>
    <Row>
      <!-- 左边部门选择 -->
      <Col v-bind="{ xs: 24, sm: 24, md: 24, lg: 4 }" class="h-[calc(100vh-80px)]">
        <DeptTree
          v-model:selectDeptId="selectDeptId"
          v-model:searchValue="searchValue"
          @select="reload"
        />
      </Col>
      <Col v-bind="{ xs: 24, sm: 24, md: 24, lg: 20 }">
        <BasicTable @register="registerTable">
          <template #toolbar>
            <!-- <a-button
              @click="
                downloadExcel(
                  buildingInfoExport,
                  '楼宇基础信息数据',
                  getForm().getFieldsValue()
                )
              "
              v-auth="'building:buildingInfo:export'"
              >导出</a-button
            > -->
            <a-button
              type="primary"
              danger
              @click="multipleRemove(buildingInfoRemove)"
              :disabled="!selected"
              v-auth="'building:buildingInfo:remove'"
              >删除</a-button
            >
            <!-- <a-button
              type="primary"
              @click="handleAdd"
              v-auth="'building:buildingInfo:add'"
              >新增</a-button
            > -->
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'action'">
              <TableAction
                stopButtonPropagation
                :actions="[
                  {
                    label: '删除',

                    type: 'primary',
                    danger: true,
                    ghost: true,
                    auth: 'building:buildingInfo:remove',
                    popConfirm: {
                      placement: 'left',
                      title: '是否删除楼宇基础信息[' + record.id + ']?',
                      confirm: handleDelete.bind(null, record),
                    },
                  },
                ]"
              />
            </template>
          </template>
        </BasicTable>
      </Col>
    </Row>

    <BuildingInfoModal @register="registerModal" @reload="reload" />
  </PageWrapper>
</template>

<script setup lang="ts">
import { PageWrapper } from "@/components/Page";
import { BasicTable, useTable, TableAction } from "@/components/Table";
import {
  buildingInfoList,
  buildingInfoExport,
  buildingInfoRemove,
} from "@/api/basicInfo/buildingsInfo";
import DeptTree from "@/components/commonDeptTree/DeptTree.vue";
import { downloadExcel } from "@/utils/file/download";
import { useModal } from "@/components/Modal";
import BuildingInfoModal from "./BuildingInfoModal.vue";
import { formSchemas, columns } from "./buildingInfo.data";
import { IconEnum } from "@/enums/appEnum";
import { ref, toValue } from "vue";
import { Row, Col } from "ant-design-vue";

defineOptions({ name: "BuildingInfo" });
// 左边部门用
const selectDeptId = ref<string[]>([]);
const searchValue = ref<string>("");
const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
  rowSelection: {
    type: "checkbox",
  },
  title: "用户签到列表",
  api: buildingInfoList,
  showIndexColumn: false,
  rowKey: "id",
  useSearchForm: true,
  formConfig: {
    schemas: formSchemas,
    baseColProps: {
      xs: 24,
      sm: 24,
      md: 24,
      lg: 6,
    },
    resetFunc: async () => {
      selectDeptId.value = [];
      searchValue.value = "";
      await reload();
    },
  },
  // 需要添加上部门参数
  beforeFetch(params: Recordable) {
    if (toValue(selectDeptId).length === 1) {
      params.deptId = toValue(selectDeptId)[0];
    }
    return params;
  },
  columns: columns,
  actionColumn: {
    width: 200,
    title: "操作",
    key: "action",
    fixed: "right",
  },
});

const [registerModal, { openModal }] = useModal();

function handleEdit(record: Recordable) {
  openModal(true, { record, update: 1 });
}

function handleAdd() {
  openModal(true, { update: 2 });
}
function handCheck(record: Recordable) {
  openModal(true, { record, update: 3 });
}
async function handleDelete(record: Recordable) {
  await buildingInfoRemove([record.id]);
  await reload();
}
function handleCancle() {
  closeModal();
  resetForm();
}
</script>

<style scoped></style>
