<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection" @selection-change="handleSelectionChange" size="small">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button v-if="selectedRowKeys.length > 0" preIcon="ant-design:flag-outlined" @click="handleSendOrg" >选择发往机构</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)"/>
      </template>
    </BasicTable>
    <SendOrgDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="iot-weihu" setup>
  import { reactive, ref } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { list } from './weihu.api';
  import { columns, searchFormSchema } from './weihu.data';
  import { useDrawer } from "@/components/Drawer";
  import SendOrgDrawer from './components/SendOrgDrawer.vue';

  const [registerDrawer, { openDrawer }] = useDrawer();
  const queryParam = reactive<any>({});
  const selectedRows = ref<any[]>([]);

  //注册table数据
  const { prefixCls,tableContext } = useListPage({
    tableProps:{
      title: '设备维修',
      api: list,
      columns,
      canResize:false,
      showIndexColumn: true,
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 120,
        fixed:'right'
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
  })

  const [registerTable, {reload},{ rowSelection, selectedRowKeys }] = tableContext

  function handleSelectionChange(selectedRowSet) {
    selectedRows.value = selectedRowSet.rows;
    console.log('当前选中数据:', selectedRows.value);
  }

  function handleSendOrg(){
    openDrawer(true, {
      records : selectedRows.value,
      isUpdate: true,
      showFooter: true,
      tenantSaas: false,
    });
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  function handleDetail(record){
    let records = [];
    records.push(record);
    openDrawer(true, {
      records : records,
      isUpdate: true,
      showFooter: false,
      tenantSaas: false,
    });
  }

  /**
   * 操作栏
   */
  function getTableAction(record){
    const actions = [];
    if(record.reserveDepartId){
      actions.push({
        label: '详情',
        onClick: () => {
          handleDetail(record);
        }
      });
    }
    return actions;
  }

</script>

<style lang="less" scoped>
:deep(.selected-row) {
  background-color: #e6f7ff !important;

  &:hover td {
    background-color: #e6f7ff !important;
  }
}
</style>
