<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #toolbar> </template>
      <template #sceneLogo="{ record }">
        <CropperAvatar
          :showBtn="false"
          :width="80"
          :uploadApi="uploadLogoApi"
          :value="record.sceneLogo"
          :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
          style="color: #0960bd; cursor: pointer"
          v-if="record.expirationTime"
          @click="openTimeModal(true, record)"
          :value="record.expirationTime"
          mode="date"
        />
      </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" @reload="reload" />
    <SubaccountModal @register="registerSubaccountModal" @update="reload" />
    <cameraModal @register="registerCameraModal" @ok="reload" />
    <TimeModal @register="registerTimeModal" @submit="reload" />
  </div>
</template>
<script lang="ts">
  import { useI18n } from '/@/hooks/web/useI18n';
  import { defineComponent, nextTick } 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, updateCompnayApi } from '/@/api/corporation/list';
  import { useModal } from '/@/components/Modal';
  import chargeModal from './chargeModal.vue';
  import deviceModal from './deviceModal.vue';
  import TimeModal from './setTime.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';

  export default defineComponent({
    components: {
      BasicTable,
      CropperAvatar,
      TableAction,
      chargeModal,
      deviceModal,
      SubaccountModal,
      cameraModal,
      Time,
      BasicUpload,
      Tag,
      TimeModal,
    },
    setup() {
      const [registerDeviceModal, { openModal: openDeviceModal }] = useModal();
      const [registerChargeModal, { openModal: openChargeModal }] = useModal();
      const [registerSubaccountModal, { openModal: openSubaccountModal }] = useModal();
      const [registerCameraModal, { openModal: openCameraModal }] = useModal();
      const [registerTimeModal, { openModal: openTimeModal }] = useModal();
      const { t } = useI18n();
      const columns: BasicColumn[] = [
        // {
        //   title: 'ID',
        //   dataIndex: 'id',
        //   fixed: 'left',
        //   width: 80,
        // },
        {
          title: t('routes.corporation.enterpriseId'),
          dataIndex: 'userName',
          fixed: 'left',
          width: 120,
        },
        {
          title: t('routes.corporation.enterpriseName'),
          dataIndex: 'name',
          width: 120,
        },
        {
          title: t('routes.corporation.sceneLogo'),
          dataIndex: 'sceneLogo',
          slots: { customRender: 'sceneLogo' },
          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: 190,
        },
        {
          title: t('routes.corporation.cameraNum'),
          dataIndex: 'cameraNum',
          slots: { customRender: 'cameraNum' },
          width: 80,
        },
        {
          title: t('routes.corporation.subNum'),
          dataIndex: 'totalSubNum',
          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: 'expirationTime',
          slots: { customRender: 'expirationTime' },
          width: 180,
        },
      ];

      const searchForm: Partial<FormProps> = {
        labelWidth: 100,
        schemas: [
          {
            field: 'name',
            label: '企业名称',
            component: 'Input',
            colProps: {
              xl: 5,
              xxl: 5,
            },
            componentProps: {
              maxLength: 100,
            },
          },
          {
            field: 'userName',
            label: t('routes.corporation.enterpriseId'),
            component: 'Input',
            componentProps: {
              maxLength: 100,
            },
            colProps: {
              xl: 12,
              xxl: 8,
            },
          },
        ],
      };
      // { getForm }
      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',
        //TODO
        fetchSetting: {
          pageField: 'page',
          sizeField: 'limit',
          listField: 'list',
          totalField: 'totalCount',
        },
        actionColumn: {
          width: 160,
          title: t('common.operation'),
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });
      function handleOpenModal(val, record: Recordable) {
        let listKey = {
          charge: openChargeModal,
          device: openDeviceModal,
          sub_account: openSubaccountModal,
        };
        listKey[val] &&
          listKey[val](true, {
            record,
          });
      }
      async function handleMusicUploadFetch({ file, name }) {
        const res = await uploadLogoApi({ file, name });
        return {
          data: {
            url: res.data.message,
          },
        };
      }
      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.message);
        console.log('record', record);
        await updateCompnayApi({
          id: record.id,
          sceneLogo: data.message,
        });
        nextTick(reload);
      }
      async function updateFloorLogo(record: Recordable, data: Result) {
        await updateCompnayApi({
          id: record.id,
          floorLogo: data.message,
        });
        nextTick(reload);
      }

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