<template>
	<div>
		<SearchBox ref="SearchBoxRef" formName="query" :formData="query" @PopupNo="PopupNo" @reset="reset">
		</SearchBox>

		<ButtonBar @search="handleSearch" @downloadProcessHistory="downloadProcessHistory"></ButtonBar>

		<el-card :body-style="cardBodyStyle">
			<el-tabs v-model="activeNameTop" type="border-card">
				<el-tab-pane label="合同审批" name="master">
					<el-table :data="tableDataMaster" border highlight-current-row
						@selection-change="handleSelectionChange" @row-click="rowClick">
						<el-table-column type="selection" fixed></el-table-column>
						<el-table-column type="index" label="序号" align="center" width="60" fixed>
							<template #default="scope">
								<span>{{ scope.$index + (currentPage - 1) * pageSize + 1 }}</span>
							</template>
						</el-table-column>

						<el-table-column v-if="true" prop="contrNo" label="合同编号" width="150" header-align="center"
							align="center" show-overflow-tooltip fixed>
						</el-table-column>
						<el-table-column v-if="true" prop="contrNm" label="合同名称" width="180" header-align="center"
							align="center" show-overflow-tooltip fixed>
						</el-table-column>
						<el-table-column v-if="true" prop="supplierNm" label="交易方名称" width="150" header-align="center"
							align="center" show-overflow-tooltip>
						</el-table-column>
						<el-table-column v-if="true" prop="contrTy" label="合同类型" width="100" header-align="center"
							align="center" show-overflow-tooltip :formatter="formatter">
						</el-table-column>
						<el-table-column v-if="true" prop="conSts" label="合同状态" width="120" header-align="center"
							align="center" show-overflow-tooltip :formatter="formatter">
						</el-table-column>
						<el-table-column v-if="true" prop="khContrNo" label="对方合同编号" width="150" header-align="center"
							align="center" show-overflow-tooltip>
						</el-table-column>
						<el-table-column v-if="true" prop="approveDt" label="审批结束时间" width="150" header-align="center"
							align="center" show-overflow-tooltip>
						</el-table-column>
						<el-table-column v-if="true" prop="delvDate" label="预计交货日期" width="120" header-align="center"
							align="center" show-overflow-tooltip>
						</el-table-column>
						<el-table-column v-if="true" prop="invMth" label="结算方式" width="100" header-align="center"
							align="center" show-overflow-tooltip :formatter="formatter">
						</el-table-column>
						<el-table-column v-if="true" prop="analyDataAccord" label="化验结算依据" width="120"
							header-align="center" align="center" show-overflow-tooltip :formatter="formatter">
						</el-table-column>
						<el-table-column v-if="true" prop="weightDataAccord" label="计量结算依据" width="120"
							header-align="center" align="center" show-overflow-tooltip :formatter="formatter">
						</el-table-column>
						<el-table-column v-if="true" prop="priceDcnMth" label="定价方式" width="100" header-align="center"
							align="center" show-overflow-tooltip :formatter="formatter">
						</el-table-column>
						<el-table-column v-if="true" prop="delvMth" label="交货方式" width="100" header-align="center"
							align="center" show-overflow-tooltip :formatter="formatter">
						</el-table-column>
						<el-table-column v-if="true" prop="delvPlace" label="交货地点" width="120" header-align="center"
							align="center" show-overflow-tooltip>
						</el-table-column>
						<el-table-column v-if="true" prop="ifAnnual" label="是否长协" width="100" header-align="center"
							align="center" show-overflow-tooltip :formatter="formatter">
						</el-table-column>
						<el-table-column v-if="true" prop="ifBid" label="是否招投标" width="100" header-align="center"
							align="center" show-overflow-tooltip :formatter="formatter">
						</el-table-column>
						<el-table-column v-if="false" prop="salMth" label="销售方式" width="100" header-align="center"
							align="center" show-overflow-tooltip :formatter="formatter">
						</el-table-column>
						<el-table-column v-if="true" prop="signReqNo" label="签约请示" width="120" header-align="center"
							align="center" show-overflow-tooltip>
						</el-table-column>
						<el-table-column v-if="true" prop="totalAmt" label="合同金额" width="100" header-align="center"
							align="right" show-overflow-tooltip>
						</el-table-column>
						<el-table-column v-if="true" prop="qty" label="重量" width="120" header-align="center"
							align="right" show-overflow-tooltip>
						</el-table-column>
						<el-table-column v-if="true" prop="contrReq" label="合同要求" width="100" header-align="center"
							align="center" show-overflow-tooltip>
						</el-table-column>
						<el-table-column v-if="true" prop="contrDt" label="合同日期" width="120" header-align="center"
							align="center" show-overflow-tooltip>
						</el-table-column>
						<el-table-column v-if="true" prop="checkYn" label="争议所在地法院" width="150" header-align="center"
							align="center" show-overflow-tooltip :formatter="formatter">
						</el-table-column>
						<el-table-column v-if="true" prop="riskInfo" label="风险提示" width="100" header-align="center"
							align="center" show-overflow-tooltip>
						</el-table-column>
						<el-table-column v-if="true" prop="tempNm" label="合同模板名称" width="120" header-align="center"
							align="center" show-overflow-tooltip>
						</el-table-column>
						<el-table-column v-if="true" prop="addTime" label="录入日期" width="120" header-align="center"
							align="center" show-overflow-tooltip :formatter="formatter">
						</el-table-column>
						<el-table-column v-if="true" prop="addName" label="录入人" width="100" header-align="center"
							align="center" show-overflow-tooltip>
						</el-table-column>
						<el-table-column label="操作" header-align="center" align="center" width="200px" fixed="right">
							<template #default="{ row }">
								<el-button plain type="primary" @click="handleDetail(row)"
									v-if="row.workId !== ''">详情</el-button>
							</template>
						</el-table-column>
					</el-table>
				</el-tab-pane>
			</el-tabs>
		</el-card>

		<el-card :body-style="cardBodyStyle">
			<el-pagination :current-page="currentPage" :page-size="pageSize" :page-sizes="pageSizes" :total="totalRows"
				background="background" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
				@current-change="handleCurrentChange">
			</el-pagination>
		</el-card>

		<el-tabs v-model="activeNameBottom" type="border-card">
			<el-tab-pane label="合同审批记录" name="first">
				<el-card :body-style="cardBodyStyle">
					<el-table :data="tableDataHistory" border highlight-current-row>
						<el-table-column prop="actionname" label="操作名称" width="200" header-align="center" align="center"
							show-overflow-tooltip>
						</el-table-column>
						<el-table-column prop="actiontime" label="操作时间" width="200" header-align="center" align="center"
							show-overflow-tooltip>
						</el-table-column>
						<el-table-column prop="comments" label="审批意见" width="200" header-align="center" align="center"
							show-overflow-tooltip>
						</el-table-column>
						<el-table-column prop="nodename" label="节点名称" width="200" header-align="center" align="center"
							show-overflow-tooltip>
						</el-table-column>
						<el-table-column prop="username" label="用户名" width="200" header-align="center" align="center"
							show-overflow-tooltip>
						</el-table-column>
						<el-table-column prop="memo" label="备注" header-align="center" align="center"
							show-overflow-tooltip>
						</el-table-column>
					</el-table>
				</el-card>
			</el-tab-pane>
		</el-tabs>

	</div>
</template>

<script setup>
import ButtonBar from '@/components/ButtonBar.vue'

import {
	ref,
	reactive,
	onMounted,
	getCurrentInstance
} from 'vue'

const {
	proxy
} = getCurrentInstance()

import tools from '@/tools'
const cardBodyStyle = ref(tools.style.card)
const inputStyle = ref(tools.style.input)


import Attachment from '@/components/Attachment.vue'
import { ElMessage } from 'element-plus';


onMounted(() => {
	// getMenuTable()
	getDictList()
	handleSearch()
})


/**
 * 查询公司代码
 */
const userId = window.localStorage.getItem('userId')
const userCode = window.localStorage.getItem('userCode')
const userName = window.localStorage.getItem('userDesc')
const dept = JSON.parse(window.localStorage.getItem('dept'))
const deptTree = JSON.parse(window.localStorage.getItem('deptTree'))
/**
 * 查询公司代码
 */


/**
 * 查询表格字段
 */
// const column = ref([])
// const getMenuTable = () => {
// 	var url = 'framework/MenuTable/queryMenuTable'
// 	var param = {
// 		menuId: localStorage.getItem('menuId'),
// 		tName: 'tableDate'
// 	}
// 	proxy.$axios.post(url, param).then(response => {
// 		if (response.data.code == '0') {
// 			column.value = response.data.data
// 		} else {
// 			ElMessage({
// 				type: 'error',
// 				message: '操作失败' + response.data.msg,
// 			});
// 		}
// 	});
// }
/**
 * 查询表格
 */

/**
 *  查询字典
 */
const activeNameTop = ref('master')
const activeNameBottom = ref('first')
const dicList = ref({
	CONTRACT_TYPE: [],
	contrSts: [],
	SAL_INV_MTH: [],
	PRICE_DCN_MTH: [],
	DELV_MTH: [],
	WEIGHT_DATA_ACCORD: [],
	ANALY_DATA_ACCORD: [],
	SAL_SAL_MTH: [],
	COM_YN: [],
	CHECK_YN: []
})
const getDictList = () => {
	var url = 'framework/Common/querySelectorList'
	var param = {
		type: JSON.stringify(['CONTRACT_TYPE', 'contrSts', 'SAL_INV_MTH', 'PRICE_DCN_MTH', 'DELV_MTH', 'WEIGHT_DATA_ACCORD', 'ANALY_DATA_ACCORD', 'SAL_SAL_MTH', 'COM_YN', 'CHECK_YN'])
	}
	proxy.$axios.get(url, param).then(response => {
		if (response.data.code == '0') {
			dicList.value = response.data.data
		} else {
			ElMessage.error('操作失败！' + response.data.msg)
		}
	})
}
const formatter = (row, column, cellValue, index) => {
	if (!cellValue || cellValue == null || cellValue == '') {
		return cellValue
	}
	switch (column.property) {
		case 'contrTy':
			cellValue = dicList.value.CONTRACT_TYPE.find(item => item.dicCode == cellValue)?.dicName
			break
		case 'conSts':
			cellValue = dicList.value.contrSts.find(item => item.dicCode == cellValue)?.dicName
			break
		case 'invMth':
			cellValue = dicList.value.SAL_INV_MTH.find(item => item.dicCode == cellValue)?.dicName
			break
		case 'priceDcnMth':
			cellValue = dicList.value.PRICE_DCN_MTH.find(item => item.dicCode == cellValue)?.dicName
			break
		case 'delvMth':
			cellValue = dicList.value.DELV_MTH.find(item => item.dicCode == cellValue)?.dicName
			break
		case 'weightDataAccord':
			cellValue = dicList.value.WEIGHT_DATA_ACCORD.find(item => item.dicCode == cellValue)?.dicName
			break
		case 'analyDataAccord':
			cellValue = dicList.value.ANALY_DATA_ACCORD.find(item => item.dicCode == cellValue)?.dicName
			break
		case 'salMth':
			cellValue = dicList.value.SAL_SAL_MTH.find(item => item.dicCode == cellValue)?.dicName
			break
		case 'ifAnnual':
			cellValue = dicList.value.COM_YN.find(item => item.dicCode == cellValue)?.dicName
			break
		case 'ifBid':
			cellValue = dicList.value.COM_YN.find(item => item.dicCode == cellValue)?.dicName
			break
		case 'checkYn':
			cellValue = dicList.value.CHECK_YN.find(item => item.dicCode == cellValue)?.dicName
			break
		case 'addTime':
			cellValue = cellValue.split(' ')[0]
			break
	}

	return cellValue
}
/**
 *  查询字典
 */


/**
 *   查询
 */
const query = ref({
	contrTy: '',
	contrNo: '',
	contrNm: '',
	khContrNo: '',
	supplierNm: '',
	contrDt: '',
	conSts: ['B']
})
const tableDataMaster = ref([])
const handleSearch = () => {
	tableDataHistory.value = []
	currentPage.value = 1
	gettableDataMaster()
}

const gettableDataMaster = () => {
	var url = 'cot/Head/query'
	var param = {
		page: currentPage.value,
		rows: pageSize.value,
		params: JSON.stringify(query.value)
	}
	proxy.$axios.get(url, param).then(response => {
		if (response.data.code == '0') {
			tableDataMaster.value = response.data.data.records
			totalRows.value = response.data.data.total
		} else {
			ElMessage.error('操作失败！' + response.data.msg)
		}
	})
}
/**
 *   查询
 */


/**
 * 分页
 */
const currentPage = ref(1)
const totalRows = ref(0)
const pageSizes = ref([5, 10, 20, 30, 40])
const pageSize = ref(5)
const handleSizeChange = (val) => {
	pageSize.value = val
	gettableDataMaster()
}
const handleCurrentChange = (val) => {
	currentPage.value = val
	gettableDataMaster()
}
/**
 * 分页
 */



/**
 * 保存、修改、删除
 */

const multipleSelection = ref([])
const handleSelectionChange = (val) => {
	multipleSelection.value = val
}

const rowClick = (row) => {
	getHistory(row)
}
const tableDataHistory = ref([])
const getHistory = (row) => {

	var url = 'workflow/process/history'
	var param = {
		workId: row.workId
	}
	proxy.$axios.post(url, param).then(response => {
		if (response.data.code == '0') {
			tableDataHistory.value = response.data.data.records
		} else {
			ElMessage.error('操作失败！' + response.data.msg)
		}
	})
}

const downloadProcessHistory = () => {
	if (multipleSelection.value.length != 1) {
		ElMessage({
			type: 'warning',
			message: '请选择一条数据打印！'
		})
		return
	}

	location.href = import.meta.env.VITE_BASE_API +
					'/cot/Head/downloadProcessHistory?userId=' + localStorage.getItem('userId') + 
					'&id=' + encodeURIComponent(multipleSelection.value[0].id)
}
/**
 * 工作流相关
 */
const flowId = ref('')
const handleDetail = (row) => {
	switch (row.contrTy) {
		case '10':
			flowId.value = 'salContract'
			break
		case '20':
			flowId.value = 'purContract'
			break
		case '30':
			flowId.value = 'serviceContract'
			break
	}

	if (row.workId === null) {
		ElMessage({
			type: 'info',
			message: '无法查看',
		})
	} else {

		sessionStorage.setItem('workflowQuery', JSON.stringify({
			flowId: flowId.value,
			workId: row.workId,
			trackId: row.trackId,
			date: new Date().getTime()
		}))

		proxy.$router.push({
			path: '/workflow/detail',
		})
	}
}
</script>


<style></style>