<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #toolbar> </template>
      <template #cover="{ record }">
        <TableImg :size="150" :simpleShow="true" :imgList="[record.cover]" />
      </template>
      <template #action>
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: () => {
                createMessage.info(`暂未接入`);
              },
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否确认删除',
                confirm: () => {
                  createMessage.info(`暂未接入`);
                },
              },
            },
          ]"
        />
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import {
    BasicTable,
    useTable,
    BasicColumn,
    FormProps,
    TableAction,
    TableImg,
  } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { uploadApi } from '/@/api/sys/upload';
  import { Switch } from 'ant-design-vue';
  import { h } from 'vue';
  import { ListApi } from '/@/api/scene/list';
  import { useI18n } from '/@/hooks/web/useI18n';

  export default defineComponent({
    components: { BasicTable, TableAction, TableImg },
    setup() {
      const { createMessage } = useMessage();
      const { t } = useI18n();
      const columns: BasicColumn[] = [
        {
          title: 'ID',
          dataIndex: 'id',
          fixed: 'left',
          width: 100,
        },
        {
          title: '场景名称',
          dataIndex: 'name',
          width: 230,
        },
        {
          title: '封面',
          dataIndex: 'cover',
          slots: { customRender: 'cover' },
          width: 120,
        },
        {
          title: '场景链接',
          dataIndex: 'link',
          slots: { customRender: 'link' },
          width: 180,
        },
        {
          title: '是否显示',
          dataIndex: 'isShow',
          width: 180,
          customRender: ({ record }) => {
            if (!Reflect.has(record, 'pendingStatus')) {
              record.pendingStatus = false;
            }
            return h(Switch, {
              checked: record.isShow,
              checkedChildren: t('common.yes'),
              unCheckedChildren: t('common.no'),
              loading: false,
              onChange(checked: boolean) {
                record.pendingStatus = true;
                const newStatus = checked ? '1' : '0';
                const { createMessage } = useMessage();
                createMessage.info(`暂未接入` + newStatus);
                // setRoleStatus(record.id, newStatus)
                //   .then(() => {
                //     record.status = newStatus;
                //     createMessage.success(`已成功修改角色状态`);
                //   })
                //   .catch(() => {
                //     createMessage.error('修改角色状态失败');
                //   })
                //   .finally(() => {
                //     record.pendingStatus = false;
                //   });
              },
            });
          },
        },

        {
          title: '操作',
          dataIndex: '',
          slots: { customRender: 'action' },
          width: 120,
        },
      ];

      const searchForm: Partial<FormProps> = {
        labelWidth: 100,
        schemas: [
          {
            field: 'id',
            label: 'id',
            component: 'Input',
            colProps: {
              xl: 3,
              xxl: 3,
            },
          },
          {
            field: 'userName',
            label: '企业账号',
            component: 'Input',
            colProps: {
              xl: 12,
              xxl: 8,
            },
          },
        ],
      };
      // { getForm }
      const [registerTable] = useTable({
        title: '场景列表',
        api: ListApi,
        columns: columns,
        useSearchForm: false,
        formConfig: searchForm,
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: false,
        rowKey: 'id',
        //TODO
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
        pagination: { pageSize: 20 },
      });

      // pagination.value = { pageSize: 20 };
      return {
        registerTable,
        createMessage,
        t,
        uploadApi: uploadApi as any,
      };
    },
  });
</script>
