<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #toolbar> </template>
      <template #topLogo="{ record }">
        <CropperAvatar
          :showBtn="false"
          :width="80"
          :uploadApi="uploadLogoApi"
          :value="record.topLogo"
          :title="t('routes.corporation.sceneLogo')"
          @change="
            (_, data) => {
              updateSceneLogo(record, data);
            }
          "
        />
      </template>
      <template #floorLogo="{ record }">
        <CropperAvatar
          :showBtn="false"
          :width="80"
          :uploadApi="uploadLogoApi"
          :value="record.floorLogo"
          :title="t('routes.corporation.floorLogo')"
          @change="
            (_, data) => {
              updateFloorLogo(record, data);
            }
          "
        />
      </template>
      <template #bgMusic="{ record }">
        <BasicUpload
          :maxSize="5"
          :maxNumber="1"
          @change="
            (data) => {
              handleMusicUpload(record, data);
            }
          "
          :api="handleMusicUploadFetch"
          class="my-5"
          :value="[record.bgMusic]"
          :accept="['.mp3', '.wav']"
        />
      </template>
      <template #expirationTime="{ record }">
        <Time v-if="record.auditTime" :value="record.auditTime" mode="datetime" />
      </template>

      <template #cameraNum="{ record }">
        <Tag
          style="cursor: pointer"
          @click="
            () => {
              openCameraModal(true, record);
            }
          "
          color="green"
          >{{ record.cameraNum }}</Tag
        >
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            // {
            //   // icon: 'ri:money-cny-circle-fill',
            //   label: t('routes.corporation.recharge'),
            //   onClick: handleOpenModal.bind(null, 'charge', record),
            // },
            {
              // icon: 'icon-park-outline:devices',
              label: t('routes.corporation.device'),
              onClick: handleOpenModal.bind(null, 'device', record),
            },
            {
              // icon: 'mdi:account-group',
              color: 'error',
              label: t('routes.corporation.sub_account'),
              onClick: handleOpenModal.bind(null, 'sub_account', record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <chargeModal @register="registerChargeModal" @update="reload" />
    <deviceModal @register="registerDeviceModal" @update="reload" />
    <SubaccountModal @register="registerSubaccountModal" @update="reload" />
    <cameraModal @register="registerCameraModal" @ok="reload" />
  </div>
</template>
<script lang="ts">
  import { useI18n } from '/@/hooks/web/useI18n';
  import { defineComponent, provide, nextTick, computed } from 'vue';
  import { BasicTable, useTable, BasicColumn, FormProps, TableAction } from '/@/components/Table';
  // import { CollapseContainer } from '/@/components/Container';
  import { CropperAvatar } from '/@/components/Cropper';
  import { BasicUpload } from '/@/components/Upload';
  // import { uploadApi } from '/@/api/sys/upload';
  // import { Avatar } from 'ant-design-vue';
  // import { getBasicColumns, getBasicData } from './tableData';
  import {
    ListApi,
    uploadLogoApi,
    uploadMusicApi,
    updateCompnayApi,
  } from '/@/api/corporation/list';
  import { useModal } from '/@/components/Modal';
  import chargeModal from './chargeModal.vue';
  import deviceModal from './deviceModal.vue';
  import SubaccountModal from './SubaccountModal.vue';
  import cameraModal from './cameraModal.vue';
  import { Time } from '/@/components/Time';
  import { Tag } from 'ant-design-vue';
  import type { Result } from '/#/axios';
  import { useLocaleStore } from '/@/store/modules/locale';
  const localeStore = useLocaleStore();
  export default defineComponent({
    components: {
      BasicTable,
      CropperAvatar,
      TableAction,
      chargeModal,
      deviceModal,
      SubaccountModal,
      cameraModal,
      Time,
      BasicUpload,
      Tag,
    },
    setup() {
      const [registerDeviceModal, { openModal: openDeviceModal }] = useModal();
      const [registerChargeModal, { openModal: openChargeModal }] = useModal();
      const [registerSubaccountModal, { openModal: openSubaccountModal }] = useModal();
      const [registerCameraModal, { openModal: openCameraModal }] = useModal();
      const { t } = useI18n();
      const isJA = computed(() => localeStore.getLocale === 'ja');
      const columns: BasicColumn[] = [
        // {
        //   title: 'ID',
        //   dataIndex: 'id',
        //   fixed: 'left',
        //   width: 80,
        // },
        {
          title: t('routes.corporation.enterpriseId'),
          dataIndex: 'managerPhone',
          fixed: 'left',
          width: 120,
        },
        {
          title: t('routes.corporation.enterpriseName'),
          dataIndex: 'companyName',
          width: 120,
        },
        {
          title: t('routes.corporation.sceneLogo'),
          dataIndex: 'topLogo',
          slots: { customRender: 'topLogo' },
          width: 160,
        },
        {
          title: t('routes.corporation.floorLogo'),
          dataIndex: 'floorLogo',
          slots: { customRender: 'floorLogo' },
          width: 160,
        },
        // {
        //   title: t('routes.corporation.bgMusic'),
        //   dataIndex: 'bgMusic',
        //   slots: { customRender: 'bgMusic' },
        //   width: isJA.value ? 220 : 160,
        // },
        {
          title: t('routes.corporation.cameraNum'),
          dataIndex: 'cameraNum',
          slots: { customRender: 'cameraNum' },
          width: 80,
        },
        {
          title: t('routes.corporation.subNum'),
          dataIndex: 'subNum',
          ellipsis: false,
          width: 80,
        },
        {
          title: t('routes.corporation.sceneNum'),
          dataIndex: 'sceneNum',
          ellipsis: false,
          width: 80,
        },
        // {
        //   title: t('routes.corporation.point'),
        //   ellipsis: false,
        //   dataIndex: 'point',
        //   width: 80,
        // },
        {
          title: t('routes.corporation.expirationTime'),
          dataIndex: 'auditTime',
          slots: { customRender: 'expirationTime' },
          width: 180,
        },
      ];

      const searchForm: Partial<FormProps> = {
        labelWidth: isJA.value ? 150 : 100,
        schemas: [
          // {
          //   field: 'id',
          //   label: 'id',
          //   component: 'Input',
          //   colProps: {
          //     xl: 8,
          //     xxl: 8,
          //   },
          // },
          {
            field: 'managerName',
            label: t('routes.corporation.enterpriseId'),
            labelWidth: isJA.value ? 250 : 100,
            component: 'Input',
            colProps: {
              xl: 12,
              xxl: 8,
            },
          },
        ],
      };

      provide('tablereload', tablereload);
      const [registerTable, { reload }] = useTable({
        title: t('routes.corporation.enterpriseId'),
        api: ListApi,
        columns: columns,
        useSearchForm: true,
        formConfig: searchForm,
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: true,
        rowKey: 'id',
        actionColumn: {
          width: isJA.value ? 200 : 120,
          title: t('common.operation'),
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });
      function tablereload() {
        console.log('tablereload');
        reload();
      }
      function handleOpenModal(val, record: Recordable) {
        let listKey = {
          charge: openChargeModal,
          device: openDeviceModal,
          sub_account: openSubaccountModal,
        };
        listKey[val] &&
          listKey[val](true, {
            record,
          });
        // openChargeModal(true, {
        //   record,
        // });
      }
      async function handleMusicUploadFetch({ file, name }) {
        const res = await uploadMusicApi({ file, name });
        const url = res?.data.data || '';
        console.log('url', url);
        return {
          data: {
            url: url,
          },
        };
      }
      async function handleMusicUpload(record: Recordable, data) {
        console.log('handleMusicUpload', record, data);
        const bgMusicRes: string = data?.length > 0 ? data[0] : '';
        await updateCompnayApi({
          id: record.id,
          bgMusic: bgMusicRes,
        });
        nextTick(reload);
      }
      async function updateSceneLogo(record: Recordable, data: Result) {
        console.log('src', data.data);
        await updateCompnayApi({
          id: record.id,
          topLogo: data.data,
        });
        nextTick(reload);
      }
      async function updateFloorLogo(record: Recordable, data: Result) {
        console.log('src', data.data);
        await updateCompnayApi({
          id: record.id,
          floorLogo: data.data,
        });
        nextTick(reload);
      }

      return {
        t,
        reload,
        registerTable,
        registerChargeModal,
        registerDeviceModal,
        registerSubaccountModal,
        handleOpenModal,
        uploadLogoApi: uploadLogoApi as any,
        tablereload,
        handleMusicUpload,
        updateSceneLogo,
        updateFloorLogo,
        handleMusicUploadFetch,
        registerCameraModal,
        openCameraModal,
      };
    },
  });
</script>
