<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>

      <n-card
        title="玩家角色拥有情况"
        :content-style="{ padding: '10px' }"
        :header-style="{ padding: '10px' }"
      >
        <Chart :loading="loading" :options="options3" style="width: 100%; min-height: 600px" />
      </n-card>
    </n-card>

    <n-card
      title="选择区间内玩家角色占比"
      :content-style="{ padding: '10px' }"
      :header-style="{ padding: '10px' }"
    >
      <Chart :loading="loading" :options="options7" style="width: 100%; min-height: 600px" />
    </n-card>
  </div>
</template>

<script lang="ts" setup>
  import { onMounted, reactive, ref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { UsersMap } from '@/api/echarts/UsersMap';
  import Chart from '@/views/echarts/adv/components/Chart.vue';
  import { timestampToDate } from '@/utils/dateUtil';
  import { useUserStoreWidthOut } from '@/store/modules/user';

  let options3 = ref({});

  const useUserStore = useUserStoreWidthOut();
  const params = ref<any>({
    pageSize: 10,
    day: timestampToDate(new Date() - 86400000 * 30),
    endDay: timestampToDate(new Date()),
    channel_id: '',
    server_id: 1,
  });

  const schemas: FormSchema[] = [
    {
      field: 'server_id',
      component: 'NSelect',
      label: '服务器',
      componentProps: {
        placeholder: '请选择服务器',
        defaultValue: params.value.server_id,
        options: useUserStore.config.server_simple_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: 'createtime',
      component: 'NDatePicker',
      label: '选择日期',
      componentProps: {
        type: 'daterange',
        clearable: true,
        defaultValue: [new Date() - 86400000 * 30, new Date()],
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.day = timestampToDate(e[0]);
          params.value.endDay = timestampToDate(e[1]);
        },
      },
    },
  ];

  const loading = ref(false);
  const message = useMessage();
  const formParams = reactive({
    day: '',
    endDay: '',
    channel_id: '',
  });

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

  const loadDataTable = async (res) => {
    new Promise((_resolve, _reject) => {
      UsersMap({ ...formParams, ...params.value, ...res })
        .then((res) => {
          draw(res);
          loadPie(res);
        })
        .catch((error) => {
          message.error(error.toString());
        });
    });
  };
  onMounted(async () => {
    await loadDataTable({});
  });

  function reloadTable() {
    loadDataTable({});
  }

  const legendData = ref([
    '1个角色',
    '2个角色',
    '3个角色',
    '4个角色',
    '5个角色',
    '6个角色',
    '7个角色',
    '8个角色',
    '9个角色',
    '10个角色',
    '11个角色',
    '12个角色',
    '13个角色',
    '14个角色',
  ]);

  function draw(values) {
    if (values == undefined) {
      values = {};
    }

    options3.value = {
      tooltip: {
        trigger: 'axis',
      },
      legend: {
        data: legendData.value,
        selected: {
          '1个角色': false, // 默认隐藏
        },
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true,
      },
      toolbox: {
        show: true,
        feature: {
          dataView: { readOnly: false },
          magicType: { type: ['line', 'bar'] },
          saveAsImage: {},
        },
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: values.days, //reverse(),
      },
      yAxis: {
        type: 'value',
      },
      series: [
        {
          name: '1个角色',
          type: 'line',
          data: values.info.player1,
        },
        {
          name: '2个角色',
          type: 'line',
          data: values.info.player2,
        },
        {
          name: '3个角色',
          type: 'line',
          data: values.info.player3,
        },
        {
          name: '4个角色',
          type: 'line',
          data: values.info.player4,
        },
        {
          name: '5个角色',
          type: 'line',
          data: values.info.player5,
        },
        {
          name: '6个角色',
          type: 'line',
          data: values.info.player6,
        },
        {
          name: '7个角色',
          type: 'line',
          data: values.info.player7,
        },
        {
          name: '8个角色',
          type: 'line',
          data: values.info.player8,
        },
        {
          name: '9个角色',
          type: 'line',
          data: values.info.player9,
        },
        {
          name: '10个角色',
          type: 'line',
          data: values.info.player10,
        },
        {
          name: '11个角色',
          type: 'line',
          data: values.info.player11,
        },
        {
          name: '12个角色',
          type: 'line',
          data: values.info.player12,
        },
        {
          name: '13个角色',
          type: 'line',
          data: values.info.player13,
        },
        {
          name: '14个角色',
          type: 'line',
          data: values.info.player14,
        },
      ],
    };
  }

  function loadPie(values) {
    if (values == undefined) {
      values = {};
    }

    let data: any = [];
    data.push({ value: values.info.player1.pop(), name: '1个角色' });
    data.push({ value: values.info.player2.pop(), name: '2个角色' });
    data.push({ value: values.info.player3.pop(), name: '3个角色' });
    data.push({ value: values.info.player4.pop(), name: '4个角色' });
    data.push({ value: values.info.player5.pop(), name: '5个角色' });
    data.push({ value: values.info.player6.pop(), name: '6个角色' });
    data.push({ value: values.info.player7.pop(), name: '7个角色' });
    data.push({ value: values.info.player8.pop(), name: '8个角色' });
    data.push({ value: values.info.player9.pop(), name: '9个角色' });
    data.push({ value: values.info.player10.pop(), name: '10个角色' });
    data.push({ value: values.info.player11.pop(), name: '11个角色' });
    data.push({ value: values.info.player12.pop(), name: '12个角色' });
    data.push({ value: values.info.player13.pop(), name: '13个角色' });
    data.push({ value: values.info.player14.pop(), name: '14个角色' });
    options7.value = {
      series: [
        {
          name: '玩家占比',
          type: 'pie',
          radius: ['40%', '70%'],
          data: data,
          emphasis: {
            label: {
              show: true,
              fontSize: '20',
              fontWeight: 'bold',
            },
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)',
            },
          },
        },
      ],
      tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b} : {c} ({d}%)',
      },
      legend: {
        data: legendData.value,
        selected: {
          '1个角色': false, // 默认隐藏
        },
      },
      toolbox: {
        show: true,
        feature: {
          saveAsImage: { show: true },
        },
      },
      grid: {
        x: '15%',
        y: '5%',
        x2: '1%',
        y2: '15%',
      },
    };
  }

  const options7 = ref({});

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

  function handleReset(values: Recordable) {
    console.log(values);
    params.value = {
      pageSize: 10,
      day: timestampToDate(new Date() - 86400000 * 30),
      endDay: timestampToDate(new Date()),
      channel_id: '',
      server_id: 1,
    };
    reloadTable();
  }
</script>

<style scoped></style>
