<template>
	<div class="statistics-center-seat-satisfaction-container layout-pd">
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="gridOptions.loading">
				<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>
					<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="20">
				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
					<el-row :gutter="10">
						<el-col :xs="24" :sm="24" :md="18" :lg="18" :xl="18">
							<v-chart class="chart" :option="option" :loading="state.loading" autoresize />
						</el-col>
						<el-col :xs="24" :sm="24" :md="6" :lg="6" :xl="6">
							<div style="height: 70vh">
								<vxe-grid v-bind="gridOptions"> </vxe-grid>
							</div>
						</el-col>
					</el-row>
				</el-col>
				<el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<div style="height: 70vh">
						<v-chart class="chart1" :option="option1" :loading="state.loading" autoresize />
					</div>
				</el-col>
			</el-row>
		</el-card>
	</div>
</template>
<script setup lang="tsx" name="statisticsCenterSeatSatisfaction">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';
import { useRouter } from 'vue-router';
import { centerSatisfaction, centerSatisfactionExport, centerSatisfactionRate } from '@/api/statistics/center';

const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		crTime: defaultDate, //
		StartTime: null,
		EndTime: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
/** 搜索按钮操作 */
const handleQuery = () => {
	queryList();
};

const requestParams = ref<EmptyObjectType>({});
const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	printConfig: {},
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	toolbarConfig: {
		refresh: {
			queryMethod: () => {
				queryList();
			},
		},
		tools: [{ toolRender: { name: 'exportAll' } }],
	},
	customConfig: {
		storage: true,
	},
	id: 'statisticsCenterSeatSatisfaction',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	align: 'center',
	columns: [
		{ field: 'seatSatisfactionName', title: '-' },
		{ field: 'count', title: '数量' },
	],
	data: [],
	params: {
		exportMethod: centerSatisfactionExport,
		exportParams: requestParams,
	},
});
/** 获取列表 */
const dataTable = ref([]);
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 [res, res1] = await Promise.all([centerSatisfaction(requestParams.value), centerSatisfactionRate(requestParams.value)]);
		state.tableData = res.result;
		gridOptions.data = res.result;
		const legendData = state.tableData.map((item: any) => {
			return item.seatSatisfactionName;
		});
		dataTable.value = state.tableData.map((item: any) => {
			return {
				name: item.seatSatisfactionName,
				value: item.count,
				...item,
			};
		});
		setOption(legendData, dataTable.value);
		const data = [
			{
				value: res1.result?.satisfactionRate,
				name: '满意率',
			},
		];
		setOption1(data);
		state.loading = false;
		gridOptions.loading = false;
	} catch (e) {
		console.log(e);
		state.loading = false;
		gridOptions.loading = false;
	}
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
const option = ref<EmptyObjectType>({});
// 坐席总体满意度分析图
const setOption = (legendData: string[], data: any) => {
	option.value = {
		title: {
			text: '坐席总体满意度分析图',
			left: 'center',
		},
		tooltip: {
			formatter: '{b0}: {c0} ({d}%)',
		},
		legend: [
			{
				left: 'left',
				top: '40',
				orient: 'vertical',
				data: legendData,
			},
		],
		series: [
			{
				type: 'pie',
				radius: ['0%', '60%'],
				top: '20%',
				label: {
					show: true,
					overflow: 'none',
					formatter: (params: any) => {
						if (params.name !== '') {
							return `${params.name}:${params.data.value}（${params.percent}%）`;
						}
					},
				},
				data: data,
			},
		],
	};
};
// 坐席总体满意度分析图
const option1 = ref<EmptyObjectType>({});
const setOption1 = (data: any) => {
	option1.value = {
		tooltip: {
			formatter: '{b} : {c}%',
		},
		title: {
			text: '坐席总体满意率分析',
			left: 'center',
			top: '3%',
		},
		series: [
			{
				name: '',
				type: 'gauge',
				axisLine: {
					width: 10,
					show: true,
					lineStyle: {
						shadowBlur: 0,
						color: [
							[0.2, '#92C7AE'],
							[0.8, '#64869D'],
							[1, '#C13335'],
						],
					},
				},
				title: {
					//标题
					show: true,
					offsetCenter: [0, '-26%'], // x, y，单位px
					fontSize: 24, //表盘上的标题文字大小
				},
				detail: {
					formatter: '{value}%',
					offsetCenter: [0, '50%'],
					fontSize: 35,
				},
				data,
			},
		],
	};
};

// 查看列表明细
const router = useRouter();
const onDetail = () => {
	router.push({
		path: '/statistics/center/detailSeatSatisfactionList',
	});
};
onMounted(() => {
	queryList();
});
</script>
<style lang="scss" scoped>
.chart {
	height: 70vh;
	margin-top: 10px;
}
</style>
