<template>
	<div class="all" style="padding: 20px;">
		<el-tabs v-model="tabs" type="card">
			<el-tab-pane label="奖扣成功" name="success"></el-tab-pane>
			<el-tab-pane label="待审批" name="waiting"></el-tab-pane>
			<el-tab-pane label="被驳回" name="refuse"></el-tab-pane>
		</el-tabs>
		<el-row style="margin-bottom: 15px;">
			<el-col :span="12">
				<el-button type="primary" @click="point_b">奖扣B分</el-button>
				<el-button type="primary" @click="point_a" plain>奖扣A分</el-button>
			</el-col>
			<el-col :span="6" :offset="6">
				<el-input v-model="keyword" placeholder="输入同事姓名/内容" @keyup.enter.native="getData">
					<el-button slot="append" @click="getData" icon="el-icon-search"></el-button>
				</el-input>
			</el-col>
		</el-row>

		<el-table v-if="tabs == 'success'" :data="dataList" stripe fit v-loading="table_loading" @row-click="open_detail">
			<el-table-column label="姓名" prop="employee_id" align="left">
				<template slot-scope="scope">
					<div class="flex-box flex-contet-conter">
						<userImage  :user_name="scope.row.employee_name" width="50px" height="50px"></userImage>
						<span style="margin-left: 10px; line-height: 50px;">{{ scope.row.employee_name }}</span>
					</div>
				</template>
			</el-table-column>
			<el-table-column prop="remark" show-overflow-tooltip label="奖扣内容"></el-table-column>
			<el-table-column prop="point" label="积分" width="120">
				<template slot-scope="scope">
					<span :class="{ color_green: scope.row.point < 0, color_red: scope.row.point > 0 }">
						<span v-show="scope.row.point > 0">+</span>
						{{ scope.row.point }} {{ point_name(scope.row.pt_id) }}
					</span>
				</template>
			</el-table-column>
			<el-table-column prop="create_time" label="奖扣时间" width="150">
				<template slot-scope="scope">
					{{ cuttString(scope.row.create_time) }}
				</template>
			</el-table-column>
			<template slot="empty">
				<noData></noData>
			</template>
		</el-table>

		<el-table v-if="tabs !== 'success'" :data="dataList" stripe fit v-loading="table_loading" @row-click="open_detail1">
			<el-table-column label="姓名" prop="employee_id" align="left">
				<template slot-scope="scope">
					<div class="flex-box flex-contet-conter">
						<userImage  :user_name="scope.row.employee_name" width="50px" height="50px"></userImage>
						<span style="margin-left: 10px; line-height: 50px;">{{ scope.row.employee_name }}</span>
					</div>
				</template>
			</el-table-column>
			<el-table-column prop="remark" show-overflow-tooltip label="奖扣内容">
				<template slot-scope="scope">
					{{ scope.row.remark.customize }}
				</template>
			</el-table-column>
			<el-table-column prop="event_time" label="奖扣时间" width="150"></el-table-column>

			<template slot="empty">
				<noData></noData>
			</template>
		</el-table>

		<center style="margin-top: 15px;">
			<el-pagination
				background
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="formData.page"
				:page-sizes="[10, 20, 30, 40, 50, 100]"
				layout="total, sizes, prev, pager, next"
				:page-size="pageLimit"
				:total="formData.total"
			></el-pagination>
		</center>

		<el-drawer title="奖扣详情" :visible.sync="drawer" ref="drawer" :with-header="false" :size="'600px'" direction="rtl">
			<div class="drawer_title">奖扣详情</div>
			<div class="detail_popup" v-loading="detail_loading" v-if="detail_info !== null">
				<el-row :gutter="10" style="padding-bottom:10px;border-bottom:1px #f8f8f8 solid;">
					<el-col :span="24">
						<div class="flex-box flex-v-ce">
							<userImage :user_name="detail_info.employee_name" :img_url="detail_info.img_url" width="50px" height="50px" fontSize="1"></userImage>
							<span style="line-height:50px; margin-left:10px;margin:0px 10px;">{{ detail_info.employee_name }}</span>
							<span class="color_red point" v-show="detail_info.point >= 0 && detail_info.point"> +{{ detail_info.point }}</span>
							<span class="color_green point" v-show="detail_info.point < 0 && detail_info.point"> {{ detail_info.point }}</span>
							<span v-show="detail_info.point"> {{ detail_info.pt_id == 1 ? '绩效分' : detail_info.pt_id == 2 ? 'A分' : detail_info.pt_id == 3 ? 'B分' : '' }}</span>
						</div>
					</el-col>
				</el-row>

				<el-row :gutter="10">
					<el-col :span="4">任务描述</el-col>
					<el-col :span="20" v-if="detail_info.remark">{{ detail_info.remark && (detail_info.remark.customize || detail_info.remark.rule) }}</el-col>
				</el-row>

				<el-row :gutter="10">
					<el-col :span="4">规则分类</el-col>
					<el-col :span="20">{{ detail_info.rule_list }}</el-col>
				</el-row>

				<el-row :gutter="10" v-show="detail_info.rule_list">
					<el-col :span="4">事件时间</el-col>
					<el-col :span="20">{{ detail_info.date }}</el-col>
				</el-row>

				<el-row :gutter="10" v-show="detail_info.source_type_mark">
					<el-col :span="4">来源类型</el-col>
					<el-col :span="20">{{ detail_info.source_type_mark }}</el-col>
				</el-row>

				<el-row :gutter="10" v-show="!detail_info.rule_id">
					<el-col :span="4">记录人</el-col>
					<el-col :span="20">{{ detail_info.employee_name }}</el-col>
				</el-row>

				<el-row :gutter="10" :v-if="detail_info.files">
					<el-col v-for="(item, index) in detail_info.files" :key="index" :span="4">{{ index == 0 ? '图片' : '' }}</el-col>
					<el-col :span="20">
						<el-image
							v-for="(item, index) in detail_info.files"
							:key="index"
							style="width: 100px; height: 100px;margin-right:8px"
							:src="item"
							:preview-src-list="detail_info.files"
						></el-image>
					</el-col>
				</el-row>

				<div v-show="detail_info.rule_item_id">
					<p class="row_title">规则依据</p>
					<el-row :gutter="10">
						<el-col :span="4">规则分类</el-col>
						<el-col :span="19">{{ detail_info.rule_list }}</el-col>
					</el-row>
					<el-row v-if="detail_info.remark && detail_info.remark.rule">
						<el-col :span="4">积分规则</el-col>
						<el-col :span="19">{{ detail_info.remark.rule }}</el-col>
					</el-row>
					<el-row v-if="detail_info.rule_item">
						<el-col :span="4">积分</el-col>
						<el-col :span="19" v-show="detail_info.rule_item.min_point == detail_info.rule_item.max_point">{{ detail_info.rule_item.min_point }}</el-col>
						<el-col :span="19" v-show="detail_info.rule_item.min_point != detail_info.rule_item.max_point">
							{{ detail_info.rule_item.min_point }} ~ {{ detail_info.rule_item.max_point }}
						</el-col>
					</el-row>
				</div>

				<div v-show="detail_info.process != false">
					<p class="row_title">
						审批
						<span class="row_tips">多人审批时，以最后一人为准</span>
					</p>
					<div class="examine_steps">
						<el-steps direction="vertical" :space="50">
							<el-step v-for="(item, index) in detail_info.process" :key="index">
								<template slot="icon">
									<userImage width="36px" height="36px" :img_url="item.img_url" :user_name="item.name"></userImage>
								</template>
								<template slot="title">
									<div style="color: #303133;font-size:14px;margin-top:-2px;">
										{{ item.name }} {{ item.remark }}
										<!-- <span v-show="item.point !== 0">{{item.point}}</span> -->
									</div>
								</template>
								<template slot="description">
									<div style="color:rgb(130 130 130)">{{ item.time }}</div>
								</template>
							</el-step>
						</el-steps>
					</div>
				</div>
			</div>
		</el-drawer>

		<el-drawer title="奖扣详情" :visible.sync="detailShow" :with-header="false" :size="'600px'" direction="rtl">
			<div class="drawer_title">奖扣详情</div>
			<div class="detail_popup" v-loading="detail_loading" v-if="detail_info !== null">
				<el-row :gutter="10" style="padding-bottom:10px;border-bottom:1px #f8f8f8 solid;">
					<el-col :span="24" >
						<div class="flex-box flex-v-ce">
						<userImage :user_name="detail_info.employee_name" width="50px" height="50px" fontSize="1"></userImage>
						<span style="line-height:50px; margin-left:10px;margin:0px 10px;">{{ getEmployeeName(detail_info.applyor_id) }}</span>
						<span class="color_red point" v-show="detail_info.point >= 0 && detail_info.point">+{{ detail_info.point }}</span>
						<span class="color_green point" v-show="detail_info.point < 0 && detail_info.point">{{ detail_info.point }}</span>
						<span v-show="detail_info.point">{{ detail_info.pt_id == 1 ? '绩效分' : detail_info.pt_id == 2 ? 'A分' : detail_info.pt_id == 3 ? 'B分' : '' }}</span>
						</div>
					</el-col>
				</el-row>

				<el-row :gutter="10" v-show="detail_info.source_type_mark">
					<el-col :span="4">来源类型</el-col>
					<el-col :span="20">{{ detail_info.source_type_mark }}</el-col>
				</el-row>

				<el-row :gutter="10" v-for="(item, index) in detail_info.detail" :key="index">
					<el-col :span="4">{{ item.key }}</el-col>
					<el-col :span="20" v-if="item.key == '附件'">
						<el-image style="width: 100px; height: 100px" :src="item.value[0]" :preview-src-list="item.value"></el-image>
					</el-col>
					<el-col :span="20" v-else>{{ item.value }}</el-col>
				</el-row>

				<el-row :gutter="10" :v-if="detail_info.files">
					<el-col v-for="(item, index) in detail_info.files" :key="index" :span="4">{{ index == 0 ? '图片' : '' }}</el-col>
					<el-col :span="20">
						<el-image
							v-for="(item, index) in detail_info.files"
							:key="index"
							style="width: 100px; height: 100px;margin-right:8px"
							:src="item"
							:preview-src-list="detail_info.files"
						></el-image>
					</el-col>
				</el-row>

				<div v-show="detail_info.rule_id && detail_info.process !== null && detail_info.process.length > 0">
					<p class="row_title">
						审批
						<span class="row_tips">多人审批时，以最后一人为准</span>
					</p>

					<div class="examine_steps">
						<el-steps direction="vertical" :space="50">
							<el-step v-for="(item, index) in detail_info.process" :key="index">
								<template slot="icon">
									<userImage width="36px" height="36px" :img_url="item.img_url" :user_name="item.name"></userImage>
								</template>
								<template slot="title">
									<div style="color: #303133;font-size:14px;margin-top:-2px;">
										{{ item.name }} {{ item.remark }}
										<!-- <span v-show="item.point !== 0">{{item.point}}</span> -->
									</div>
								</template>
								<template slot="description" style="">
									<div style="color:rgb(130 130 130)">{{ item.time }}</div>
								</template>
							</el-step>
						</el-steps>
					</div>
				</div>
			</div>
		</el-drawer>

		<bonusPointsPopup :title="popuTitle" :visible.sync="popupVisible" :refresh.sync="refreshData" :integralType.sync="integralType"></bonusPointsPopup>
	</div>
</template>

<script>
import EmployeeSelector from '@/components/EmployeeSelector.vue';
import moment from 'moment';
import noData from '@/components/noData';
import bonusPointsPopup from '@/components/bonusPointsPopup';

export default {
	data() {
		return {
			refreshData: '',
			popuTitle: '奖扣A分',
			popupVisible: false,
			integralType: 0,
			table_loading: false,
			formData: {
				page: 1,
				page_size: 10,
				total: 0
			},
			tabs: 'success',
			dataList: [],
			pageLimit: 10,
			keyword: '',
			showTitle: false,
			dialogVisible: false,
			dialogData: {
				members: [],
				items: [
					{
						rule_switch: false,
						rule_id: '',
						item_id: '',
						point: '0',
						remark: '',
						event_time: moment().format('YYYY-MM-DD'),
						approval: '',
						approval_not_select: [],
						approval_selected: { dept: [], employee: [] },
						plus: true,
						pt_id: '',
						reviewer_id: '',
						files: []
					}
				]
			},
			itemData: {},
			rules: {
				manager: [{ required: true, message: '请选择录入对象', trigger: 'blur' }]
			},
			manager: '',
			employee_not_select: [],
			manager_selected: { dept: [], employee: [] },
			show_employee_selector: false,

			show_approval_selector: false,
			itemIndex: 0,
			rule_list: [],

			rule_item_list: {},
			rule_item_list_value: null,

			drawer: false,
			detail_loading: false,
			detail_info: {},
			detailShow: false,

			employee_name: JSON.parse(localStorage.getItem('SET_EMPLOYEE_MAP')),
			fileList: [],
			userId:167
		};
	},
	components: {
		EmployeeSelector,
		bonusPointsPopup,
		noData
	},
	mounted() {
		// if(this.$route.query.type == 1){
		//   this.point_a()
		// }else if(this.$route.query.type == 2){
		//   this.point_b()
		// }
		this.getData()
	},
	watch: {
		tabs(val) {
			this.dataList = [];
			this.formData.total = 0;
			this.keyword = '';
			this.formData.page = 1;
			this.formData.page_size = 10;
			this.getData();
		},
		refreshData(val) {
			// this.getData();
		}
	},
	methods: {
		getEmployeeName(id) {
			for (const key in this.employee_name) {
				if (this.employee_name[key].id == id) {
					return this.employee_name[key].name;
				}
			}
		},
		open_detail1(item) {
			this.detailShow = true;
			let data = {
				review_id: item.id
			};
			this.detail_loading = true;
			this.$axios('get', '/api/integral/review', data).then(res => {
				if (res.data.code == 1) {
					this.detail_info = res.data.data;
				} else {
					this.$message.error(res.data.data.msg);
				}
			})
			.finally(() => {
				this.detail_loading = false;
			});
		},
		open_detail(item) {
			this.drawer = true;
			let data = {
				event_id: item.id
			};
			this.detail_loading = true;
			this.$axios('get', '/api/integral/statistics/integral/info', data).then(res => {
				if (res.data.code == 1) {
					this.detail_info = res.data.data;
				} else {
					this.$message.error(res.data.data.msg);
				}
			})
			.finally(() => {
				this.detail_loading = false;
			});
		},

		cuttString(data) {
			return data.substring(0, 10);
		},
		point_a() {
			this.popupVisible = false;
			this.integralType = 2;
			this.popuTitle = '奖扣A分';
			this.popupVisible = true;
		},
		point_b() {
			this.popupVisible = false;
			this.integralType = 3;
			this.popuTitle = '奖扣B分';
			this.popupVisible = true;
		},
		point_name(id) {
			return this.$getTyps(id).name;
		},
		handleCurrentChange(val) {
			this.formData.page = val;
			this.getData();
		},
		handleSizeChange(val) {
			this.pageLimit = val;
			this.formData.page_size = this.pageLimit;
			this.getData();
		},
		getData() {
			this.table_loading = true;
			let data = {};
			if (this.tabs == 'success') {
				data = {
					page: this.formData.page,
					page_size: this.formData.page_size,
					recorder_id:this.userId,
					source_type: '1',
					keyword: this.keyword
				};
			} else if (this.tabs == 'waiting') {
				data = {
					page: this.formData.page,
					page_size: this.formData.page_size,
					keyword: this.keyword,
					pt_id: '0',
					type: 'waiting'
				};
			} else {
				data = {
					page: this.formData.page,
					page_size: this.formData.page_size,
					keyword: this.keyword,
					pt_id: '0',
					type: 'refuse'
				};
			}
			this.$axios('get', this.tabs == 'success' ? '/api/integral/statistics/integral' : '/api/integral/review/entry/list', data)
				.then(res => {
					if (res.data.code == 1) {
						this.dataList = res.data.data.list;
						this.formData.total = res.data.data.total;
					}
				})
				.finally(() => {
					this.table_loading = false;
				});
		}
	}
};
</script>

<style scoped lang="scss">
.box {
	background-color: #ffffff;
	padding: 20px;
	min-height: calc(100vh - 160px);
}
.color_green {
	color: #4bd964;
}
.color_red {
	color: #f56c6c;
}
.detail_popup {
	padding: 20px;
	height: calc(100vh - 60px);
	overflow: auto;
	.row_title {
		position: relative;
		margin: 0 0 20px 0;
		padding-top: 12px;
		font-size: 16px;
		color: #303133;
		line-height: 22px;
	}
	.row_title:before {
		position: absolute;
		top: 0;
		content: ' ';
		width: 100%;
		border-top: 1px #f8f8f8 solid;
	}
	.el-row {
		margin-bottom: 10px;
		font-size: 14px;
		.el-col-4 {
			color: #606266;
		}
	}
}
.drawer_title {
	font-size: 18px;
	padding: 20px;
	border-bottom: 1px #efefef solid;
}
::v-deep .el-table tr:hover {
	cursor: pointer;
}
</style>
