<template>
	<div class="statistics-order-detail-accept-type-list-container layout-padding">
    <div class="layout-padding-auto layout-padding-view pd20">
      <div class="table-search-content">
        <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 label-width="0">
            <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-PieChart" class="mr5" /> 图表 </el-button>
          </el-form-item>
        </el-form>
      </div>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-grid v-bind="gridOptions"> </vxe-grid>
			</div>
		</div>
	</div>
</template>
<script setup lang="tsx" name="statisticsOrderDetailAcceptTypeList">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';
import { statisticsOrderAcceptList, statisticsOrderAcceptListExport } from '@/api/statistics/order';
import { useRouter } from 'vue-router';
import { callPeriodBase } from '@/api/statistics/call';
import XEUtils from 'xe-utils';

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,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				queryList();
			},
		},
		tools: [{ toolRender: { name: 'exportAll' } }],
	},
	customConfig: {
		storage: true,
	},
	showFooter: true,
	footerMethod: ({ columns, data }) => {
		return [
			columns.map((column: any, columnIndex: number) => {
				if (columnIndex === 0) {
					return '合计';
				}
				return XEUtils.sum(data, column.property);
			}),
		];
	},
	id: 'statisticsOrderDetailAcceptTypeList',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [],
	data: [],
	params: {
		exportMethod: statisticsOrderAcceptListExport,
		exportParams: requestParams,
		isSpecialExport: true,
	},
});
/** 获取列表 */
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 statisticsOrderAcceptList(requestParams.value);
		let columns = result.acceptType?.map((item: any) => {
			return {
				prop: item.dicDataValue,
				label: item.dicDataName,
				align: 'center',
				field: item.dicDataValue,
				title: item.dicDataName,
				sortable: true,
				minWidth: 80,
				render: (scope) => {
					return (
						<el-button type="primary" link onClick={() => linkDetail(scope.row, scope)}>
							{scope.row[item.dicDataValue] ? scope.row[item.dicDataValue] : 0}
						</el-button>
					);
				},
				slots: {
					default (scope:any) {
						return (
							<el-button type="primary" onClick={() => linkDetail(scope.row, scope)} link>
								{scope.row[item.dicDataValue] ? scope.row[item.dicDataValue] : 0}
							</el-button>
						)
					}
				}
			};
		});
		// 计算横轴小计
		const tableData = result?.data.map((item: any) => {
			let subtotal = 0;
			for (let i of result.acceptType) {
				const itemI = item[i.dicDataValue] ?? 0;
				subtotal += itemI;
			}
			return {
				...item,
				subtotal,
			};
		});
		columns.unshift(
			{
				prop: 'Date',
				label: '日期',
				field: 'Date',
				title: '日期',
				align: 'center',
				minWidth: 100,
				fixed: 'left',
			},
			{
				prop: 'subtotal',
				label: '小计',
				align: 'center',
				field: 'subtotal',
				title: '小计',
				minWidth: 100,
				sortable: true,
			}
		);
		gridOptions.data = tableData;
		gridOptions.columns = columns;

		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 router = useRouter();
const onDetail = () => {
	router.push({
		name: 'statisticsOrderAcceptType',
	});
};
// 获取基础信息
const getBaseInfo = async () => {
	try {
		const { result } = await callPeriodBase();
		state.callForwardingSource = result.callForwardingSource ?? [];
	} catch (e) {
		console.log(e);
	}
};
// 查看明细
const linkDetail = (row: any, scope: any) => {
	router.push({
		path: '/statistics/order/detailAcceptType',
		query: {
			Date: row.Date,
			AcceptTypeCode: scope.column.property,
		},
	});
};
onMounted(() => {
	getBaseInfo();
	queryList();
});
</script>