<template>
  <div>
    <n-card :bordered="false" class="proCard" title="次日关卡分布">
      <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"
        :pagination="false"
        :resizeHeightOffset="-2000"
      />
    </n-card>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { chapterFirstDay } from '@/api/echarts/chapterFirstDay';
  import { defaultStatisticsTimeOptions, timestampToDate } from '@/utils/dateUtil';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  import { getChapterAndDifficulty } from '@/convert/chapter/map';

  const useUserStore = useUserStoreWidthOut();
  const columns = [
    {
      title: '房间ID',
      key: 'room_id',
      width: 100,
    },
    {
      title: '房间途径人数',
      key: 'count',
      width: 100,
    },
    {
      key: 'count',
      width: 100,
      title: '房间留存率',
      render(row) {
        if (row.oldTimes == 0 || row.oldUsers == 0) {
          return '-';
        }
        return ((row.count / indexNum.value) * 100).toFixed(2) + '%';
      },
    },
    {
      title: '房间结算人数',
      key: 'settle_count',
      width: 100,
    },
  ];

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

  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;
        },
      },
    },
    {
      field: 'flag',
      component: 'NSelect',
      label: '玩家分类',
      componentProps: {
        placeholder: '请选择玩家分类',
        options: useUserStore.config.flag_option,
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.flag = e;
        },
      },
    },
    {
      field: 'type',
      component: 'NSelect',
      label: '选择章节',
      componentProps: {
        placeholder: '请选择章节',
        options: useUserStore.config.chapter_option,
        consistentMenuWidth: false,
        onUpdateValue: (e: any) => {
          const chapter = getChapterAndDifficulty(e);
          params.value.chapter_id = chapter?.id;
          params.value.difficulty = chapter?.diff;
        },
      },
    },
    {
      field: 'makeDate',
      component: 'NDatePicker',
      label: '选择日期',
      defaultValue: defaultStatisticsTimeOptions(),
      componentProps: {
        type: 'date',
        clearable: true,
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.day = timestampToDate(e);
        },
      },
    },
  ];

  const indexNum = ref(0);
  const actionRef = ref();
  const formParams = reactive({
    day: '',
    date: null,
    difficulty: 0,
    chapter_id: 0,
    days: 0,
    channel_id: '',
    flag: -1,
  });

  const [register, {}] = useForm({
    gridProps: { cols: '6' },
    labelWidth: 80,
    schemas,
  });

  const loadDataTable = async (res) => {
    const data = await chapterFirstDay({ ...formParams, ...params.value, ...res });
    console.log('data:' + JSON.stringify(data));

    if (data.list !== undefined && data.list !== null && data.list.length > 0) {
      indexNum.value = data.list[0].count;
    }

    return data;
  };

  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: 10,
      day: timestampToDate(defaultStatisticsTimeOptions()),
      difficulty: 0,
      chapter_id: 0,
      days: 1,
      channel_id: '',
      server_id: null,
      flag: -1,
    };
    reloadTable();
  }
</script>

<style scoped></style>
