<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" :loading="tableLoading">
          <template #toolbar>
            <a-button
              @click="
                downloadExcel(
                  relevancyCodeExport,
                  '电梯二维码关联信息;数据',
                  getForm().getFieldsValue(),
                )
              "
              v-auth="'elevator:relevancyCode:export'"
              >导出</a-button
            >
            <a-button
              type="primary"
              danger
              @click="multipleRemove(relevancyCodeRemove)"
              :disabled="!selected"
              v-auth="'elevator:relevancyCode:remove'"
              >删除</a-button
            >
            <a-button type="primary" @click="handleAdd" v-auth="'elevator:relevancyCode:add'"
              >新增</a-button
            >
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'isRelevancy'">
              <div>
                {{ record.isRelevancy === 1 ? '已关联' : '未关联' }}
              </div>
            </template>
            <template v-if="column.key === 'action'">
              <TableAction
                stopButtonPropagation
                :actions="[
                  {
                    label: '生成二维码',

                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:relevancyCode:createCode',
                    onClick: handleCreateCode.bind(null, record),
                  },
                  {
                    label: '修改',

                    type: 'primary',
                    ghost: true,
                    auth: 'elevator:relevancyCode:edit',
                    onClick: handleEdit.bind(null, record),
                  },
                  {
                    label: '删除',

                    type: 'primary',
                    danger: true,
                    ghost: true,
                    auth: 'elevator:relevancyCode:remove',
                    popConfirm: {
                      placement: 'left',
                      title: '是否删除电梯二维码关联信息;[' + record.id + ']?',
                      confirm: handleDelete.bind(null, record),
                    },
                  },
                ]"
              />
            </template>
          </template>
        </BasicTable> </Col
    ></Row>

    <RelevancyCodeModal @register="registerModal" @reload="reload" />
    <!-- <CodeCreate @register="registerCodeModal"  /> -->
    <Modal v-model:open="open" title="生成二维码" :footer="null">
      <Image :src="currentImage"></Image>
    </Modal>
  </PageWrapper>
</template>

<script setup lang="ts">
import { Row, Col, Space, Avatar, Modal, Image } from 'ant-design-vue';
import { PageWrapper } from '@/components/Page';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import {
  relevancyCodeList,
  relevancyCodeExport,
  relevancyCodeRemove,
  relevancyCodeCreate,
} from '@/api/codeManage/codeRelationShip';
import { downloadExcel } from '@/utils/file/download';
import { useModal } from '@/components/Modal';
import RelevancyCodeModal from './RelevancyCodeModal.vue';
// import CodeCreate from './CodeCreate.vue';
import { formSchemas, columns } from './relevancyCode.data';
import { IconEnum } from '@/enums/appEnum';
import DeptTree from './DeptTree.vue';
import { ref, toValue } from 'vue';
defineOptions({ name: 'RelevancyCode' });
const selectDeptId = ref<string[]>([]);
const searchValue = ref<string>('');
const open = ref(false);
const currentImage = ref('');
const tableLoading = ref(false);
const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
  rowSelection: {
    type: 'checkbox',
  },
  title: '电梯二维码关联信息列表',
  api: relevancyCodeList,
  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: 200,
    title: '操作',
    key: 'action',
    fixed: 'right',
  },
});

const [registerModal, { openModal }] = useModal();
const [registerCodeModal, { openModal: openCodeModal }] = useModal();
const handleCreateCode = (record: Recordable) => {
  tableLoading.value = true;
  relevancyCodeCreate({ id: record.id })
    .then((res: any) => {
      open.value = true;
      console.log(res, 'red');
      currentImage.value = res.qrCodeUrl;
      tableLoading.value = false;
    })
    .finally((result) => {
      tableLoading.value = false;
    });
};
function handleEdit(record: Recordable) {
  openModal(true, { record, update: true });
}

function handleAdd() {
  openModal(true, { update: false });
}

async function handleDelete(record: Recordable) {
  await relevancyCodeRemove([record.id]);
  await reload();
}
</script>

<style scoped></style>
