<template>
  <PageWrapper contentBackground>
    <template #footer>
      <a-tabs v-model:activeKey="searchInfo.type" @change="tabChange">
        <a-tab-pane key="0" tab="四维看看" />
        <a-tab-pane key="1" tab="四维深时" />
        <a-tab-pane key="2" tab="四维全景" />
        <a-tab-pane key="3" tab="四维带看" />
        <!-- <a-tab-pane key="4" tab="四维深光" /> -->
      </a-tabs>
    </template>
    <div class="desc-wrap-BasicTable">
      <BasicTable @register="registerTimeTable">
        <template #toolbar>
          <a-button
            type="primary"
            v-if="getTypeCheckPerm('updataTips-add')"
            @click="
              () => {
                openAddModal(true, searchInfo.type);
              }
            "
            >新增</a-button
          >
        </template>
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                label: '编辑',
                //icon: 'ep:edit',
                ifShow: getTypeCheckPerm('updataTips-edit'),
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                ifShow: getTypeCheckPerm('updataTips-delete') && record.status != 1,
                //icon: 'ic:outline-delete-outline',
                onClick: handleDelete.bind(null, record),
              },
            ]"
          />
        </template>
      </BasicTable>
    </div>
    <AddModal @update="reload" @register="registerAddModal" />
    <EditModal @register="registerEditModal" @update="reload" />
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, reactive, h } from 'vue';
  import { Time } from '/@/components/Time';
  import { BasicTable, useTable, FormProps, TableAction, BasicColumn } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { Divider, Card, Empty, Descriptions, Steps, Tabs, Switch } from 'ant-design-vue';
  import { tipList, tipDelete, SpaceSdkOnline } from '/@/api/product';
  import { tipUpdata } from '/@/api/product/index';
  import { useModal } from '/@/components/Modal';
  import { useI18n } from '/@/hooks/web/useI18n';
  import AddModal from './AddModal.vue';
  import EditModal from './EditModal.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { usePermissionStore } from '/@/store/modules/permission';
  export default defineComponent({
    components: {
      BasicTable,
      AddModal,
      EditModal,
      TableAction,
      PageWrapper,
      [Divider.name]: Divider,
      [Card.name]: Card,
      Empty,
      [Descriptions.name]: Descriptions,
      [Descriptions.Item.name]: Descriptions.Item,
      [Steps.name]: Steps,
      [Steps.Step.name]: Steps.Step,
      [Tabs.name]: Tabs,
      [Tabs.TabPane.name]: Tabs.TabPane,
    },
    setup() {
      const { t } = useI18n();
      const permissionStore = usePermissionStore();
      const { getCheckPerm } = permissionStore;
      const searchInfo = reactive<Recordable>({
        type: '0',
      });
      const [registerAddModal, { openModal: openAddModal }] = useModal();
      const [registerEditModal, { openModal: openEditModal }] = useModal();
      const { createMessage, createConfirm } = useMessage();
      const searchForm: Partial<FormProps> = {
        labelWidth: 100,
        schemas: [
          {
            field: 'version',
            label: '版本号',
            component: 'Input',
            componentProps: {
              maxLength: 100,
            },
            colProps: {
              xl: 6,
              xxl: 6,
            },
          },
        ],
      };
      const sdkTableSchema: BasicColumn[] = [
        {
          title: '版本号',
          width: 80,
          dataIndex: 'version',
        },
        {
          title: '提示时段',
          width: 240,
          dataIndex: 'timeList',
          customRender: ({ record }) => {
            return record.timeList[0] + '~' + record.timeList[1];
          },
        },
        {
          title: '标题',
          width: 150,
          dataIndex: 'title',
          ellipsis: true,
        },
        {
          title: '创建人',
          width: 120,
          dataIndex: 'createName',
        },
        {
          title: '创建时间',
          width: 150,
          dataIndex: 'createTime',
          customRender: ({ record }) => {
            return (
              record.createTime &&
              h(Time, {
                value: record.createTime,
                mode: 'datetime',
              })
            );
          },
        },
        {
          title: '状态',
          dataIndex: 'banStatus',
          // ifShow: getCheckPerm('tips-status'),
          ifShow: getCheckPerm('updataTips-enable'),
          width: 100,
          customRender: ({ record }) => {
            if (!Reflect.has(record, 'banStatus')) {
              record.pendingStatus = false;
            }
            return h(Switch, {
              checked: record.banStatus == 0 ? true : false,
              checkedChildren: '启用',
              unCheckedChildren: '禁用',
              // disabled:record.status != 1,
              loading: false,
              onChange: async (checked: boolean) => {
                record.pendingStatus = true;
                const newStatus = checked ? 0 : 1;
                await tipUpdata({ ...record, banStatus: newStatus });
                Reflect.set(record, 'banStatus', checked);
                createMessage.success(t('common.optSuccess'));
                reload();
              },
            });
          },
        },
      ];
      const [registerTimeTable, { reload }] = useTable({
        api: tipList,
        title: '',
        columns: sdkTableSchema,
        useSearchForm: true,
        formConfig: searchForm,
        showTableSetting: true,
        showIndexColumn: false,
        rowKey: 'id',
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
        searchInfo: searchInfo,
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
        canResize: true,
      });
      function tabChange(val: string) {
        console.log('tabChange', val);
        reload();
      }
      function renderType(type: number): string {
        switch (type) {
          case 0:
            return t(`routes.product.statusType.0`);
          case 1:
            return t(`routes.product.statusType.1`);
          default:
            return t(`routes.product.statusType.0`);
        }
      }
      async function handleDelete(record: Recordable) {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', t('sys.app.logoutTip')),
          content: () => h('span', '确定要删除吗？'),
          onOk: async () => {
            await tipDelete({ id: record.id });
            createMessage.success(t('common.optSuccess'));
            reload();
          },
        });
      }
      function handleOpen(record: Recordable) {
        console.log('点击了启用', record);
      }
      async function handlePublish(record: Recordable) {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', t('sys.app.logoutTip')),
          content: () => h('span', '是否确定发布该文件？发布后将在官网展示。'),
          onOk: async () => {
            await SpaceSdkOnline({ id: record.id, status: 1 }); //状态 0 - 未发布 1 -发布  2-下架
            createMessage.success(t('common.optSuccess'));
            reload();
          },
        });
      }
      async function handleOff(record: Recordable) {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', t('sys.app.logoutTip')),
          content: () => h('span', '是否确定下架？下架后官网不再展示该SDK。'),
          onOk: async () => {
            await SpaceSdkOnline({ id: record.id, status: 2 }); //状态 0 - 未发布 1 -发布  2-下架
            createMessage.success(t('common.optSuccess'));
            reload();
          },
        });
      }
      function handleEdit(record: Recordable) {
        console.log('record', record);
        openEditModal(true, {
          ...record,
          type: searchInfo.type,
        });
      }

      function getTypeCheckPerm(val) {
        let myType = searchInfo.type;
        return getCheckPerm(val) || getCheckPerm(`${val}-${myType}`);
      }
      return {
        registerTimeTable,
        handleDelete,
        handleOpen,
        handlePublish,
        tabChange,
        handleOff,
        reload,
        registerAddModal,
        registerEditModal,
        openAddModal,
        handleEdit,
        getTypeCheckPerm,
        t,
        searchInfo,
      };
    },
  });
</script>
<style lang="less" scoped>
  .desc-wrap-BasicTable {
    background-color: #f0f2f5;
    .vben-basic-table-form-container {
      padding: 0;
    }
  }
</style>
