<script setup lang="tsx">
import { NButton, NImage, NPopconfirm, NSwitch } from 'naive-ui';
import {
  fetchAddAdvertInfo,
  fetchDeleteAdvertInfo,
  fetchGetadvertInfoList,
  fetchUpdateAdvertInfo
} from '@/service/api/xsb-manage/advertisement';
import { useTable } from '@/components/zt/Table/hooks/useTable';
import { useModalFrom } from '@/components/zt/ModalForm/hooks/useModalForm';

const columns: NaiveUI.TableColumn<Api.operation.AdvertInfo>[] = [
  {
    key: 'advertName',
    title: '广告位名称',
    align: 'center'
  },
  {
    key: 'advertImg',
    title: '广告图',
    align: 'center',
    render: row => {
      return <NImage src={row.advertImg} class={'h90px w180px'}></NImage>;
    }
  },
  {
    key: 'advertBack',
    title: '背景图',
    align: 'center',
    render: row => {
      return <NImage src={row.advertBack} class={'h90px w180px'}></NImage>;
    }
  },
  {
    key: 'jumpUrl',
    title: '跳转链接',
    align: 'center'
  },
  {
    key: 'advertSort',
    title: '排序',
    align: 'center'
  },
  {
    key: 'status',
    title: '状态',
    align: 'center',
    render: row => {
      return (
        <NSwitch
          uncheckedValue={0}
          checkedValue={1}
          value={row.status}
          onUpdate:value={val => {
            row.status = val;
            fetchUpdateAdvertInfo(row);
          }}
        ></NSwitch>
      );
    }
  }
];

const [registerTable, { refresh, setTableLoading }] = useTable({
  searchFormConfig: {
    schemas: [
      {
        field: 'advertName',
        label: '广告位名称',
        component: 'NInput'
      },
      {
        field: 'status',
        label: '状态',
        component: 'NSelect',
        componentProps: {
          options: [
            {
              label: '启用',
              value: 1
            },
            {
              label: '禁用',
              value: 0
            }
          ]
        }
      }
    ],
    inline: false,
    size: 'small',
    labelPlacement: 'left',
    isFull: false
  },
  tableConfig: {
    keyField: 'id',
    title: '广告位列表',
    showAddButton: true
  }
});

async function handleDelete(row: Recordable) {
  setTableLoading(true);
  await fetchDeleteAdvertInfo(row.id);
  refresh();
}
const [registerModalForm, { openModal, closeModal, getFieldsValue, setFieldsValue }] = useModalFrom({
  modalConfig: {
    title: '广告位',
    width: 800,
    isShowHeaderText: true,
    height: 600
  },
  formConfig: {
    schemas: [
      {
        label: '',
        field: 'id',
        component: 'NInput',
        show: false
      },
      {
        field: 'advertName',
        label: '广告位名称',
        component: 'NInput',
        required: true
      },
      {
        field: 'advertImg',
        label: '广告图',
        component: 'zUpload',
        required: true,
        componentProps: {
          tipText: '单张，仅支持png，jpg，jpeg格式，建议尺寸比例 16:9；图片大小不能超过3m。',
          max: 1,
          FileSize: 3
        }
      },
      {
        field: 'advertBack',
        label: '背景图',
        component: 'zUpload',
        required: true,
        componentProps: {
          tipText: '单张，仅支持png，jpg，jpeg格式，建议尺寸比例 16:9；图片大小不能超过3m。',
          max: 1,
          FileSize: 3
        }
      },
      {
        field: 'jumpUrl',
        label: '链接和参数',
        component: 'NInput',
        componentProps: {
          placeholder: '列如：/pages/xxx?id=1'
        },
        required: true
      },
      {
        field: 'advertSort',
        label: '排序',
        component: 'NInputNumber',
        required: true
      },
      {
        field: 'status',
        label: '状态',
        component: 'NSwitch',
        required: true,
        componentProps: {
          checkedValue: 1,
          uncheckedValue: 0
        },
        defaultValue: 1
      }
    ],
    gridProps: {
      cols: '1'
    },
    labelWidth: 120
  }
});
async function handleSubmit() {
  const form = await getFieldsValue();
  if (form.id) {
    await fetchUpdateAdvertInfo(form);
  } else {
    await fetchAddAdvertInfo(form);
  }
  closeModal();
  refresh();
}

async function edit(row: Recordable) {
  openModal(row);
  setFieldsValue(row);
}
</script>

<template>
  <LayoutTable>
    <ZTable :columns="columns" :api="fetchGetadvertInfoList" @register="registerTable" @add="openModal">
      <template #op="{ row }">
        <NButton size="small" ghost type="primary" @click="edit(row)">编辑</NButton>
        <NPopconfirm @positive-click="handleDelete(row)">
          <template #trigger>
            <NButton size="small" type="error" ghost>删除</NButton>
          </template>
          确定删除吗？
        </NPopconfirm>
      </template>
    </ZTable>
    <BasicModelForm @register-modal-form="registerModalForm" @submit-form="handleSubmit"></BasicModelForm>
  </LayoutTable>
</template>

<style scoped></style>
