<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"
        :resizeHeightOffset="-3000"
        :pagination="{ pageSize: 15 }"
      />
    </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 { DisconnectList } from '@/api/echarts/disconnect';
  import { timestampToDate } from '@/utils/dateUtil';
  import { useUserStoreWidthOut } from '@/store/modules/user';
  import { defRangeShortcuts } from '@/utils/dateUtil';
  const useUserStore = useUserStoreWidthOut();
  const columns = [
    {
      title: '日期',
      dataIndex: 'date',
      key: 'date',
      width: 120,
    },
    {
      title: '断线人数 / 成功重连',
      dataIndex: 'dis_users',
      key: 'dis_users',
      render: (rows, _) => {
        return rows.dis_users + ' / ' + rows.re_users;
      },
      width: 150,
    },
    {
      title: '断线次数 / 成功重连',
      dataIndex: 'dis_count',
      key: 'dis_count',
      render: (rows, _) => {
        return rows.dis_count + ' / ' + rows.re_count;
      },
      width: 150,
    },
    {
      title: '新手断线人数 / 成功重连',
      dataIndex: 'new_dis_users',
      key: 'new_dis_users',
      render: (rows, _) => {
        return rows.new_dis_users + ' / ' + rows.new_re_users;
      },
      width: 200,
    },
    {
      title: '新手断线次数 / 成功重连',
      dataIndex: 'new_dis_count',
      key: 'new_dis_count',
      render: (rows, _) => {
        return rows.new_dis_count + ' / ' + rows.new_re_count;
      },
      width: 200,
    },
    {
      title: '移动网断线人数 / 成功重连',
      dataIndex: 'mobile_dis_users',
      key: 'mobile_dis_users',
      render: (rows, _) => {
        return rows.mobile_dis_users + ' / ' + rows.mobile_re_users;
      },
      width: 200,
    },
    {
      title: '移动网断线次数 / 成功重连',
      dataIndex: 'mobile_dis_count',
      key: 'mobile_dis_count',
      render: (rows, _) => {
        return rows.mobile_dis_count + ' / ' + rows.mobile_re_count;
      },
      width: 200,
    },
    {
      title: 'wifi断线人数 / 成功重连',
      dataIndex: 'wifi_dis_users',
      key: 'wifi_dis_users',
      render: (rows, _) => {
        return rows.wifi_dis_users + ' / ' + rows.wifi_re_users;
      },
      width: 200,
    },
    {
      title: 'wifi断线次数 / 成功重连',
      dataIndex: 'wifi_dis_count',
      key: 'wifi_dis_count',
      render: (rows, _) => {
        return rows.wifi_dis_count + ' / ' + rows.wifi_re_count;
      },
      width: 200,
    },
  ];

  const params = ref<any>({
    pageSize: 10,
    day: timestampToDate(new Date() - 86400000 * 30),
    endDay: timestampToDate(new Date() - 86400000),
    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: 'createtime',
      component: 'NDatePicker',
      label: '选择日期',
      componentProps: {
        type: 'daterange',
        clearable: true,
        shortcuts: defRangeShortcuts(),
        defaultValue: [new Date() - 86400000 * 30, new Date() - 86400000],
        onUpdateValue: (e: any) => {
          console.log(e);
          params.value.day = timestampToDate(e[0]);
          params.value.endDay = timestampToDate(e[1]);
        },
      },
    },
  ];

  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) => {
    return await DisconnectList({ ...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: 10,
      day: timestampToDate(new Date() - 86400000 * 30),
      endDay: timestampToDate(new Date() - 86400000),
      channel_id: '',
      server_id: null,
    };
    reloadTable();
  }
</script>

<style scoped></style>
