<template>
<div class="grainPriceCollectCitySum pageContent">
<a-space class="search">
		库存点名称:<a-select
		v-model:value="SearchForm.orgId"
		style="width:200px"
	>
		<a-select-option v-for="item in OrgOptions" :key="item.id" :value="item.id">
			{{ item.name }}
		</a-select-option>
	</a-select>
	汇总日期：
	<a-space direction="vertical" :size="12">
		<a-range-picker v-model:value="SearchForm.times" />
	</a-space>
	<div class="btnBox">
		<a-button type="primary" @click="onSearch"> 查询 </a-button>
		<a-button @click="onReset"> 重置 </a-button>
		<a-button class="pub" @click="onSum"> 汇总 </a-button>
	</div>
</a-space>
<div class="list">
	<a-table :dataSource="tableData" :pagination="tablePage" :columns="tableColumns" @change="onChangePage">
		<template #bodyCell="{ column, record,index }">
			<template v-if="column.key === 'INDEX'">{{index+1}}</template>
			<template v-if="column.key === 'status'">
				<a-tag :color="statusKeyVal[record.status].color">{{statusKeyVal[record.status].label}}</a-tag>
			</template>
			<template v-else-if="column.key === 'CZ'">
				<a-button class="view" @click="onViewRow(record)">查看</a-button>
				<a-button class="reject" @click="onRejectRow(record)">驳回</a-button>
				<a-button class="pub" @click="onPubRow(record)">上报</a-button>
			</template>
		</template>
	</a-table>
</div>
<a-modal v-model:visible="AuditDialog.open" :title="AuditDialog.title" @ok="onDialogOkAudit">
	<a-textarea :auto-size="{minRows:2}" v-model:value="AuditDialog.form.AuditRejectAdvice" />
</a-modal>
</div>
</template>

<script setup name="grainPriceCollectCitySum">
import { message } from 'ant-design-vue';
import unReporCityApi from '@/api/basic.info/unReporCityApi.js'
import grainPriceCollectCitySumApi from '@/api/basic.info/grainPriceCollectCitySumApi.js'
import { useRouter } from "vue-router";
import { globalStore } from '@/store'
const global_store = globalStore()
const userInfo = computed(() => {
	return global_store.userInfo
})
const router = useRouter();
const statusKeyVal = {
	0:{label:'未上报',color:'cyan'},
	1:{label:'已上报',color:'green'},
	2:{label:'已驳回',color:'red'},
}
const tableColumns = [
	{title:'序号',key:'INDEX'},
	{title:'市州名称',dataIndex:'sysName'},
	{title:'汇总人',dataIndex:'userName'},
	{title:'汇总时间',dataIndex:'sumTime'},
	{title:'上报',key:'status'},
	{title:'操作',key:'CZ'},
];
const OrgOptions = ref([]);
const tablePage = reactive({
	total:0,
	current:1,
	size:20,
});
const SearchForm = reactive({
	orgId:'',
	times:[],
});
const AuditDialog = reactive({
	row:undefined,
	open:false,
	title:'',
	form:{
		AuditRejectAdvice:'',
	}
});

onMounted(()=>{
	getSiteDate();
	onSearch();
});
const tableData = ref([]);
function getSiteDate(){
	unReporCityApi.unReporCityquery().then((res) => {
		OrgOptions.value = res
	})
}
function onSearch(){
	let fd = JSON.parse(JSON.stringify(SearchForm));
	fd.current = tablePage.current;
	fd.size = tablePage.size;
	const times = fd.times;
	fd.times = null;
	if(times[0]){ fd.sumStartTime = times[0]; }
	if(times[1]){ fd.sumEndTime = times[1]; }
	grainPriceCollectCitySumApi.grainPriceCollectCitySumPage(fd).then(reg=>{
		tableData.value = reg.records;
		tablePage.total = reg.total;
	});
}
function onReset(){
	SearchForm.orgId = '';
	SearchForm.times = [];
	tablePage.current = 1;
	onSearch();
}
// 汇总
function onSum(){
	let orgId = userInfo.value.orgId;
	let userId = userInfo.value.id;
	grainPriceCollectCitySumApi.grainPriceCollectCitySumSum({orgId,userId}).then(reg=>{
		message.success('汇总成功！');
	})
}
function onViewRow(row){
	router.push({path:'/basic.info/grainPriceCollectCitySumDetail',query:{id:row.id}})
}
function onRejectRow(row){
	AuditDialog.row = row;
	AuditDialog.title = '驳回';
	AuditDialog.open = true;
}
function onDialogOkAudit(){
	let fd = Object.assign({},AuditDialog.form);
	fd.id = AuditDialog.row.id;
	fd.auditStatus = 2;
	grainPriceCollectCitySumApi.grainPriceCollectCitySumCollectCityUpload(fd).then(reg=>{
		message.success('驳回成功！');
	})
}
function onPubRow(row){
	grainPriceCollectCitySumApi.grainPriceCollectCitySumCollectCityUpload({id:row.id}).then(reg=>{
		message.success('上报成功！');
	})
}
function onChangePage(ev){
	console.log(ev);
	Object.assign(tablePage,ev);
	onSearch();
}
</script>
<style lang="less" scoped>
.view { background-color: rgba(112, 125, 233); color: #fff; }
.reject { background-color: rgba(255, 70, 70); color: #fff; }
.pub { background-color: rgba(83, 184, 36); color: #fff; }
.pageContent{
	background:#fff; padding:10px 20px;
	.search{
		padding:10px 0;
		.btnBox{
			display:flex;
			>*{ margin-right:10px; }
		}
	}
}
</style>