<template>
  <PageWrapper dense>
    <Row>
      <!-- 左边部门选择 -->
      <Col v-bind="{ xs: 24, sm: 24, md: 24, lg: 4 }" class="h-[calc(100vh-80px)]">
        <BehaviorMessageDeptTree
          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(
                  behaviorMessageExport,
                  '行为消息;数据',
                  getForm().getFieldsValue()
                )
              "
              v-auth="'elevator:behaviorMessage:export'"
              >导出</a-button
            >
            <!-- <a-button
              type="primary"
              danger
              @click="multipleRemove(behaviorMessageRemove)"
              :disabled="!selected"
              v-auth="'behavior:behaviorMessage:remove'"
              >删除</a-button
            > -->
            <!-- <a-button
              type="primary"
              @click="handleAdd"
              v-auth="'behavior:behaviorMessage:add'"
              >新增</a-button
            > -->
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'picture'">
              <div>
                <Image :width="100" :height="50" :src="record.picture"></Image>
              </div>
            </template>
            <template v-if="column.dataIndex === 'state'">
              <div>
                {{ record.state === 1 ? "已确认" : "未确认" }}
              </div>
            </template>
            <template v-if="column.key === 'action'">
              <TableAction
                stopButtonPropagation
                :actions="[
                  {
                    label: '确认',
                    disabled: record.state === 1,
                    type: 'primary',
                    ghost: false,
                    auth: 'elevator:behaviorMessage:confirm',
                    onClick: handConfirm.bind(null, record),
                  },
                  {
                    label: '查看',

                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:behaviorMessage:query',
                    onClick: handCheck.bind(null, record),
                  },
                ]"
                :dropDownActions="[
                  {
                    label: '查看位置',
                    icon: 'elevator:behaviorMessage:position',
                    auth: '',

                    onClick: handleOpenAuth.bind(null, record),
                  },
                  {
                    label: '故障图片',
                    icon: 'solar:user-outline',
                    auth: 'elevator:behaviorMessage:picture',

                    onClick: handleAssignRole.bind(null, record),
                  },
                  {
                    label: '故障波形',
                    icon: 'solar:user-outline',
                    auth: 'elevator:behaviorMessage:boShape',
                    disabled: true,
                  },
                ]"
              />
            </template>
          </template>
        </BasicTable>
      </Col>
    </Row>
    <confirmModal @register="registerConfirmModal" @reload="reload"></confirmModal>
    <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>
    <BehaviorMessageModal @register="registerModal" @reload="reload" />
    <FaultImage @register="registerImageModal" @reload="reload"></FaultImage>
  </PageWrapper>
</template>

<script setup lang="ts">
import { Row, Col, message, Image } from "ant-design-vue";
import { PageWrapper } from "@/components/Page";
import { BasicTable, useTable, TableAction } from "@/components/Table";
import {
  behaviorMessageList,
  behaviorMessageExport,
  behaviorMessageRemove,
} from "@/api/statusCheck/behavior";
import { downloadExcel } from "@/utils/file/download";
import { useModal } from "@/components/Modal";
import BehaviorMessageModal from "./BehaviorMessageModal.vue";
import { formSchemas, columns } from "./behaviorMessage.data";
import { IconEnum } from "@/enums/appEnum";
import BehaviorMessageDeptTree from "./BehaviorMessageDeptTree.vue";
import { ref, toValue } from "vue";
import confirmModal from "./confirmModal.vue";
import PreviewMap from "./PreviewMap.vue";
import { faultMessageList } from "@/api/statusCheck/fault";
import FaultImage from "./faultImage.vue";
defineOptions({ name: "BehaviorMessage" });
// 左边部门用
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<any>("");
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];
    }
    params.faultTypeKey = "fault_exception_type";
    return params;
  },
  actionColumn: {
    width: 250,
    title: "操作",
    key: "action",
    fixed: "right",
  },
});

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

const [registerImageModal, { openModal: openImageModal }] = useModal();
const [registerConfirmModal, { openModal: openConfirmModal }] = useModal();
const [registerMapModal, { openModal: openMapModal }] = useModal();
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);
  openImageModal(true, { url: record.picture });
}
function handleEdit(record: Recordable) {
  openModal(true, { record, update: 1 });
}
function handConfirm(record: Recordable) {
  openConfirmModal(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 behaviorMessageRemove([record.id]);
  await reload();
}
</script>

<style scoped></style>
