<template>
	<div class="statistics-order-accept-type-container layout-pd">
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item prop="crTime">
					<statistical-time v-model="state.queryParams.crTime" @change="handleQuery" ref="statisticalTimeRef" :disabled="state.loading"/>
				</el-form-item>
				<el-form-item label="来电主体" prop="TypeId">
					<el-select v-model="state.queryParams.TypeId" placeholder="请选择来电主体" @change="handleQuery">
            <el-option :value="0" label="全部" />
            <el-option :value="1" label="市民" />
            <el-option :value="2" label="企业" />
					</el-select>
				</el-form-item>
        <el-form-item label="热线号码" prop="Gateway">
        <el-select v-model="state.queryParams.Gateway" placeholder="请选择热线号码" clearable @change="handleQuery">
          <el-option v-for="item in state.callForwardingSource" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
        </el-select>
        </el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" class="default-button" :loading="state.loading">
						<SvgIcon name="ele-Refresh" class="mr5" />重置
					</el-button>
					<el-button type="primary" @click="onDetail" :loading="state.loading"> <SvgIcon name="ele-List" class="mr5" /> 列表明细 </el-button>
				</el-form-item>
			</el-form>
			<el-row :gutter="10">
				<el-col :xs="24" :sm="24" :md="24" :lg="14" :xl="14">
					<v-chart class="chart" :option="option" :loading="state.loading" autoresize />
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="10" :xl="10">
					<div style="height: 70vh">
						<vxe-grid v-bind="gridOptions"> </vxe-grid>
					</div>
				</el-col>
			</el-row>
		</el-card>
	</div>
</template>
<script setup lang="tsx" name="statisticsOrderAcceptType">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';
import { statisticsOrderAccept, statisticsOrderAcceptExport } from '@/api/statistics/order';
import { useRouter } from 'vue-router';
import {callPeriodBase} from "@/api/statistics/call";

const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
    TypeId: 0, // 关键词
    Gateway:null,
		crTime: defaultDate, //
		StartTime: null,
		EndTime: null,
	},
	loading: false, // 加载
	total: 0, // 总数
	callForwardingSource: [],
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
const requestParams = ref<EmptyObjectType>({});
const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	printConfig: {},
	columnConfig: {
		resizable: true,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				queryList();
			},
		},
		tools: [{ toolRender: { name: 'exportAll' } }],
	},
	customConfig: {
		storage: true,
	},
	id: 'statisticsOrderAcceptType',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{ field: 'acceptType', title: '受理类型', align: 'center', minWidth: 90 },
		{ field: 'sumCount', title: '分类量', align: 'center' ,sortable: true},
		{ field: 'completionCount', title: '办结量', align: 'center',sortable: true },
		{ field: 'completionRate', title: '办结率', align: 'center' },
		{ field: 'visitCount', title: '回访量', align: 'center',sortable: true },
		{ field: 'visitRate', title: '回访率', align: 'center' },
		{ field: 'sumCountRate', title: '占比', align: 'center' },
	],
	data: [],
	params: {
		exportMethod: statisticsOrderAcceptExport,
		exportParams: requestParams,
	},
});
/** 获取列表 */
const queryList = async () => {
	state.loading = true;
	gridOptions.loading = true;
	try {
		requestParams.value = Other.deepClone(state.queryParams);
		requestParams.value.StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
		requestParams.value.EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
		Reflect.deleteProperty(requestParams.value, 'crTime');
		const { result } = await statisticsOrderAccept(requestParams.value);
		gridOptions.data = result;
		const legendData = ['分类量', '办结量', '回访量'];
		// 获取acceptType 并且acceptType不等于合计的数组
		const xData = gridOptions.data.filter((item: any) => item.acceptType !== '合计').map((item: any) => item.acceptType);
		const seriesData = [
			{
				name: '分类量',
				type: 'bar',
				stack: 'total',
        barMaxWidth:  30,
        data: gridOptions.data.map((item: any) => {
					return item.sumCount;
				}),
			},
			{
				name: '办结量',
				type: 'bar',
				stack: 'total',
        barMaxWidth:  30,
				data: gridOptions.data.map((item: any) => {
					return item.completionCount;
				}),
			},
			{
				name: '回访量',
				type: 'bar',
				stack: 'total',
        barMaxWidth:  30,
				data: gridOptions.data.map((item: any) => {
					return item.visitCount;
				}),
			},
		];
		setOption(legendData, xData, seriesData);
		state.loading = false;
		gridOptions.loading = false;
	} catch (e) {
		state.loading = false;
		gridOptions.loading = false;
		console.log(e);
	}
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
const option = ref<any>({});
// 信件来源统计
const setOption = (legendData: string[], xData: any, seriesData: any) => {
	option.value = {
		title: {
			text: '受理类型统计',
			left: 'center',
			top: '0',
		},
		tooltip: {
			trigger: 'axis',
			confine: true,
		},
		legend: {
			top: '7%',
			data: legendData,
		},
		grid: {
			left: '0',
			right: '1%',
			bottom: '2%',
			top: '20%',
			containLabel: true,
		},
		xAxis: {
			data: xData,
		},
		yAxis: {
			type: 'value',
		},
		series: seriesData,
		animationEasing: 'elasticOut',
		animationDuration: 2000,
	};
};
// 查看列表明细
const router = useRouter();
const onDetail = () => {
	router.push({
		path: '/statistics/order/detailAcceptTypeList',
	});
};
// 获取基础信息
const getBaseInfo = async () => {
  try {
    const { result } = await callPeriodBase();
    state.callForwardingSource = result.callForwardingSource ?? [];
  } catch (e) {
    console.log(e);
  }
};
onMounted(() => {
	queryList();
  getBaseInfo();
});
</script>
<style lang="scss" scoped>
.chart {
	height: 70vh;
	margin-top: 10px;
}
</style>
