<template>
	<lay-container fluid="true" class="content-box">
		<lay-table height="160px" class="row-select" v-if="dataColumn.length>0" :columns="dataColumn"
			:data-source="dataList" :row-style="rowStyle">
			<template v-slot:toolbar v-if="dataInfoObj.FCanEdit!=2">
				<lay-button size="sm" @click="saveBatch" type="primary" :disabled="editColumn.length===0">保存
				</lay-button>
			</template>
			<template v-for="item in editColumn" v-slot:[item.key]="{ data }">
				<lay-input v-if="!item.dataType" size="sm" v-model="data[item.key]" required></lay-input>
			</template>
		</lay-table>
		<OperateLog v-if="dataInfoObj.FTeamID" ref="OperateLog" :dataInfoObj="dataInfoObj"></OperateLog>
	</lay-container>
</template>

<style scoped>
	.content-box {
		min-height: 580px;
		margin: 30px 10px;
		display: block;
		position: relative;
		clear: both;
		float: none;
	}

	.row-select td {
		overflow: inherit;
	}
</style>
<script>
	import {
		ref
	} from 'vue';
	import {
		getListByTeamId,
		updateBatchById
	} from "/src/api/api/view";
	import {
		getBasicRoleList
	} from "/src/api/api/user";

	import {
		reviewMaterialGroup
	} from "/src/api/api/task";
	import OperateLog from "./OperateLog.vue";

	import {
		getDataCode,
		commonSave
	} from "/src/api/api/common";

	export default {
		components: {
			OperateLog
		},
		setup() {
			const dataColumn = ref([]);
			const dataList = ref([]);
			const cacheDataList = ref([]);
			const dataColor = ref([]);
			const editColumn = ref([]);
			const selectDataMap = ref({});
			let colors = ["1E9FFF", "009688", "393D49", "5FB878"];
			const rowStyle = function(row, rowIndex) {
				let color = "";
				for (let idx = 0; idx < dataColor.value.length; idx++) {
					if (rowIndex > dataColor.value[idx]) color = "color:#" + colors[idx];
				}
				return color;
			}
			return {
				dataColumn,
				dataList,
				cacheDataList,
				dataColor,
				editColumn,
				selectDataMap,
				rowStyle
			}
		},
		props: {
			dataInfoObj: {
				type: Object,
				default: () => {},
			},
			viewDataList: {
				type: Object,
				default: () => [],
			}
		},
		mounted() {
			console.log(this.dataInfoObj)
			this.initPage();
		},
		methods: {
			async initPage() {
				let dataColumn = [];
				let dataColor = [];
				let _data = await getListByTeamId({
					teamId: this.dataInfoObj.FTeamID,
					viewType: 1,
					FAllView: "1"
				});
				let _power = [];
				if (this.dataInfoObj.FCanEdit != 2) {
					_power = (await getBasicRoleList({
						FRoleType: 36
					}) || []).map(it => parseInt(it.F2));
				}
				dataColumn = _data.columns;
				this.editColumn = dataColumn.filter(it => {
					if (this.dataInfoObj.FCanEdit == 2) return true;
					let b = _power.indexOf(it.id) >= 0;
					if (b) it.customSlot = it.key;
					return b;
				});
				//修改项移到前面
				if (this.dataInfoObj.FCanEdit != 2) {
					let tempColumn1 = dataColumn.filter(s => s.customSlot) || [];
					let tempColumn2 = dataColumn.filter(s => s.customSlot == undefined) || [];
					dataColumn = [...tempColumn1, ...tempColumn2];
				}
				let col7 = dataColumn.filter(s => s.fieldType == 7) || [];
				_data.rows.forEach((item) => {
					let info = _data.infos.find(s => s.FDataID == item.FMaterialID) || null;
					if (info != null) {
						col7.forEach((col) => {
							item[col.key] = info[col.key] || item[col.key] || "";
						});
					}
					if (item.FTypeID1 == 0 || item.FTypeID1 == -1) {
						item.FTypeID1 = "";
					} else {
						let first = _data.types.find(s => s.FID == item.FTypeID1) || null;
						if (first != null) {
							item.FTypeID1 = first.FName;
							first = _data.types.find(s => s.FID == item.FTypeID2) || null;
							item.FTypeID2 = first != null ? first.FName : "";
						} else {
							item.FTypeID1 = item.FTypeID2 = "";
						}
					}
				});
				this.cacheDataList = JSON.parse(JSON.stringify(_data.rows));
				this.dataList = _data.rows;
				this.dataColumn = dataColumn;
				this.dataColor = dataColor
			},
			cancelClick(isRefresh) {
				isRefresh = isRefresh == undefined ? false : isRefresh;
				this.$emit('cancelClick', isRefresh);
			},
			async submitUpdate(_list, _logs) {
				let idx = layer.load(2);
				let _result = await reviewMaterialGroup({
					FTaskID: this.dataInfoObj.FID,
					FTeamID: this.dataInfoObj.FTeamID,
					FList: JSON.stringify(_list)
				});
				if (_result > 0) {
					await commonSave({
						teamId: this.dataInfoObj['FTeamID'],
						type: this.dataInfoObj['FType'],
						desc: _logs.length > 0 ? _logs.join("；") : "未修改"
					}, "OperateLog");
					let $this = this;
					setTimeout(function() {
						layer.close(idx);
						layer.msg('复核成功');
						$this.cancelClick(true)
					}, 500);
				} else {
					layer.close(idx);
					layer.msg('复核失败');
				}
				this.$refs.OperateLog._getPageList();
			},
			saveBatch() {
				let _list = [];
				let _logs = [];
				this.dataList.forEach((row, index) => {
					let cache = this.cacheDataList[index];
					let _row = {};
					let _log = [];
					for (let col of this.editColumn) {
						if (row[col.key] !== cache[col.key]) {
							_row[col.key] = row[col.key];
							_log.push(col.title + "：" + row[col.key]);
						}
					}
					if (Object.keys(_row).length > 0) {
						_row['FID'] = row['FID'];
						_list.push(_row);
						_logs.push(_log.join("、"));
					}
				});
				let max = this.dataList.length;
				let min = _list.length;
				let message = min <= 0 ? "您没做修改，" : (min != max ? ("您有" + (max - min) + "项没修改，") : "");
				message += "确认提交吗？";
				let $this = this;
				layer.confirm(message, {
					title: "提示",
					btn: [{
							text: '确定',
							callback: function(id) {
								layer.close(id);
								$this.submitUpdate(_list, _logs);
							}
						},
						{
							text: '取消',
							callback: function(id) {
								layer.close(id);
							}
						}
					]
				});
			}
		}
	}
</script>
