<template>
  <PageWrapper dense>
    <Row>
      <!-- 左边部门选择 -->
      <Col v-bind="{ xs: 24, sm: 24, md: 24, lg: 4 }" class="h-[calc(100vh-80px)]">
        <WorkerInfoDeptTree
          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(
                  workerInfoExport,
                  '工作人员管理;数据',
                  getForm().getFieldsValue()
                )
              "
              v-auth="'elevator:workerInfo:export'"
              >导出</a-button
            >
            <a-button @click="handleImport" v-auth="'system:user:import'">导入</a-button>

            <a-button
              type="primary"
              danger
              @click="multipleRemove(workerInfoRemove)"
              :disabled="!selected"
              v-auth="'elevator:workerInfo:remove'"
              >删除工作人员</a-button
            >
            <a-button type="primary" @click="handleAdd" v-auth="'elevator:workerInfo:add'"
              >新增</a-button
            >
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'photo'">
              <avatar v-if="record.photo" :src="record.photo" />
              <avatar
                v-else
                src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"
              />
            </template>
            <template v-if="column.key === 'action'">
              <TableAction
                stopButtonPropagation
                :actions="[
                  {
                    label: '查看',
                    icon: IconEnum.PREVIEW,
                    type: 'primary',
                    ghost: true,
                    auth: 'worker:workerInfo:query',
                    onClick: handCheck.bind(null, record),
                  },
                  {
                    label: '修改',
                    icon: IconEnum.EDIT,
                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:workerInfo:edit',
                    onClick: handleEdit.bind(null, record),
                  },
                  {
                    label: '删除',
                    icon: IconEnum.DELETE,
                    type: 'primary',
                    danger: true,
                    ghost: true,
                    auth: 'elevator:workerInfo:remove',
                    popConfirm: {
                      placement: 'left',
                      title: '是否删除工作人员管理;[' + record.id + ']?',
                      confirm: handleDelete.bind(null, record),
                    },
                  },
                ]"
                :dropDownActions="[
                  {
                    label: '关闭视频功能',
                    icon: 'mingcute:department-line',
                    auth: '',

                    onClick: handleCloseVideo.bind(null, record),
                  },
                  // {
                  //   label: '批量关注电梯',
                  //   icon: 'mingcute:department-line',
                  //   auth: '',

                  //   onClick: handleOBatchEva.bind(null, record),
                  // },
                ]"
              />
            </template>
          </template>
        </BasicTable>
      </Col>
    </Row>

    <WorkerInfoModal @register="registerModal" @reload="reload" />
    <UserImportModal @register="registerImportModal" @reload="reload" />
    <batchLoveEvaModal @register="registerBatchModal" />
  </PageWrapper>
</template>

<script setup lang="ts">
import { Row, Col, Modal ,Avatar} from "ant-design-vue";
import { PageWrapper } from "@/components/Page";
import { BasicTable, useTable, TableAction } from "@/components/Table";
import {
  workerInfoList,
  workerInfoExport,
  workerInfoRemove,
} from "@/api/workPlace/workerInfoManage";
import { downloadExcel } from "@/utils/file/download";
import { useModal } from "@/components/Modal";
import WorkerInfoModal from "./WorkerInfoModal.vue";
import { formSchemas, columns } from "./workerInfo.data";
import { IconEnum } from "@/enums/appEnum";
import WorkerInfoDeptTree from "./WorkerInfoDeptTree.vue";
import { ref, toValue } from "vue";
import UserImportModal from "@/views/analyseData/evaBadData/UserImportModal.vue";
import batchLoveEvaModal from "./batchLoveEvaModal.vue";
defineOptions({ name: "WorkerInfo" });
// 左边部门用
const selectDeptId = ref<string[]>([]);
const searchValue = ref<string>("");
const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
  rowSelection: {
    type: "checkbox",
  },
  title: "工作人员管理;列表",
  api: workerInfoList,
  showIndexColumn: true,
  rowKey: "id",
  useSearchForm: true,
  formConfig: {
    schemas: formSchemas,
    labelWidth: 140,
    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();
const [registerImportModal, { openModal: openImportModal }] = useModal();
const [registerBatchModal, { openModal: openIBatchModal }] = useModal();

function handleEdit(record: Recordable) {
  openModal(true, { record, update: 1 });
}
function handleImport() {
  openImportModal(true);
}
function handleCloseVideo() {
  Modal.confirm({
    title: `您确定要进行该操作？`,
    content: "",
    okText: "确定",
    okType: "danger",
    cancelText: "取消",
    onOk() {
      console.log("OK");
    },
    onCancel() {
      console.log("Cancel");
    },
  });
}
function handleOBatchEva(record: Recordable) {
  openIBatchModal(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 workerInfoRemove([record.id]);
  await reload();
}
</script>

<style scoped></style>
