<template>
  <PageWrapper dense>
    <Row>
      <!-- 左边部门选择 -->
      <Col v-bind="{ xs: 24, sm: 24, md: 24, lg: 4 }" class="h-[calc(100vh-80px)]">
        <ElevatorInsuranceDeptTree
          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(
                  elevatorInsuranceExport,
                  '电梯和保险信息数据',
                  getForm().getFieldsValue(),
                )
              "
              v-auth="'elevator:elevatorInsurance:export'"
              >导出</a-button
            >
            <a-button
              type="primary"
              danger
              @click="multipleRemove(elevatorInsuranceRemove)"
              :disabled="!selected"
              v-auth="'elevator:elevatorInsurance:remove'"
              >删除</a-button
            >
            <a-button type="primary" @click="handleAdd" v-auth="'elevator:elevatorInsurance:add'"
              >新增</a-button
            >
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'elevatorNum'">
              <span
                @click="handleClickNum(record.elevatorNum)"
                style="color: blue; cursor: pointer; text-decoration: underline"
                >{{ record.elevatorNum }}</span
              >
            </template>
            <template v-if="column.key === 'action'">
              <TableAction
                stopButtonPropagation
                :actions="[
                  {
                    label: '查看',

                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:elevatorInsurance:query',
                    onClick: handCheck.bind(null, record),
                  },
                  {
                    label: '修改',

                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:elevatorInsurance:edit',
                    onClick: handleEdit.bind(null, record),
                  },
                  {
                    label: '下载',

                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:elevatorInsurance:edit',
                    onClick: handleDown.bind(null, record),
                  },
                  {
                    label: '删除',
                    type: 'primary',
                    danger: true,
                    ghost: true,
                    auth: 'elevator:elevatorInsurance:remove',
                    popConfirm: {
                      placement: 'left',
                      title: '是否删除电梯和保险信息[' + record.id + ']?',
                      confirm: handleDelete.bind(null, record),
                    },
                  },
                ]"
              />
            </template>
          </template>
        </BasicTable>
      </Col>
    </Row>

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

<script setup lang="ts">
import { Row, Col } from 'ant-design-vue';
import { PageWrapper } from '@/components/Page';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import {
  elevatorInsuranceList,
  elevatorInsuranceExport,
  elevatorInsuranceRemove,
} from '@/api/workPlace/elevatorInsurance';
import { downloadExcel } from '@/utils/file/download';
import { useModal } from '@/components/Modal';
import ElevatorInsuranceModal from './ElevatorInsuranceModal.vue';
import { formSchemas, columns } from './elevatorInsurance.data';
import { IconEnum } from '@/enums/appEnum';
import ElevatorInsuranceDeptTree from './ElevatorInsuranceDeptTree.vue';
import { ref, toValue } from 'vue';
defineOptions({ name: 'ElevatorInsurance' });
// 左边部门用
const selectDeptId = ref<string[]>([]);
const searchValue = ref<string>('');
const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
  rowSelection: {
    type: 'checkbox',
  },
  title: '电梯和保险信息列表',
  api: elevatorInsuranceList,
  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();
    },
  },
  columns: columns,
  // 需要添加上部门参数
  beforeFetch(params: Recordable) {
    if (toValue(selectDeptId).length === 1) {
      params.deptId = toValue(selectDeptId)[0];
    }
    return params;
  },
  actionColumn: {
    width: 250,
    title: '操作',
    key: 'action',
    fixed: 'right',
  },
});

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

function handleEdit(record: Recordable) {
  openModal(true, { record, update: 1 });
}
function handleClickNum(num) {
  window.open(`/workPlace/workInfoMaintenance?elevatorNum=${num}`);
}
function handleAdd() {
  openModal(true, { update: 2 });
}
function handCheck(record: Recordable) {
  openModal(true, { record, update: 3 });
}
function handleDown(record: Recordable) {
  // 创建一个a标签用于下载
  const a = document.createElement('a');
    a.href = record.contract;
    a.target = '_blank'; // 在新窗口中打开
    a.download = 'yourfile.pdf'; // 设置下载文件名
    document.body.appendChild(a);
    a.click(); // 触发下载


    document.body.removeChild(a);

}
async function handleDelete(record: Recordable) {
  await elevatorInsuranceRemove([record.id]);
  await reload();
}
</script>

<style scoped></style>
