<template>
  <PageWrapper dense>
    <Row>
      <!-- 左边部门选择 -->
      <Col v-bind="{ xs: 24, sm: 24, md: 24, lg: 4 }" class="h-[calc(100vh-80px)]">
        <WorkDataDeptTree
          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
              type="primary"
              danger
              @click="multipleRemove(workDataRemove)"
              :disabled="!selected"
              v-auth="'elevator:workData:remove'"
              >误报</a-button
            >
            <!-- <a-button type="primary" primary :disabled="!selected" v-auth="'work:workData:remove'"
              >误报</a-button
            > -->
            <!-- <a-button type="primary" @click="handleAdd" v-auth="'work:workData:add'">派发</a-button> -->
            <!-- <a-button type="primary" @click="handleUpdateWork" v-auth="'work:workData:add'"
              >维保工作上报</a-button
            > -->
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'action'">
              <TableAction
                stopButtonPropagation
                :actions="[
                  {
                    label: '查看',
                    icon: IconEnum.PREVIEW,
                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:workData:query',
                    onClick: handCheck.bind(null, record),
                  },

                  {
                    label: '删除',
                    icon: IconEnum.DELETE,
                    type: 'primary',
                    danger: true,
                    ghost: true,
                    auth: 'elevator:workData:remove',
                    popConfirm: {
                      placement: 'left',
                      title: '是否删除工作数据信息;[' + record.id + ']?',
                      confirm: handleDelete.bind(null, record),
                    },
                  },
                ]"
              />
            </template>
          </template>
        </BasicTable>
      </Col>
    </Row>

    <WorkDataModal @register="registerModal" @reload="reload" />
    <givenWorkModal @register="registerGivenModal" @reload="reload" />
  </PageWrapper>
</template>

<script setup lang="ts">
import { Row, Col, Modal } from 'ant-design-vue';
import { PageWrapper } from '@/components/Page';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import { workDataList, workDataExport, workDataRemove } from '@/api/workPlace/workInfo';
import { downloadExcel } from '@/utils/file/download';
import { useModal } from '@/components/Modal';
import WorkDataModal from './WorkDataModal.vue';
import { formSchemas, columns } from './workData.data';
import { IconEnum } from '@/enums/appEnum';
import WorkDataDeptTree from './WorkDataDeptTree.vue';
import givenWorkModal from './givenWorkModal.vue';
import { ref, toValue } from 'vue';
defineOptions({ name: 'WorkData' });
// 左边部门用
const selectDeptId = ref<string[]>([]);
const searchValue = ref<string>('');
const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
  rowSelection: {
    type: 'checkbox',
  },
  title: '工作数据信息列表',
  api: workDataList,
  showIndexColumn: true,
  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];
      params.workType = 2;
    }
    return params;
  },
  actionColumn: {
    width: 250,
    title: '操作',
    key: 'action',
    fixed: 'right',
  },
});

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

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

function handleAdd() {
  openGivenModal(true, { update: 2 });
}
function handleUpdateWork() {
  Modal.confirm({
    title: `您确定要进行该操作？`,
    content: '',
    okText: '确定',
    okType: 'danger',
    cancelText: '取消',
    onOk() {
      console.log('OK');
    },
    onCancel() {
      console.log('Cancel');
    },
  });
}
function handCheck(record: Recordable) {
  openModal(true, { record, update: 3 });
}
async function handleDelete(record: Recordable) {
  await workDataRemove([record.id]);
  await reload();
}
</script>

<style scoped></style>
