<template>
	<div>
		<!-- 抢单任务弹窗 -->
		<el-dialog :title="showTitle" :visible.sync="dialogVisible" :close-on-click-modal="false" :before-close="closeDialog" width="700px" top="5%">
			<div v-loading="forTheTaskLoading">
				<div class="orange" style="text-align: center;padding-bottom: 10px;" v-if="config.event_review_status&&config.event_task_review">复核开启后，积分需管理员复核后才计入排名和统计</div>
				<el-form :model="formData" ref="formData" label-width="100px">
					<el-form-item
						label="任务内容"
						prop="task_name"
						:rules="[{ required: true, message: '请填写任务内容', trigger: 'blur' }, { min: 3, max: 100, message: '长度在 3 到 100 个字符', trigger: 'blur' }]"
					>
						<el-input type="textarea" rows="3" maxlength="100" show-word-limit v-model="formData.task_name" placeholder="请输入任务内容(限100字)" class="reward_textarea"></el-input>
					</el-form-item>
					<el-form-item label="积分类型" prop="pt_id" :rules="[{ required: true, message: '请选择积分类型', trigger: 'blur' }]">
						<el-radio-group v-model="formData.pt_id" @change="from_ptId">
							<el-radio v-for="(item, index) in point_types" :key="index" v-show="item.code !== 'JX'" :label="item.id">{{ item.name }}</el-radio>
						</el-radio-group>
					</el-form-item>
					
					<el-form-item label="指定规则">
						 <el-switch  v-model="isSelectType" @change="isSelectTypeC"></el-switch>
					</el-form-item>
					<!-- 选择分类 -->
					<el-form-item class="test_cascader_id" label="选择分类"	v-if="!isSelectType"
						prop="rule_value"
						:rules="[{ required: true, message: '请选择规则分类', trigger: 'blur' }]"
					>
						<el-cascader
							v-model="formData.rule_value"
							ref="ruleCascader"
							:popper-class="'ruleClass'"
							filterable
							clearable
							:options="trees_ab.rule_tree_all"
							@change="ruleChange2"
							:props="{ children: 'child', label: 'name', value: 'id', checkStrictly: true }"
						></el-cascader>
					</el-form-item>
					<!-- 选择规则 -->
					<el-form-item :label="trees_PtId(formData.pt_id) +'规则'" v-else	prop="tree_echo" :rules="[{ required: true, message: '请选择规则', trigger: 'blur' }]"
					>
						<el-cascader
							v-model="formData.tree_echo"
							ref="ruleItem"
							:popper-class="'itemClass'"
							filterable
							clearable
							:options="formData.pt_id == 2 ? trees_ab.A : trees_ab.B"
							@change="ruleItemChange"
							:show-all-levels="false"
							:props="{ children: 'child', label: 'name', value: 'id' }"
						></el-cascader>
					</el-form-item>
					
					<el-form-item prop="range_type" v-if="formData.range_type != 0">
						<div style="line-height: 24px;" v-show="formData.range_type == 1">
							{{ min }}
							<span class="blue">{{ trees_PtId(formData.pt_id) }}</span>
						</div>
						<div style="line-height: 24px;" v-show="formData.range_type == 2">
							{{ min }} ~ {{ max }}
							<span class="blue">{{ trees_PtId(formData.pt_id) }}</span>
						</div>
						<div style="line-height: 24px;">{{ treeName }}</div>
					</el-form-item>

					<el-form-item label="任务积分" prop="base_point" :rules="[{ required: true, message: '请填写任务积分', trigger: 'blur' }]">
						<el-input-number v-if="formData.range_type == 0" v-model.number="formData.base_point"></el-input-number>
						<el-input-number v-else :disabled="formData.range_type == 1" v-model.number="formData.base_point" :min="min" :max="max"></el-input-number>
					</el-form-item>
					<el-form-item label="审批人" prop="reviewer_id" :rules="[{ required: true, message: '请选择审批人', trigger: 'change' }]">
						<el-row>
							<el-col :span="18">
								<el-input auto-complete="off" v-model="reviewerName" placeholder="请选择审批人"></el-input>
								<div @click="show_reviewer_selector = true" style=" position: absolute; top: 0; right: 0; left: 0; bottom: 0; z-index: 9;"></div>
							</el-col>
						</el-row>
						<el-dialog title="选择审批人" width="640px" :visible.sync="show_reviewer_selector" append-to-body :before-close="handleClose">
							<EmployeeSelector
								v-if="show_reviewer_selector"
								ref="members"
								:multi="false"
								:user_Administrator_list="true"
								:selected="reviewer_selected"
								@confirm="reviewer_confirm"
							/>
							<span slot="footer" class="dialog-footer">
								<el-button @click="show_reviewer_selector = false">取 消</el-button>
								<el-button type="primary" @click="submitMembers('members')">确 定</el-button>
							</span>
						</el-dialog>
					</el-form-item>

					<el-form-item label="截止日期" prop="expire_time" v-if="formData.task_cycle == 0" :rules="[{ required: true, message: '请选择截止日期', trigger: 'blur' }]">
						<el-date-picker
							v-model="formData.expire_time"
							type="datetime"
							placeholder="选择日期时间"
							format="yyyy-MM-dd HH:mm"
							value-format="yyyy-MM-dd HH:mm"
							default-time="18:00"
							:clearable="false"
						></el-date-picker>
					</el-form-item>

					<el-form-item label="截止日期" prop="task_expire_day" v-if="formData.task_cycle == 1" :rules="[{ required: true, message: '请选择截止日期', trigger: 'blur' }]">
						<el-select v-model="formData.task_expire_day" placeholder="请选择截止日期">
							<el-option v-for="item in columns1" :key="item.value" :label="item.name" :value="item.value"></el-option>
						</el-select>
					</el-form-item>

					<el-form-item label="截止日期" prop="task_expire_day" v-if="formData.task_cycle == 2" :rules="[{ required: true, message: '请选择截止日期', trigger: 'blur' }]">
						<el-select v-model="formData.task_expire_day" placeholder="请选择截止日期">
							<el-option v-for="item in columns2" :key="item.value" :label="item.name" :value="item.value"></el-option>
						</el-select>
					</el-form-item>

					<el-form-item label="截止日期" prop="task_expire_day" v-if="formData.task_cycle == 3" :rules="[{ required: true, message: '请选择截止日期', trigger: 'blur' }]">
						<el-select v-model="formData.task_expire_day" placeholder="请选择截止日期">
							<el-option v-for="item in columns3" :key="item.value" :label="item.name" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item prop="chance" :rules="[{ required: true, message: '请输入名额', trigger: 'blur' }]">
						<template slot="label">
							<el-tooltip  effect="dark" placement="top">
							  <div slot="content">
								 抢单名额决定多少个人能抢到任务去做，最多不能超过100名
							  </div>
							  <span>抢单名额<i style="padding-left: 5px;font-size: 16px;" class="el-icon-question fontColorD"></i></span>
							</el-tooltip>
						</template>
						<el-input v-model="formData.chance" style="width: 220px;"  @input="formData.chance = formData.chance.replace(/[^\d]/g, '')" placeholder="请输入名额(最多100名)"></el-input>
					</el-form-item>
					<el-form-item label="任务备注" prop="task_remark" :rules="[{ max: 300, message: '长度不能超过 300 个字', trigger: 'blur' }]">
						<el-input type="textarea" :rows="5" v-model="formData.task_remark" maxlength="300" show-word-limit placeholder="请输入任务备注(限300字)"></el-input>
					</el-form-item>
					
					<el-form-item label="图片">
						<upload
							:headers="Xtoken"
							class="avatar-uploader"
							:action="'https://' + 'integralsys.oss-cn-shenzhen.aliyuncs.com'"
							:show-file-list="true"
							:file-list="fileList"
							:on-success="handleFilesSuccess"
							:on-preview="onFilePreView"
							:before-upload="beforeUpload"
							accept="image/jpeg,image/png"
							:on-remove="onFileRemove"
							:limit="3"
							:multiple="true"
							ref="clearPicture"
						>
							<el-button size="small" type="primary">点击上传</el-button>
							（最多选择3张）
						</upload>
					</el-form-item>
					<el-form-item label="可见范围" prop="dept_ids">
						<div style="background-color: rgb(250, 251, 252);padding: 8px;border: 5px;">
							<div style="margin-bottom: 16px;">
								<div  style="padding-left:5px;">没有指定部门与人员时默认全公司可见</div>
								<el-cascader
									size="medium"
									style="width: 360px;"
									v-model="dept_name"
									@change="handleChange"
									:options="dept_tree"
									collapse-tags
									:props="{checkStrictly: true, value: 'id', label: 'name', multiple: true, children: '_child' }"
									ref="dept"
									clearable
									placeholder="请选择部门"
								></el-cascader>
							</div>
							<div>
								<!-- <div style="padding-left:5px;">指定人员可查看或领取任务(最多50人)</div> -->
								<div class="input-box font-flex-word" @click="openUserSelect">
									<span style="color:#b5b7bb;" v-if="employeeList.length==0">请选择人员</span>
									<template v-else>
										<span v-for="(item,index) in employeeList" :key="index">{{item.name}}<span v-if="index!=employeeList.length-1">,</span></span>
									</template>
								</div>
							</div>
						</div>
					</el-form-item>

					<div style="margin-left: 80px; margin-bottom: 8px;">以天为单位，每 提前/逾期 一天 加分/扣分</div>
					<el-form-item label="提前奖分" prop="ahead_award_point">
						<el-input v-model="formData.ahead_award_point" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入每日奖分" style="width: 150px;"></el-input> B分/每天，
						奖分上限 &nbsp;<el-input v-model="formData.ahead_award_point_limit" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入奖分上限" style="width: 150px;"></el-input> B分
					</el-form-item>
					<el-form-item label="逾期扣分"  prop="timeout_deduction_point" >
						<el-input v-model="formData.timeout_deduction_point" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入每日扣分" style="width: 150px;"></el-input> B分/每天，
						扣分上限 &nbsp;<el-input v-model="formData.timeout_deduction_point_limit" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入扣分上限" style="width: 150px;"></el-input> B分
					</el-form-item>
					<el-form-item v-show="false" prop="ahead_award_point_limit" ></el-form-item>
					<el-form-item v-show="false" prop="timeout_deduction_point_limit" ></el-form-item>

					<el-form-item>
						<div class="flex-box-end">
							<el-button @click="resetForm('formData')">取消</el-button>
							<el-button type="primary" @click="sub('formData')">确认修改</el-button>
						</div>
					</el-form-item>
				</el-form>
			</div>
		</el-dialog>
		<el-dialog title="人员选择" :visible.sync="manageScope_show" top="5vh" width="700px">
			<EmployeeSelector
				v-if="manageScope_show"
				ref="Employee2"
				:isChecKedAll="false"
				:max="50"
				:selected="selected_manage"
				@confirm="manage_confirm"
			></EmployeeSelector>
			<span slot="footer">
				<el-button @click="manageScope_show=false">取消</el-button>
				<el-button type="primary"  @click="sub_manageScope">完成</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
import moment from 'moment';
import EmployeeSelector from '@/components/EmployeeSelector.vue';
import upload from '@/components/upload';
export default {
	name: 'bonusPoints',
	props: {
		rewardTaskAmendData: {
			type: Object
		},
		employee_not_select: {
			type: Array,
			default: () => {
				return [];
			}
		},
		dialogVisible: {
			type: Boolean,
			default: false
		},
		showTitle: {
			type: String,
			default: ''
		},
		trees_ab: {
			type: Object,
			default: ()=>{
				return {};
			}
		}
	},
	data() {
		return {
			isSelectType:false,//是否选择分类
			//图片附件
			Xtoken: { 'X-Token': this.$getToken() },
			fileList: [],
			max: 0,
			min: 0,
			treeName: '',

			dept_name: [],
			dept_tree: [],
			forTheTaskLoading: false,
			formData: {
				tree_echo: [],//用于编辑任务时的规则回显
				file_list: [],
				range_type: 0,
				rule_id: 0,
				rule_value:[],
				id: 0,
				task_type: '3',
				pt_id: 2,
				task_name: '',
				task_remark: '',
				base_point: '0',
				expire_time: moment().format('YYYY-MM-DD 18:00'),
				task_expire_day: '',
				targets: [],
				reviewer_id: '',
				task_cycle: 0,
				timeout_deduction_point: null,
				timeout_deduction_point_limit: null,
				ahead_award_point: null,
				ahead_award_point_limit: null,
				dept_ids: null,
				chance:1,//次数
				employee_ids:[],
			},
			point_types: [],
			arr:[],
			// 审批人
			reviewerName: null,
			reviewer_not_select: [],
			reviewer_employee_list: [],
			reviewer_selected: { dept: [], employee: [] },
			show_reviewer_selector: false,

			// 重复周期
			task_cycle_arr: [{ value: 0, name: '不重复' }, { value: 1, name: '每天重复' }, { value: 2, name: '每周重复' }, { value: 3, name: '每月重复' }],
			// 周期时间
			columns1: [
				{ name: '01:00', value: 1 },
				{ name: '02:00', value: 2 },
				{ name: '03:00', value: 3 },
				{ name: '04:00', value: 4 },
				{ name: '05:00', value: 5 },
				{ name: '06:00', value: 6 },
				{ name: '07:00', value: 7 },
				{ name: '08:00', value: 8 },
				{ name: '09:00', value: 9 },
				{ name: '10:00', value: 10 },
				{ name: '11:00', value: 11 },
				{ name: '12:00', value: 12 },
				{ name: '13:00', value: 13 },
				{ name: '14:00', value: 14 },
				{ name: '15:00', value: 15 },
				{ name: '16:00', value: 16 },
				{ name: '17:00', value: 17 },
				{ name: '18:00', value: 18 },
				{ name: '19:00', value: 19 },
				{ name: '20:00', value: 20 },
				{ name: '21:00', value: 21 },
				{ name: '22:00', value: 22 },
				{ name: '23:00', value: 23 },
				{ name: '24:00', value: 24 }
			],
			columns2: [
				{ name: '周一', value: 1 },
				{ name: '周二', value: 2 },
				{ name: '周三', value: 3 },
				{ name: '周四', value: 4 },
				{ name: '周五', value: 5 },
				{ name: '周六', value: 6 },
				{ name: '周日', value: 7 }
			],
			columns3: [
				{ name: '1号', value: 1 },
				{ name: '2号', value: 2 },
				{ name: '3号', value: 3 },
				{ name: '4号', value: 4 },
				{ name: '5号', value: 5 },
				{ name: '6号', value: 6 },
				{ name: '7号', value: 7 },
				{ name: '8号', value: 8 },
				{ name: '9号', value: 9 },
				{ name: '10号', value: 10 },
				{ name: '11号', value: 11 },
				{ name: '12号', value: 12 },
				{ name: '13号', value: 13 },
				{ name: '14号', value: 14 },
				{ name: '15号', value: 15 },
				{ name: '16号', value: 16 },
				{ name: '17号', value: 17 },
				{ name: '18号', value: 18 },
				{ name: '19号', value: 19 },
				{ name: '20号', value: 20 },
				{ name: '21号', value: 21 },
				{ name: '22号', value: 22 },
				{ name: '23号', value: 23 },
				{ name: '24号', value: 24 },
				{ name: '25号', value: 25 },
				{ name: '26号', value: 26 },
				{ name: '27号', value: 27 },
				{ name: '28号', value: 28 },
				{ name: '29号', value: 29 },
				{ name: '30号', value: 30 },
				{ name: '31号', value: 31 }
			],

			// 部门可见
			deptVisibleName: null,
			dept_not_select: [],
			dept_employee_list: [],
			dept_selected: { dept: [], employee: [] },
			show_employee_selector: false,
			chapterArr: [],

			oneruleId: [],
			echoGather: [],
			config:{},
			
			employeeList:[],
			manageScope_show:false,
			selected_manage: { employee: [], dept: [] },
		};
	},
	components: { EmployeeSelector, upload },
	watch: {
		'formData.task_cycle'(val) {
			if (val) {
				this.formData.task_expire_day = 1;
			} else {
				this.formData.expire_time = moment().format('YYYY-MM-DD 18:00');
			}
		},
		rewardTaskAmendData(val) {
			this.formData.id = this.rewardTaskAmendData.id; //任务id
			this.formData.range_type = 0;
			this.formData.pt_id = this.rewardTaskAmendData.pt_id; //积分种类id
			this.formData.task_name = this.rewardTaskAmendData.task_name; //任务名
			this.formData.task_remark = this.rewardTaskAmendData.task_remark; //任务详细描述
			this.formData.expire_time = this.rewardTaskAmendData.expire_time; //截至时间
			this.formData.task_type=this.rewardTaskAmendData.type;
			this.formData.chance = this.rewardTaskAmendData.total_chance;
			this.employeeList=this.rewardTaskAmendData.employee_info;
			
			this.reviewerName = this.rewardTaskAmendData.reviewer_name; //审批者id
			this.formData.reviewer_id = this.rewardTaskAmendData.reviewer_id;
			this.reviewer_selected.employee = [
				{ id: this.rewardTaskAmendData.reviewer_id, img_url: this.rewardTaskAmendData.img_url, name: this.rewardTaskAmendData.reviewer_name }
			];
			let add = ['ahead_award_point', 'ahead_award_point_limit', 'timeout_deduction_point', 'timeout_deduction_point_limit']
			add.forEach(item =>{
				this.deductionPoint(item)
			})
			let department_infoName = '';
			let deptIDs = [];
			this.dept_selected = { dept: [], employee: [] };
			for (let i in this.rewardTaskAmendData.department_info) {
				deptIDs.push(this.rewardTaskAmendData.department_info[i].id);
				let department_infoDept = {};
				department_infoDept.dept_id = this.rewardTaskAmendData.department_info[i].id;
				department_infoDept.dept_name = this.rewardTaskAmendData.department_info[i].name;
				department_infoDept.avatar = 'static/images/e66f.jpg';
				this.dept_selected.dept.push(department_infoDept);
				department_infoName += this.rewardTaskAmendData.department_info[i].name + ',';
			}
			this.deptVisibleName = department_infoName;
			this.formData.dept_ids = deptIDs; //可见范围

			let deptIDdata = deptIDs;
			for (let i in deptIDdata) {
				this.getTreeDatas(this.dept_tree, deptIDdata[i]);
			}
			this.dept_name = this.echoGather;

			let filtList = []
			this.rewardTaskAmendData.task_file_list.forEach((item, index) =>{
				filtList.push({name: item ,url:item, response: { url:item }})
			})
			this.fileList = filtList
			this.fileFun('', filtList)
			if(this.rewardTaskAmendData.point_config.item_info){
				let itemInfo = this.rewardTaskAmendData.point_config.item_info
				// 获取规则的上级分类
				this.arr=[itemInfo.id];
				this.ruleChange(itemInfo.rule_id)
				this.formData.tree_echo = this.arr
				this.isSelectType=true;
				this.$nextTick(_=>{
					this.arr=[];
					this.ruleItemChange(this.formData.tree_echo)
				})
			}else if(this.rewardTaskAmendData.point_config.rule_info){//指定了分类
				this.isSelectType=false;
				this.formData.rule_id = this.rewardTaskAmendData.point_config.rule_info.id;
				this.formData.rule_value = this.returnRuleVal(this.rewardTaskAmendData.point_config.rule_info.id,[]);
			}
			this.$nextTick(()=>{
				this.$nextTick(()=>{
					this.formData.task_remark = this.rewardTaskAmendData.task_remark; //任务详细描述
					this.formData.base_point = this.rewardTaskAmendData.point_config.base_point; //任务参考积分
				})
			})
		}
	},
	mounted() {
		this.config=this.$store.state.config;
		this.point_types = this.$getCache('types');
		this.getDepartment();
	},
	methods: {
		manage_confirm(val){
			if(val.employee.length>0){
				this.employeeList=val.employee;
			}else{
				this.employeeList=[];
			}
			this.manageScope_show = false
		},
		openUserSelect(){
			this.selected_manage.employee=this.employeeList;
			this.manageScope_show=true;
		},
		sub_manageScope(){
			this.$refs.Employee2.confirm();
		},
		// 规则分类变化关闭dewn
		ruleChange2(value) {
			const item = this.formData;
			if (value.length > 0) {
				item.rule_id = value[value.length - 1];
			} else {
				item.rule_id = '';
				item.rule_value = '';
			}
			this.$refs.ruleCascader.dropDownVisible=false;
		},
		isSelectTypeC(){
			let item = this.formData;
			this.max = 0;
			this.min = 0;
			this.treeName = '';
			item.tree_echo = [];
			item.task_remark = '';
			item.base_point = '0';
			item.range_type = 0;
			item.item_id = 0;
			item.rule_id=0;
			item.rule_value=[];
		},
		returnRuleVal(rule_id,arr){
			arr.unshift(rule_id)
			let list = this.trees_ab.rule_tree.filter(x => x.id == rule_id)
			if(list[0]&&list[0].pid != 0){
				this.returnRuleVal(list[0].pid,arr)
			}
			return arr;
		},
		ruleChange(aid){
			this.arr.unshift(aid);
			let list = this.trees_ab.rule_tree.filter(x => x.id == aid)
			if(list[0]&&list[0].pid == 0){
				return false
			}else{
				this.ruleChange(list[0].pid)
			}
		},
		from_ptId(val){
			let item = this.formData
			item.tree_echo = []
			this.max = 0;
			this.min = 0;
			this.treeName = ''
			item.task_remark = '';
			item.base_point = '0';
			item.range_type = 0;
			item.rule_id=0;
			item.rule_value=[];
		},
		handleFilesSuccess(response, file, fileList) {
			let fileListData=fileList.filter(e=>{
			  return e.url
			})
			this.fileFun(file, fileListData)
		},
		onFilePreView(file) {
			if (file.response) {
				window.open(file.response.url, '_blank');
			}
		},
		onFileRemove(file, fileList) {
			this.fileFun(file, fileList)
		},
		fileFun(file, fileList){
			this.fileList = fileList;
			this.formData.file_list = []
			fileList.forEach((item, index) => {
				this.formData.file_list.push(item.url);
			});
		},
		// 附件上传
		beforeUpload(file) {
			const isJPG = /^image\/(jpeg|png|jpg)$/.test(file.type);
			const isLt2M = file.size / 1024 / 1024 < 5;
			if (!isJPG) {
				this.$message.error('上传图片只能是 JPEG,PNG,JPG 格式!');
			}
			if (!isLt2M) {
				this.$message.error('上传图片大小不能超过 5MB!');
			}
			return isJPG && isLt2M;
		},
		
		trees_PtId(id) {
			return id == 2 ? 'A分' : 'B分'
		},
		ruleItemChange(val){
			let item = this.formData
			if(val.length>0){
				let id=val[val.length-2]
				let key='rule_'+id
				let list =this.trees_ab.item_list[key]; //当前选中的节点数据
				let data={};
				list.forEach(e=>{
					if(e.id==val[val.length-1]){
						data=e;
					}
				})
				item.task_remark = data.name;
				this.treeName = data.name;
				this.max = data.max_point * 1;
				this.min = data.min_point * 1;
				item.base_point = data.min_point;
				item.range_type = data.range_type;
				item.rule_id = data.rule_id;
				item.item_id = data.id;
			}else{
				item.task_remark = '';
				this.treeName = '';
				this.max = 0;
				this.min = 0;
				item.base_point = '0';
				item.range_type = 0;
				item.rule_id = 0;
				item.item_id = 0;
			}
		},



		deductionPoint(item){
			if (this.rewardTaskAmendData.point_config[item] != '0') {
				this.formData[item] = this.rewardTaskAmendData.point_config[item];
			} else {
				this.formData[item] = '';
			}
		},
		handleChange(val) {
			let data = [];
			for (let i in val) {
				data.push(val[i][val[i].length - 1]);
			}
			this.formData.dept_ids = data;
		},

		submitMembers(name) {
			this.$refs[name].confirm(); //调用组件的confirm（）；
		},
		//关闭
		handleClose(done) {
			done();
		},
		sub(formName) {
			this.$refs[formName].validate(valid => {
				if (valid) {
					this.saveFun(formName);
				}
			});
		},
		resetForm(formName) {
			this.echoGather = [];
			this.$emit('update:dialogVisible', false);
			this.$refs[formName].resetFields();
			this.fileList = [];
			this.executorName = '';
			this.reviewerName = '';
			this.dept_name = [];
			this.treeName = '';
			this.formData.tree_echo = [],
			this.dept_selected = { dept: [], employee: [] };
			this.formData.range_type=0;
			this.formData.rule_id=0;
			this.formData.item_id=0;
		},
		ahead_timeout(item,arr,codes,code){
			if(item){
				if(arr){
					if(Number(item) > Number(arr)){
						this.$message.warning(code +'上限不能小于每日'+ code)
						return true
					}
				}else{
					this.$message.warning(codes + '不为0时，' + code + '上限不能为空');
					// this.$message.warning(`设置${codes}时，需同时设置每天${code}及${code}上限`);
					return true;
				}
			}
		},
		deletedata(data,arr){
			data[arr] && data[arr] != 0 ?  '' : delete data[arr]
		},
		// 提交
		saveFun(formName) {
			let self = this;
			let data = JSON.parse(JSON.stringify(self.formData));
			data.task_remark ? '' : delete data.task_remark;
			data.timeout_deduction_point ? '' : delete data.timeout_deduction_point;
			if(!data.ahead_award_point && data.ahead_award_point_limit){
				// this.$message.warning('奖分上限不为0时每日奖分不能为空');
				this.$message.warning('设置提前奖分时，需同时设置每天奖分及奖分上限');
				return
			}else if(data.ahead_award_point && !data.ahead_award_point_limit){
				this.$message.warning('设置提前奖分时，需同时设置每天奖分及奖分上限')
				return
			}
			if(!data.timeout_deduction_point && data.timeout_deduction_point_limit){
				this.$message.warning('设置逾期扣分时，需同时设置每天扣分及扣分上限');
				return
			}else if(data.timeout_deduction_point && !data.timeout_deduction_point_limit){
				this.$message.warning('设置逾期扣分时，需同时设置每天扣分及扣分上限')
				return
			}
			if(this.ahead_timeout(data.ahead_award_point,data.ahead_award_point_limit,'提前奖分','奖分')) return
			if(this.ahead_timeout(data.timeout_deduction_point,data.timeout_deduction_point_limit,'逾期扣分','扣分')) return
			
			if(!this.$getIsCreator('creator')||this.$getUserData().point_config.point_limit.length!=0){
				let entry_limit =  Number(this.$getUserData().point_config.point_limit[1].point)
				if(entry_limit > 0){
					if(data.ahead_award_point > entry_limit || data.timeout_deduction_point > entry_limit ){
						this.$message.warning('每日奖扣分 不能超过您的奖扣分权限（您的奖扣分权限：' + entry_limit + '分）')
						return
					}
					if(data.ahead_award_point_limit > entry_limit*10 || data.timeout_deduction_point_limit > entry_limit*10 ){
						this.$message.warning('奖扣分上限不能超过' + entry_limit*10 + '分（您的奖扣分权限' + entry_limit + '分*10）')
						return
					}
				}
			}
			
			let add = ['ahead_award_point', 'ahead_award_point_limit', 'timeout_deduction_point', 'timeout_deduction_point_limit']
			add.forEach(item =>{
				this.deletedata(data,item)
			})
			if(data.base_point<=0){
				this.$message.warning('任务积分必须大于0，请调整后再发布');
				return;
			}
			if(data.chance==0){
				this.$message.warning('抢单人数不能为0');
				return;
			}
			if(data.chance>100){
				this.$message.warning('抢单人数不能超过100个');
				return;
			}
			if(this.employeeList.length>0){
				data.employee_ids=this.employeeList.map(e=> e.id).toString();
			}
			this.forTheTaskLoading = true;
			self.$axios('post', '/api/integral/task/update', data)
				.then(res => {
					if (res.data.code == 1) {
						this.$parent.get_list();
						this.resetForm('formData')
						self.$message.success(res.data.msg);
					} else {
						self.$message.error(res.data.msg);
					}
				})
				.finally(() => {
					this.echoGather = [];
					setTimeout(() => {
						this.forTheTaskLoading = false;
					}, 300);
				});
		},
		// 审核人
		reviewer_confirm(data) {
			this.reviewer_selected = { dept: [], employee: [] };
			this.formData.reviewer_id = '';
			this.reviewerName = '';
			if (data.employee !== null && data.employee.length != 0) {
				this.reviewer_selected = data;
				this.formData.reviewer_id = data.employee[0].id;
				this.reviewerName = data.employee[0].name;
			}
			this.show_reviewer_selector = false;
		},
		// 审核人 end
		// 部门可见
		dept_confirm(data) {
			this.dept_selected = { dept: [], employee: [] };
			this.formData.dept_ids = [];
			this.deptVisibleName = '';
			if (data.dept !== null && data.dept.length != 0) {
				this.dept_selected = data;
				data.dept.forEach(element => {
					this.formData.dept_ids.push(element.dept_id);
					this.deptVisibleName += element.dept_name + ',';
				});
			}
		},
		// 部门可见 end
		closeDialog() {
			this.echoGather = [];
			this.resetForm('formData')
		},

		//获取部门
		getDepartment() {
			this.$axios('get', '/api/department/tree').then(res => {
				this.dept_tree = this.getTreeData(res.data.data.list);
			});
		},

		getTreeDatas(data, item) {
			for (let i in data) {
				if (data[i].id == item) {
					if (data[i].parentid == 1) {
						this.oneruleId.unshift(data[i].id);
						this.echoGather.push(this.oneruleId);
						this.oneruleId = [];
						break;
					} else {
						this.oneruleId.unshift(data[i].id);
						this.getTreeDatas(this.dept_tree, data[i].parentid);
					}
				} else {
					if (data[i]._child) {
						this.getTreeDatas(data[i]._child, item);
					}
				}
			}
			return;
		},

		// 递归判断列表，把最后的children设为undefined
		getTreeData(data) {
			for (var i = 0; i < data.length; i++) {
				if (data[i]._child.length < 1) {
					// children若为空数组，则将children设为undefined
					data[i]._child = undefined;
				} else {
					// children若不为空数组，则继续 递归调用 本方法
					this.getTreeData(data[i]._child);
				}
			}
			return data;
		}
	}
};
</script>

<style lang="scss" scoped>
	.input-box{
		-webkit-appearance: none;
		background-color: #FFF;
		background-image: none;
		border-radius: 4px;
		border: 1px solid #DCDFE6;
		box-sizing: border-box;
		color: #606266;
		display: inline-block;
		font-size: inherit;
		height: 36px;
		line-height: 36px;
		outline: 0;
		padding: 0 15px;
		transition: border-color .2s cubic-bezier(.645,.045,.355,1);
		width: 360px;
		cursor: pointer;
	}
</style>
