<template>
  <PageWrapper dense contentFullHeight fixed-height>
    <BasicTable @register="registerTable" isMenuTable>
      <template #toolbar>
        <div>
          <a-button type="primary" v-auth="'dataDesign:add'" @click="handleCreate">
            {{ t('新增') }}
          </a-button>
        </div>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              auth: 'dataDesign:disabled',
              label: record.enabledMark === 1 ? '禁用' : '启用',
              ifShow: record.enabledMark !== -1,
              onClick: handleEnable.bind(null, record),
            },
            {
              label: '编辑',
              auth: 'dataDesign:edit',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              auth: 'dataDesign:delete',
              color: 'error',
              onClick: handleDelete.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <DataDesign v-if="DesignVisible" :editId="editId" @close="handleClose" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction, FormSchema, BasicColumn } from '/@/components/Table';

  import { PageWrapper } from '/@/components/Page';
  import { useMessage } from '/@/hooks/web/useMessage';
  import DataDesign from './components/dataDesign.vue';

  import { h, ref, createVNode } from 'vue';

  import { Modal, Tag } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { usePermission } from '/@/hooks/web/usePermission';

  import { deleteMobileData, getPageList, updateEnableMark } from '/@/api/mobileDesign';

  const { t } = useI18n();
  const DesignVisible = ref(false);
  const editId = ref();
  const searchFormSchema: FormSchema[] = [
    {
      field: 'name',
      label: t('页面名称'),
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: t('请输入页面名称'),
      },
    },
    {
      field: 'code',
      label: t('页面编码'),
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: t('请输入页面编码'),
      },
    },
    {
      field: 'enabledMark',
      label: t('页面状态'),
      component: 'Select',
      colProps: { span: 8 },
      componentProps: {
        placeholder: t('请选择页面状态'),
        options: [
          { label: '启用', value: 1 },
          { label: '禁用', value: 0 },
          { label: '草稿', value: -1 },
        ],
      },
    },
  ];

  const columns: BasicColumn[] = [
    {
      dataIndex: 'name',
      title: t('页面名称'),
      align: 'center',
      resizable: true,
    },

    {
      dataIndex: 'code',
      title: t('页面编码'),
      align: 'center',
      resizable: true,
    },
    {
      dataIndex: 'enabledMark',
      title: t('页面状态'),
      align: 'center',
      width: 120,
      resizable: true,
      customRender: ({ record }) => {
        const color =
          record.enabledMark == 1 ? 'blue' : record.enabledMark == -1 ? 'yellow' : 'red';
        const text = record.enabledMark == 1 ? '正常' : record.enabledMark == -1 ? '草稿' : '停用';
        return h(Tag, { color: color }, () => text);
      },
    },
    {
      dataIndex: 'remark',
      title: t('备注'),
      align: 'center',
      resizable: true,
    },
  ];
  const { notification } = useMessage();
  const { hasPermission } = usePermission();

  const [registerTable, { reload }] = useTable({
    title: t('数据展示页列表'),
    api: getPageList,
    rowKey: 'id',
    columns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
      fieldMapToTime: [],
      showResetButton: false,
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    actionColumn: {
      width: 140,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    tableSetting: {
      size: false,
    },
    customRow: (record) => {
      return {
        ondblclick: () => {
          if (hasPermission('dataDesign:edit')) {
            handleEdit(record);
          }
        },
      };
    },
  });

  function handleCreate() {
    DesignVisible.value = true;
  }

  function handleEdit(record: Recordable) {
    editId.value = record.id;
    DesignVisible.value = true;
  }

  function handleDelete(record: Recordable = {}) {
    const ids = record.id;

    Modal.confirm({
      title: t('提示'),
      icon: createVNode(ExclamationCircleOutlined),
      content: t('确定要删除所选项吗？'),
      onOk() {
        deleteMobileData(ids).then(() => {
          reload();
          notification.success({
            message: t('提示'),
            description: t('删除成功'),
          });
        });
      },
      onCancel() {},
      okText: t('确认'),
      cancelText: t('取消'),
    });
  }
  function handleClose() {
    editId.value = '';
    DesignVisible.value = false;
    reload();
  }
  async function handleEnable(record) {
    let res = await updateEnableMark({
      id: record.id,
      enabledMark: record.enabledMark == 1 ? 0 : 1,
    });
    if (res) {
      notification.success({
        message: t('提示'),
        description: t('修改状态成功'),
      });
      reload();
    } else {
      notification.error({
        message: t('提示'),
        description: t('修改状态失败'),
      });
    }
  }
</script>
<style lang="less" scoped></style>
