<template>
  <PageWrapper dense>
    <Row>
      <!-- 左边部门选择 -->
      <Col v-bind="{ xs: 24, sm: 24, md: 24, lg: 4 }" class="h-[calc(100vh-80px)]">
        <DeptTree
          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(
                  elevatorRelevancyDeviceExport,
                  '电梯设备关联;数据',
                  getForm().getFieldsValue()
                )
              "
              v-auth="'elevator:elevatorRelevancyDevice:export'"
              >导出</a-button
            >
            <a-button
              type="primary"
              danger
              @click="multipleRemove(elevatorRelevancyDeviceRemove)"
              :disabled="!selected"
              v-auth="'elevator:elevatorRelevancyDevice:remove'"
              >删除</a-button
            >
            <!-- <a-button
              type="primary"
              @click="handleAdd"
              v-auth="'device:elevatorRelevancyDevice:add'"
              >新增</a-button
            > -->
          </template>

          <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'isBlackBox'">
              <div>
                {{ findLabelByValue(record.isBlackBox, TRUE_RIGHT) }}
              </div>
            </template>
            <template v-if="column.dataIndex === 'isScreen'">
              <div>
                {{ findLabelByValue(record.isScreen, TRUE_RIGHT) }}
              </div>
            </template>
            <template v-if="column.dataIndex === 'isCamera'">
              <div>
                {{ findLabelByValue(record.isCamera, TRUE_RIGHT) }}
              </div>
            </template>
            <template v-if="column.dataIndex === 'isTimmy'">
              <div>
                {{ findLabelByValue(record.isTimmy, TRUE_RIGHT) }}
              </div>
            </template>
            <template v-if="column.dataIndex === 'isQrcode'">
              <div>
                {{ findLabelByValue(record.isQrcode, TRUE_RIGHT) }}
              </div>
            </template>

            <template v-if="column.key === 'action'">
              <TableAction
                stopButtonPropagation
                :actions="[
                  {
                    label: '查看',

                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:elevatorRelevancyDevice:query',
                    onClick: handCheck.bind(null, record),
                  },

                  {
                    label: '关联设备',

                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:elevatorRelevancyDevice:relation',
                    onClick: handleRelation.bind(null, record),
                  },
                ]"
              />
            </template>
          </template>
        </BasicTable>
      </Col>
    </Row>
    <RelationDeviceModal
      @register="registerRelationModal"
      @reload="reload"
    ></RelationDeviceModal>
    <ElevatorRelevancyDeviceModal @register="registerModal" @reload="reload" />
  </PageWrapper>
</template>

<script setup lang="ts">
import { Row, Col, Space, Avatar } from "ant-design-vue";
import { PageWrapper } from "@/components/Page";
import { BasicTable, useTable, TableAction } from "@/components/Table";
import {
  elevatorRelevancyDeviceList,
  elevatorRelevancyDeviceExport,
  elevatorRelevancyDeviceRemove,
} from "@/api/basicInfo/evaDeviceRelation";
import { downloadExcel } from "@/utils/file/download";
import { useModal } from "@/components/Modal";
import ElevatorRelevancyDeviceModal from "./ElevatorRelevancyDeviceModal.vue";
import { formSchemas, columns } from "./elevatorRelevancyDevice.data";
import RelationDeviceModal from "./RelationDeviceModal.vue";
import DeptTree from "./DeptTree.vue";
import { ref, toValue } from "vue";
import { findLabelByValue } from "@/utils/tool";
import { TRUE_RIGHT } from "@/utils/lib/commonMap";
defineOptions({ name: "ElevatorRelevancyDevice" });
// 左边部门用
const selectDeptId = ref<string[]>([]);
const searchValue = ref<string>("");

const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
  rowSelection: {
    type: "checkbox",
  },
  title: "电梯设备关联;列表",
  api: elevatorRelevancyDeviceList,
  showIndexColumn: true,
  rowKey: "id",
  useSearchForm: true,
  formConfig: {
    schemas: formSchemas,
    labelWidth: 130,
    baseColProps: {
      xs: 24,
      sm: 24,
      md: 24,
      lg: 6,
    },
    resetFunc: async () => {
      selectDeptId.value = [];
      searchValue.value = "";
      await reload();
    },
  },
  // 需要添加上部门参数
  beforeFetch(params: Recordable) {
    if (toValue(selectDeptId).length === 1) {
      params.deptId = toValue(selectDeptId)[0];
    }
    return params;
  },
  columns: columns,
  actionColumn: {
    width: 250,
    title: "操作",
    key: "action",
    fixed: "right",
  },
});

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

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

function handleAdd() {
  openModal(true, { update: 2 });
}
function handCheck(record: Recordable) {
  openModal(true, { record, update: 3 });
}
function handleRelation(record: Recordable) {
  openRelationModal(true, { record, update: 1 });
}
async function handleDelete(record: Recordable) {
  await elevatorRelevancyDeviceRemove([record.id]);
  await reload();
}
</script>

<style scoped></style>
