<template>
  <div>
    <div class="n-layout-page-header">
      <n-card :bordered="false" title="免费武器投放">
        <n-scrollbar x-scrollable>
          <n-table single-column :single-line="false">
            <thead>
              <tr>
                <th>到达广告界面总人数</th>
                <th>选择答案A总人数 / 占比</th>
                <th>选择答案B / 占比</th>
                <th>选择答案C / 占比</th>
                <th>选择放弃 / 占比</th>
                <th>选择立即装备 / 占比</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td style="min-width: 100px">{{ dataSource.goto }}</td>
                <td style="min-width: 150px"
                  >{{ dataSource.a }} /
                  {{ Number((dataSource.a / dataSource.goto) * 100).toFixed(2) }}%</td
                >
                <td style="min-width: 150px"
                  >{{ dataSource.b }} /
                  {{ Number((dataSource.b / dataSource.goto) * 100).toFixed(2) }}%</td
                >
                <td style="min-width: 150px"
                  >{{ dataSource.c }} /
                  {{ Number((dataSource.c / dataSource.goto) * 100).toFixed(2) }}%</td
                >
                <td style="min-width: 150px"
                  >{{ dataSource.waive }} /
                  {{ Number((dataSource.waive / dataSource.goto) * 100).toFixed(2) }}%</td
                >
                <td style="min-width: 150px"
                  >{{ dataSource.equip_now }} /
                  {{ Number((dataSource.equip_now / dataSource.goto) * 100).toFixed(2) }}%</td
                >
              </tr>
            </tbody>
          </n-table>
        </n-scrollbar>
      </n-card>
    </div>
    <n-card :bordered="false" class="proCard">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
        <template #statusSlot="{ model, field }">
          <n-input v-model:value="model[field]" />
        </template>
      </BasicForm>

      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="actionRef"
        @update:checked-row-keys="onCheckedRow"
        :scroll-x="1090"
        :canResize="true"
        :resizeHeightOffset="-20000"
      />
    </n-card>
  </div>
</template>

<script lang="ts" setup>
  import { h, reactive, ref, Component } from 'vue';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { NButton, NIcon, NTooltip } from 'naive-ui';
  import { FirstAdv, FirstAdvStat } from '@/api/echarts/UserAdv';
  import { defaultStatisticsTimeOptions, timestampToDate } from '@/utils/dateUtil';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  import { HelpCircleOutline } from '@vicons/ionicons5';

  const renderTooltip = (trigger, content) => {
    return h(NTooltip, null, {
      trigger: () => trigger,
      default: () => content,
    });
  };

  function renderIcon(icon: Component) {
    return () => h(NIcon, null, { default: () => h(icon) });
  }

  const params = ref<any>({
    pageSize: 10,
    day: timestampToDate(defaultStatisticsTimeOptions()),
    days: 0,
    channel_id: '',
    server_id: null,
  });

  const useUserStore = useUserStoreWidthOut();
  const columns = reactive([
    {
      title: '日期',
      dataIndex: 'date',
      key: 'date',
      width: 100,
    },
    {
      dataIndex: 'goto',
      key: 'goto',
      width: 120,
      title(column) {
        return renderTooltip(
          h(
            NButton,
            {
              ghost: true,
              strong: true,
              tertiary: true,
              size: 'small',
              text: true,
              iconPlacement: 'right',
            },
            { default: () => '到达该广告界面', icon: renderIcon(HelpCircleOutline) }
          ),
          '每天到达该广告界面的人数'
        );
      },
    },
    {
      title(column) {
        return renderTooltip(
          h(
            NButton,
            {
              ghost: true,
              strong: true,
              tertiary: true,
              size: 'small',
              text: true,
              iconPlacement: 'right',
            },
            { default: () => '选择答案A / 占比', icon: renderIcon(HelpCircleOutline) }
          ),
          '选择答案A人数 / 到达该广告界面人数 *100'
        );
      },
      dataIndex: 'a',
      key: 'a',
      width: 120,
      render(row) {
        if (row.a == 0 || row.goto == 0) {
          return '-';
        }
        return row.a + ' / ' + ((row.a / row.goto) * 100).toFixed(2) + '%';
      },
    },
    {
      title: '选择答案B / 占比',
      dataIndex: 'b',
      key: 'b',
      width: 120,
      render(row) {
        if (row.b == 0 || row.goto == 0) {
          return '-';
        }
        return row.b + ' / ' + ((row.b / row.goto) * 100).toFixed(2) + '%';
      },
    },
    {
      title: '选择答案C / 占比',
      dataIndex: 'c',
      key: 'c',
      width: 120,
      render(row) {
        if (row.c == 0 || row.goto == 0) {
          return '-';
        }
        return row.c + ' / ' + ((row.c / row.goto) * 100).toFixed(2) + '%';
      },
    },
    {
      title: '选择放弃 / 占比',
      dataIndex: 'waive',
      key: 'waive',
      width: 120,
      render(row) {
        if (row.waive == 0 || row.goto == 0) {
          return '-';
        }
        return row.waive + ' / ' + ((row.waive / row.goto) * 100).toFixed(2) + '%';
      },
    },
    {
      title: '选择立即装备 / 占比',
      dataIndex: 'equipNow',
      key: 'equipNow',
      width: 120,
      render(row) {
        if (row.equipNow == 0 || row.goto == 0) {
          return '-';
        }
        return row.equipNow + ' / ' + ((row.equipNow / row.goto) * 100).toFixed(2) + '%';
      },
    },
  ]);

  const actionRef = ref();
  const dataSource = ref<any>({
    goto: 0,
    a: 0,
    b: 27,
    c: 0,
    waive: 0,
    equip_now: 0,
  });
  const formParams = reactive({
    day: '',
    days: 0,
    channel_id: '',
  });
  const schemas: FormSchema[] = [
    {
      field: 'server_id',
      component: 'NSelect',
      label: '服务器',
      componentProps: {
        placeholder: '请选择服务器',
        defaultValue: params.value.server_id,
        options: useUserStore.config.server_option,
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.server_id = e;
        },
      },
    },
    {
      field: 'channel_id',
      component: 'NTreeSelect',
      label: '推广渠道',
      componentProps: {
        placeholder: '请选择推广渠道',
        clearFilterAfterSelect: true,
        clearable: true,
        filterable: true,
        defaultExpandAll: true,
        options: useUserStore.config.channel_tree_option,
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.channel_id = e;
        },
      },
    },
  ];

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  const loadDataTable = (res) => {
    FirstAdvStat({ ...formParams, ...params.value, ...res }).then((res) => {
      dataSource.value = res;
      console.log(' dataSource.value:' + JSON.stringify(dataSource.value));
    });

    return FirstAdv({ ...formParams, ...params.value, ...res });
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function handleSubmit(values: Recordable) {
    console.log(values);
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
    params.value = {
      pageSize: 100,
      day: timestampToDate(defaultStatisticsTimeOptions()),
      days: 0,
      channel_id: '',
      server_id: null,
    };
    reloadTable();
  }
</script>

<style scoped></style>
