<template>
  <PageWrapper dense>
    <Row>
      <!-- 左边部门选择 -->
      <Col v-bind="{ xs: 24, sm: 24, md: 24, lg: 4 }" class="h-[calc(100vh-80px)]">
        <FaultMessageDeptTree
          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(
                  faultMessageExport,
                  '故障消息;数据',
                  getForm().getFieldsValue()
                )
              "
              v-auth="'fault:faultMessage:export'"
              >导出</a-button
            >
     
            <!-- <a-button type="primary" @click="handleAdd" v-auth="'fault:faultMessage:add'"
              >新增</a-button
            > -->
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'picture'">
              <div>
                <Image :width="100" :height="50" :src="record.picture"></Image>
              </div>
            </template>
            <template v-if="column.key === 'action'">
              <TableAction
                stopButtonPropagation
                :actions="[
                  {
                    label: '查看',
                    icon: IconEnum.PREVIEW,
                    type: 'primary',
                    ghost: true,
                    auth: 'behavior:behaviorMessage:query',
                    onClick: handCheck.bind(null, record),
                  },
             
                ]"
                :dropDownActions="[
                  {
                    label: '查看位置',
                    icon: 'mingcute:department-line',
                    auth: '',

                    onClick: handleOpenAuth.bind(null, record),
                  },
                  {
                    label: '故障图片',
                    icon: 'solar:user-outline',
                    auth: 'system:role:edit',
                    onClick: handleAssignRole.bind(null, record.picture),
                  },
                  {
                    label: '故障波形',
                    icon: 'solar:user-outline',
                    auth: 'system:role:edit',
                    disabled: true,
                  },
                ]"
              />
            </template>
          </template>
        </BasicTable>
      </Col>
    </Row>

    <FaultMessageModal @register="registerModal" @reload="reload" />
    <PreviewMap @register="registerMapModal" @reload="reload"></PreviewMap>

    <Modal width="800px" title="查看故障图片" v-model:open="showPictureModal">
      <template #footer>
        <div>
          <a-button type="primary">人体关键点分析</a-button>
        </div>
      </template>
      <Image :src="badPicture"></Image>
    </Modal>
  </PageWrapper>
</template>

<script setup lang="ts">
import { Row, Col, Image, Modal } from "ant-design-vue";
import { PageWrapper } from "@/components/Page";
import { BasicTable, useTable, TableAction } from "@/components/Table";
import PreviewMap from "./PreviewMap.vue";
import {
  faultMessageList,
  faultMessageExport,
  faultMessageRemove,
} from "@/api/statusCheck/fault";
import { downloadExcel } from "@/utils/file/download";
import { useModal } from "@/components/Modal";
import FaultMessageModal from "./FaultMessageModal.vue";
import { formSchemas, columns } from "./faultMessage.data";
import { IconEnum } from "@/enums/appEnum";
import FaultMessageDeptTree from "./FaultMessageDeptTree.vue";
import { ref, toValue } from "vue";
defineOptions({ name: "FaultMessage" });
// 左边部门用
const selectDeptId = ref<string[]>([]);
const searchValue = ref<string>("");
const showModal = ref<boolean>(false);
const showPictureModal = ref<boolean>(false);
const PreviewMapRefs = ref<any>();
const badPicture = ref<string>("");
const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
  rowSelection: {
    type: "checkbox",
  },
  title: "故障消息;列表",
  api: faultMessageList,
  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];
    }
    return params;
  },
  actionColumn: {
    width: 250,
    title: "操作",
    key: "action",
    fixed: "right",
  },
});

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

function handleEdit(record: Recordable) {
  openModal(true, { record, update: 1 });
}
function handleOpenAuth(record: Recordable) {
  if (!record.lng || !record.lat) {
    message.error("未查询到经纬度信息");
    return;
  }
  openMapModal(true, { record, update: 1 });
  console.log(PreviewMapRefs.value);
}
function handleAssignRole(record: Recordable) {
  console.log(record);
  badPicture.value = record;
  showPictureModal.value = true;
}
function handleAdd() {
  openModal(true, { update: 2 });
}
function handCheck(record: Recordable) {
  openModal(true, { record, update: 3 });
}
async function handleDelete(record: Recordable) {
  await faultMessageRemove([record.id]);
  await reload();
}
</script>

<style scoped></style>
