<template>
	<a-card :bordered="false" :body-style="{ 'padding-bottom': '20px' }" class="mb-2">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
						<a-form-item label="日期：" name="grossTime">
							<a-range-picker v-model:value="searchFormState.grossTime" value-format="YYYY-MM-DD" show-date style="width: 100%" />
						</a-form-item>
					</a-col>

				<a-col :span="10">
					<a-button type="primary" html-type="submit" @click="refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a-button type="primary" html-type="submit" @click="exportReport()">导出</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">

			<a-tabs v-model:activeKey="activeKey" @change="clickTab(value)">
				<a-tab-pane key="1">
					<template #tab>
						<span>
						<CarOutlined />
						车辆
						</span>
					</template>
					<div id="printForm1">
						<a-table
							ref="table"
							:columns="columns1"
							:data-source = "data1"
							bordered
							:row-key="(record) => record.id"
							:pagination="false"
						>
						</a-table>
					</div>
				</a-tab-pane>
				<a-tab-pane key="2">
					<template #tab>
						<span>
						<HddOutlined />
						品名
						</span>
					</template>
					<div id="printForm2">
						<a-table
							ref="table"
							:columns="columns2"
							:data-source = "data2"
							bordered
							:row-key="(record) => record.id"
							:pagination="false"
						>
						</a-table>
					</div>
				</a-tab-pane>
			</a-tabs>

	</a-card>

</template>

<script setup name="customerinfo">

	import bizInventoryApi from '@/api/biz/bizInventoryApi'
	import downloadUtil from '@/utils/downloadUtil'
	import { onMounted } from 'vue'
	import moment from 'moment'

	const activeKey = ref('1');

	const searchFormRef = ref()
	let searchFormState = reactive({})

	const data1 = ref([])
	const data2 = ref([])

	const dayTime = ref()
	const zhibiao = ref()
	const fuhe = ref()
	const typeTitle = ref()



	onMounted(() => {
		console.log("12121212")
		dayTime.value = "时间：" + moment(new Date()).format('YYYY-MM-DD')
		zhibiao.value = "制表："
		fuhe.value = "复核："
		if(activeKey.value == '1'){
			typeTitle.value = "车辆日报表"
		}else if(activeKey.value == '2'){
			typeTitle.value = "货品日报表"
		}
		loadData1()
	})

	const table = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns1 = [
		{
			title: '序号',
			dataIndex: 'number',
			align: 'center',
		},
		{
			title: '车辆',
			dataIndex: 'licensePlate',
			align: 'center',
		},
		{
			title: '统计',
			children: [
				{
					title: '车数',
					dataIndex: 'carCount',
					align: 'center',
				},
				{
					title: '出库',
					dataIndex: 'outWeight',
					align: 'center',
				},
				{
					title: '入库',
					dataIndex: 'inWeight',
					align: 'center',
				},

			]
		},
	]

	const columns2 = [
		{
			title: '序号',
			dataIndex: 'number',
			align: 'center',
		},
		{
			title: '品名',
			dataIndex: 'licensePlate',
			align: 'center',
		},
		{
			title: '统计',
			children: [
				{
					title: '次数',
					dataIndex: 'carCount',
					align: 'center',
				},
				{
					title: '出库',
					dataIndex: 'outWeight',
					align: 'center',
				},
				{
					title: '入库',
					dataIndex: 'inWeight',
					align: 'center',
				},
			]
		},
	]

	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}

	//车辆
	const loadData1 = () => {
		console.log("activeKey:"+JSON.stringify(activeKey))
		/*if(searchFormState.queryFlag== '' || searchFormState.queryFlag== null){
			searchFormState.queryFlag = '1'
		}*/
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		// signTime范围查询条件重载
		if (searchFormParam.grossTime) {
			searchFormParam.grossTimeBegin = searchFormParam.grossTime[0]
			searchFormParam.grossTimeEnd = searchFormParam.grossTime[1]
			delete searchFormParam.grossTime
		}
		searchFormParam.queryFlag = '1'
		bizInventoryApi.getReportList(Object.assign(searchFormParam)).then((data) => {
			data1.value = data
			console.log("data1:"+JSON.stringify(data1.value))
		})
	}

	//货品
	const loadData2 = () => {
		/*if(searchFormState.queryFlag== '' || searchFormState.queryFlag== null){
			searchFormState.queryFlag = '2'
		}*/
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		// signTime范围查询条件重载
		if (searchFormParam.grossTime) {
			searchFormParam.grossTimeBegin = searchFormParam.grossTime[0]
			searchFormParam.grossTimeEnd = searchFormParam.grossTime[1]
			delete searchFormParam.grossTime
		}
		searchFormParam.queryFlag = '2'
		bizInventoryApi.getReportList(Object.assign(searchFormParam)).then((data) => {
			data2.value =  data
		})
	}


	//切换
	const clickTab = () => {

		if(activeKey.value == '1'){
			typeTitle.value = "车辆日报表"
		}else if(activeKey.value == '2'){
			typeTitle.value = "货品日报表"
		}

		if(searchFormState.grossTime!="" || searchFormState.grossTime!=null){
			dayTime.value = "时间：" + moment(searchFormState.grossTime).format('YYYY-MM-DD')
		}else{
			dayTime.value = "时间：" + moment(new Date()).format('YYYY-MM-DD')
		}

		console.log("activeKey:"+JSON.stringify(activeKey))
		if(activeKey.value == '1'){
			searchFormState.queryFlag = '1'
			loadData1();

		}else if(activeKey.value == '2'){
			searchFormState.queryFlag = '2'
			loadData2();
		}
	}

	//查询
	const refresh = () => {

		if(searchFormState.grossTime!="" || searchFormState.grossTime!=null){
			dayTime.value = "时间：" + moment(searchFormState.grossTime).format('YYYY-MM-DD')
		}else{
			dayTime.value = "时间：" + moment(new Date()).format('YYYY-MM-DD')
		}

		if(activeKey.value == '1'){
			searchFormState.queryFlag = '1'
			loadData1();

		}else if(activeKey.value == '2'){
			searchFormState.queryFlag = '2'
			loadData2();
		}
	}

	//导出
	const exportReport = () => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		if (searchFormParam.grossTime) {
			searchFormParam.grossTimeBegin = searchFormParam.grossTime[0]
			searchFormParam.grossTimeEnd = searchFormParam.grossTime[1]
			delete searchFormParam.grossTime
		}
		if(activeKey.value == '1'){
			searchFormParam.queryFlag = '1'
			bizInventoryApi.getReportExport(Object.assign(searchFormParam)).then((res) => {
				downloadUtil.resultDownload(res)
			})

		}else if(activeKey.value == '2'){
			searchFormParam.queryFlag = '2'
			bizInventoryApi.getReportExport(Object.assign(searchFormParam)).then((res) => {
				downloadUtil.resultDownload(res)
			})
		}
	}

	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()

	}

	//打印
	const onPrint = () => {
		console.log("activity:"+activeKey.value)
		if(activeKey.value == '1'){
			print(
				document.getElementById('printForm1'),
				{
					title: '车辆日报表',
					subTitle: dayTime.value,
					fields: ['制　表',  '复  核'],
				},
			)
		}else if(activeKey.value == '2'){
			print(
				document.getElementById('printForm2'),
				{
					title: '货品日报表',
					subTitle: dayTime.value,
					fields: ['制　表',  '复  核'],
				},
			)
		}
	}

</script>
