<template>
	<div class="all-box">
		<div class="all" style="padding: 40px;">
			<div style="margin-bottom: 30px;">
				<div class="initia_title">工作日志积分</div>
				<div class="initia_title_1">
					1、启用后，在钉钉发布工作日志，系统自动按“日志积分规则”奖分
					<br />
					2、日志名称必须与“钉钉日志模板名称”一致，否则奖分不会生效
					<br />
					3、日志支持每月、每周、每次汇报，可自行根据需要设置“日志积分规则
				</div>
				<div class="flex-box-end" style="margin-top: 20px;">
					<el-button  v-if="$authoritys() != 'dept_manager' && $authoritys() != 'employee'" plain @click="isShowLog = true">刷新汇报记录</el-button>
					<el-button  type="primary"  @click="add()">添加日志模板</el-button>
				</div>
			</div>
			<el-table ref="multipleTable" :data="list" tooltip-effect="dark" align="center" v-loading="loading">
				<el-table-column label="序号" align="left" type="index"></el-table-column>
				<el-table-column label="日志名称" prop="name"></el-table-column>
				<el-table-column label="日志积分规则">
					<template slot-scope="scope">
						<span>{{ getMoney(scope.row.interval) }}汇报,每次奖{{ scope.row.config.point }} B分</span>
					</template>
				</el-table-column>
				<el-table-column label="状态">
					<template slot-scope="scope">
						<span v-if="scope.row.enable == 0" class="yellow">已禁用</span>
						<span v-else class="green">已启用</span>
					</template>
				</el-table-column>
				<el-table-column label="操作">
					<template slot-scope="scope">
						<div class="flex-box-ce">
							<div @click.stop="openSet(scope.row)" style="margin-right: 10px;"><span class="lookQrcode">编辑</span></div>
							<div @click.stop="deleteSet(scope.row)"><span class="lookQrcode red">删除</span></div>
						</div>
					</template>
				</el-table-column>
				<template slot="empty">
					<noData :isSolt="true">
						<div>
							您当前没有日志模板，去添加
							<span style="cursor: pointer;margin-left: 10px;" @click="add()" class="blue">添加</span>
						</div>
					</noData>
				</template>
			</el-table>
		</div>
		<el-dialog title="设置" :visible.sync="dialogVisible" top="25vh" width="520px" class="dialog">
			<el-alert class="el-dialog__body" title="日志名称必须与钉钉日志模板名称一致，否则设置无效，不会产生积分" type="warning" :closable="false" show-icon></el-alert>
			<el-form label-width="110px" :model="selectItem" ref="setForm" class="form">
				<el-form-item style="margin-bottom: 0px;" label="日志名称">
					<div class="flex-box flex-v-ce">
						<el-input v-if="isAdd" style="width: 200px;" v-model="selectItem.name" placeholder="请输入日志名称"></el-input>
						<template v-else>
							<div class="font-flex-word name">{{ selectItem.name }}</div>
							<div class="blue" @click="showName(selectItem.name)">修改</div>
						</template>
					</div>
					<el-dialog title="设置名称" :visible.sync="isShowName" top="25vh" width="520px" append-to-body>
						<div style="text-align: center;">
							<el-alert
								class="el-dialog__body"
								title="日志名称必须与钉钉日志模板名称一致，否则设置无效，不会产生积分"
								type="warning"
								:closable="false"
								show-icon
							></el-alert>
							<el-input style="width: 200px;" v-model="nameVal" placeholder="请输入日志名称"></el-input>
							<div class="flex-box-end" style="margin-top: 20px;">
								<el-button @click="isShowName = false">取消</el-button>
								<el-button type="primary" @click="submitName()">确定</el-button>
							</div>
						</div>
					</el-dialog>
				</el-form-item>
				<el-form-item style="margin-bottom: 10px;" label="是否启用"><el-switch v-model="selectItem.enable"></el-switch></el-form-item>
				<template v-if="selectItem.enable">
					<div style="margin-top: 20px;"></div>
					<div class="title">日志积分规则</div>
					<el-form-item label="日志汇报要求" prop="interval" :rules="[{ required: true, message: '请选择汇报要求' }]">
						<el-select class="widthInput" v-model="selectItem.interval" placeholder="请选择">
							<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="每次汇报奖励" :rules="[{ required: true, message: '请输入分值', trigger: 'change' }]" prop="config.point">
						<el-input
							class="widthInput"
							placeholder="请输入(1~999)"
							@input="checkCountry($event, 'input1')"
							id="input1"
							v-model.number="selectItem.config.point"
						></el-input>
						<span style="margin-left: 5px;">B分</span>
					</el-form-item>
					<el-form-item label="汇报提交超过" prop="config.upper_limit" :rules="[{ required: true, message: '请输入次数' }]">
						<el-input
							class="widthInput"
							placeholder="请输入(1~999)"
							@input="checkCountry($event, 'input2')"
							id="input2"
							v-model.number="selectItem.config.upper_limit"
						></el-input>
						<span style="margin-left: 5px;">次,不再奖励积分</span>
					</el-form-item>
				</template>
				<el-form-item style="text-align: right; margin-bottom: 0">
					<el-button @click="dialogVisible = false">取消</el-button>
					<el-button type="primary" :loading="setLoading" :disabled="setLoading" @click="submit('setForm')">确定</el-button>
				</el-form-item>
			</el-form>
		</el-dialog>
		<el-dialog title="刷新日志记录" :visible.sync="isShowLog" top="25vh" width="520px" class="dialog" :close-on-click-modal="false" :close-on-press-escape="false">
			<el-form :model="logData" ref="setForm2" label-width="110px">
				<el-form-item label="日志汇报要求" prop="type" :rules="[{ required: true, message: '请选择汇报要求' }]">
					<el-select class="widthInput" v-model="logData.type" placeholder="请选择" style="width: 250px;">
						<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="选择日期" prop="target_date" :rules="[{ required: true, message: '请选择汇报要求' }]">
					<el-date-picker :picker-options="pickerOptions" v-model="logData.target_date" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 250px;"></el-date-picker>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="isShowLog = false">取 消</el-button>
				<el-button type="primary" @click="refresh('setForm2')">确 定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>

export default {
	
	data() {
		return {
			loading: false,
			setLoading: false,
			ding_enable: true,
			list: [],
			dialogVisible: false,
			pickerOptions: {
				disabledDate(time) {
					return time.getTime() > Date.now() - 8.64e7;
				}
			},
			selectItem: {
				config: { point: 0, upper_limit: 0 },
				enable: true,
				id: '',
				interval: 1,
				name: '',
				site_id: '',
				type: ''
			},
			options: [
				{
					value: 1,
					label: '每日'
				},
				{
					value: 2,
					label: '每周'
				},
				{
					value: 3,
					label: '每月'
				}
			],
			isShowName: false,
			nameVal: '',

			// 添加模板
			isAdd: false,

			isShowLog: false,
			logData: {
				type: 1,
				target_date: ''
			}
		};
	},
	watch: {
		dialogVisible(val) {
			if (!val) {
				this.isAdd = false;
				this.selectItem = {
					config: { point: 0, upper_limit: 0 },
					enable: true,
					id: '',
					interval: 1,
					name: '',
					site_id: '',
					type: ''
				};
			}
		}
	},
	mounted() {
		this.getList();
	},
	methods: {
		refresh(fromName) {
			this.$refs[fromName].validate(valid => {
				if (valid) {
					let type='';
					if(this.logData.type==1){
						type='daily'
					}else if(this.logData.type==2){
						type='weekly'
					}else{
						type='monthly'
					}
					let data = {
						type: type,
						// employee_id: this.$getUserData().id,
						target_date: this.logData.target_date
					};
					this.$axios('post', '/api/ding/report_sync', data)
						.then(res => {
							this.$message.success('已刷新');
						})
						.finally(() => {
							this.logData={
								type: 1,
								target_date: ''
							};
							this.isShowLog = false;
						});
				}
			});
		},
		deleteSet(e) {
			this.$confirm('确定要删除该日志模板吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$axios('DELETE', '/api/integral/report/templates', { id: e.id }).then(res => {
					if (res.data.code == 1) {
						this.$message.success('删除成功');
						this.getList();
					}
				});
			});
		},
		add() {
			if (this.list.length == 20) {
				this.$message.warning('日志模板最多有20个');
				return false;
			}
			this.isAdd = true;
			this.dialogVisible = true;
		},
		// 最大值不能大于100
		checkCountry(e, id) {
			//解决v-model与oninput同时使用失效问题
			var dom = document.getElementById(id);
			var value = dom.value;
			//写你自己的输入规则
			value = value.replace(/[^\d]/g, '');
			if (value > 999) {
				id == 'input1' ? (this.selectItem.config.point = '') : (this.selectItem.config.upper_limit = '');
				this.$message.warning('不能大于999');
				return false;
			}
			dom.value = value; //输入框赋值
			id == 'input1' ? (this.selectItem.config.point = dom.value) : (this.selectItem.config.upper_limit = dom.value);
		},
		submitName() {
			if (!this.nameVal || this.nameVal == null || this.nameVal == undefined) {
				this.$message.error('请输入日志名称');
				return;
			}
			this.selectItem.name = this.nameVal;
			this.isShowName = false;
		},
		showName(name) {
			this.nameVal = name;
			this.isShowName = true;
		},
		submit(fromName) {
			if (this.isAdd) {
				if (!this.selectItem.name) {
					this.$message.error('请输入日志名称');
					return false;
				}
			}
			if (this.selectItem.enable) {
				if (this.selectItem.config.point == 0) {
					this.$message.error('汇报奖励不能为0');
					return false;
				}
				if (this.selectItem.config.upper_limit == 0) {
					this.$message.error('提交超过次数不能为0');
					return false;
				}
			}
			this.$refs[fromName].validate(valid => {
				if (valid) {
					this.setLoading = true;
					let jsonItem = JSON.stringify(this.selectItem);
					let data = JSON.parse(jsonItem);
					data.enable = data.enable ? 1 : 0;
					data.point = data.config.point;
					data.upper_limit = data.config.upper_limit;
					if (this.isAdd) {
						data.type = 'other';
					}
					this.$axios('post', '/api/integral/report/templates', data)
						.then(res => {
							if (res.data.code == 1) {
								this.$message.success(res.data.msg);
								this.getList();
								this.dialogVisible = false;
							}
						})
						.finally(() => {
							this.setLoading = false;
						});
				}
			});
		},
		openSet(item) {
			console.log(item);
			var jsonItem = JSON.stringify(item);
			this.selectItem = JSON.parse(jsonItem);
			this.selectItem.enable = this.selectItem.enable == 0 ? false : true;
			this.dialogVisible = true;
		},
		getMoney: function(index) {
			var str; //1-每日 2-每周 3-每月
			switch (index) {
				case 1:
					str = '每日';
					break;
				case 2:
					str = '每周';
					break;
				case 3:
					str = '每月';
					break;
			}
			return str;
		},
		getList() {
			this.loading = true;
			this.$axios('get', '/api/integral/report/templates/list')
				.then(res => {
					this.list = res.data.data.list;
				})
				.finally(err => {
					this.loading = false;
				});
		}
	}
};
</script>
<style scoped lang="scss">
.el-dialog__body {
	position: relative;
	top: -20px;
}
.widthInput {
	width: 150px;
}
.title {
	margin: 10px 0;
	margin-top: 0;
	font-size: 14px;
	font-weight: 700;
	padding-left: 13px;
}
.initia_title {
	font-size: 20px;
	color: rgba(48, 49, 51, 1);
	font-family: PingFangSC-Regular;
}
.initia_title_1 {
	color: #e6a23c;
	font-size: 14px;
	margin-top: 10px;
}
.lookQrcode {
	color: #606266;
	cursor: pointer;
	transition: all 0.3s;
	font-weight: 700;
}
.lookQrcode:hover {
	color: #2490fd;
}
.name {
	max-width: 200px;
	margin-right: 10px;
}
</style>
