<template>
  <div>
    <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 :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
        :loading="loading"
        :columns="columns"
        :dataSource="dataSource"
        :row-key="(row) => row.id"
        ref="actionRef"
        @update:checked-row-keys="onCheckedRow"
        :scroll-x="1090"
        :resizeHeightOffset="-200000"
      />
    </n-card>
  </div>
</template>

<script lang="ts" setup>
import {h, onMounted, reactive, ref} from 'vue';
import {NTag, useMessage} from 'naive-ui';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { LevelOutputLog } from '@/api/echarts/Duel';
  import Chart from '@/views/echarts/adv/components/Chart.vue';
  import { reverse } from '@/utils/array';
  import { useUserStoreWidthOut } from '@/store/modules/user';
import {UtilsTool} from "@/utils/macd/UtilsTool";
  const useUserStore = useUserStoreWidthOut();
  let options3 = ref({});

  const columns = [
    {
      title: '等级',
      dataIndex: 'Level',
      key: 'Level',
      width: 120,
    },
    {
      title: '金币',
      dataIndex: 'Coin',
      key: 'Coin',
      width: 150,
      render(row) {
        return UtilsTool.unitConversion(row.Coin);
      },
    },
    {
      title: '钻石',
      dataIndex: 'Diamond',
      key: 'Coin',
      width: 150,
      render(row) {
        return UtilsTool.unitConversion(row.Diamond);
      },
    },
  ];

  const dataSource = ref([]);

  const params = ref<any>({
    pageSize: 10,
    startLevel: 1,
    endLevel: 50,
    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: 'level',
      component: 'NInput',
      label: '玩家等级',
      componentProps: {
        pair: true,
        separator: '~',
        clearable: true,
        placeholder: ['从', '到'],
        defaultValue: [params.value.startLevel, params.value.endLevel],
        onInput: (e: any) => {
          console.log(e);
          params.value.startLevel = parseInt(e[0]);
          params.value.endLevel = parseInt(e[1]);
        },
      },
    },
  ];

  const loading = ref(false);
  const message = useMessage();
  const actionRef = ref();
  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) => {
    loading.value = true;
    new Promise((_resolve, _reject) => {
      LevelOutputLog({ ...formParams, ...params.value, ...res })
        .then((res) => {
          console.log(JSON.stringify(res));
          if (res.rows === null || res.rows === undefined) {
            res.rows = [];
          }
          dataSource.value = res.rows;
          draw(res.info);
          loading.value = false;
        })
        .catch((error) => {
          message.error(error.toString());
          loading.value = false;
        });
    });
  };

  onMounted(async () => {
    await loadDataTable({});
  });

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

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

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

    options3.value = {
      tooltip: {
        trigger: 'axis',
      },
      legend: {
        data: ['金币', '钻石'],
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true,
      },
      toolbox: {
        feature: {
          saveAsImage: {},
        },
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: reverse(values.Level),
      },
      yAxis: {
        type: 'value',
      },
      series: [
        {
          name: '金币',
          type: 'line',
          data: reverse(values.Coin),
        },
        {
          name: '钻石',
          type: 'line',
          data: reverse(values.Diamond),
        },
      ],
    };
  }

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

  function handleReset(values: Recordable) {
    console.log(values);
    params.value = {
      pageSize: 10,
      startLevel: 1,
      endLevel: 50,
      channel_id: '',
      server_id: null,
    };
    reloadTable();
  }
</script>

<style scoped></style>
