<template>
  <PageWrapper contentBackground>
    <template #footer>
      <a-tabs v-model:activeKey="searchInfo.platformType" @change="tabChange">
        <a-tab-pane key="1" :tab="t('routes.product.sdkType.1')" />
        <a-tab-pane key="2" :tab="t('routes.product.sdkType.2')" />
      </a-tabs>
    </template>
    <div class="desc-wrap-BasicTable">
      <BasicTable @register="registerTimeTable">
        <template #toolbar>
          <a-button
            type="primary"
            v-if="getTypeCheckPerm('sdk-add')"
            @click="
              () => {
                openAddModal(true, searchInfo.platformType);
              }
            "
            >{{ t('routes.product.addVstive') }}</a-button
          >
        </template>
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                label: '发布',
                //icon: 'arcticons:efa-publish',
                ifShow:getTypeCheckPerm('sdk-publish') && record.status != 1,
                onClick: handlePublish.bind(null, record),
              },
              {
                label: '编辑',
                //icon: 'ep:edit',
                ifShow: getTypeCheckPerm('sdk-update'),
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '下架',
                ifShow: getTypeCheckPerm('sdk-lower') && record.status == 1,
                //icon: 'akar-icons:statistic-down',
                onClick: handleOff.bind(null, record),
              },
              {
                label: '删除',
                ifShow: getTypeCheckPerm('sdk-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 { SpaceSdkList, SpaceSdkDelete, SpaceSdkOnline } from '/@/api/product';
import { SpaceSdkTop } 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>({
      platformType: '1',
    });
    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: 'imprintCh',
      },
      {
        title: '首次发布时间',
        width: 120,
        dataIndex: 'publishTime',
        customRender: ({ record }) => {
          return (
            record.publishTime &&
            h(Time, {
              value: record.publishTime,
              mode: 'datetime',
            })
          );
        },
      },{
        title: '创建人',
        width: 120,
        dataIndex: 'createName',
      },{
        title: '创建时间',
        width: 120,
        dataIndex: 'createTime',
        customRender: ({ record }) => {
          return (
            record.createTime &&
            h(Time, {
              value: record.createTime,
              mode: 'datetime',
            })
          );
        },
      },{
        title: '置顶',
        dataIndex: 'isTop',
        ifShow: getCheckPerm('sdk-top'),
        width: 80,
        customRender: ({ record }) => {
          if (!Reflect.has(record, 'isTop')) {
            record.pendingStatus = false;
          }
          return h(Switch, {
            checked: record.isTop == 1 ? true : false,
            checkedChildren: '是',
            unCheckedChildren: '否',
            disabled:record.status != 1,
            loading: false,
            onChange: async (checked: boolean) => {
              record.pendingStatus = true;
              const newStatus = checked?1:0;
              await SpaceSdkTop({...record,isTop:newStatus});
              Reflect.set(record, 'isTop', checked);
              createMessage.success(t('common.optSuccess'));
              reload()
            },
          });
        },
      },{
        title: '发布状态',
        dataIndex: 'status',
        width: 80,
        customRender: ({ record }) => {
          return renderType(record.status)
        },
      },
    ];
    const [registerTimeTable, { reload }] = useTable({
      api: SpaceSdkList,
      title: 'SDK列表',
      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 SpaceSdkDelete({ 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.platformType
      });
    }
    
    function getTypeCheckPerm(val){
        let myType = searchInfo.platformType
        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>
