<template>
	<div>
		<common @asynCallBack="asynCallBack"></common>
		<top-header :pageTitle="pageTitle"></top-header>

		<div class="mui-content vongi-wordcard">
			<div class="mui-content-padded vongi-wordcard-padded">
				<h5>当前打卡班次</h5>
				<h3>{{tjForm.workAttendanceDate}}<span v-text="tjForm.workAttendanceTime"></span></h3>
			</div>
			<div class="mui-content-padded">
				<form class="mui-input-group">
					<div class="mui-input-row">
						<label><span class="colorfe616c">*</span>选择审批人</label>
						<div class="mui-navigate-right" @click="selectPerson">
							<span class="mui-btn mui-btn-block" type='button' v-text="approvalPersonName"></span>
						</div>
					</div>
					<div class="mui-input-row">
						<label><span class="colorfe616c">*</span>选择打卡类型</label>
						<div class="mui-navigate-right" @click="selectType">
							<span class="mui-btn mui-btn-block" v-text="typeName"></span>
						</div>
					</div>
				</form>
			</div>
			<div class="mui-content-padded">
				<h5><span class="colorfe616c">*</span>申请理由</h5>
				<div class="mui-input-row">
					<textarea v-model="tjForm.content" rows="5" placeholder="请输入"></textarea>
				</div>
			</div>
			<div class="mui-content-padded">
				<h5>上传照片</h5>
				<div class="fyy-upphoto">
					<div class="mui-col-xs-3 fyy-upphoto-close" v-for="(item,index) in picList">
						<img :src="item" />
						<a class="mui-icon mui-icon-closeempty" @click="delPic(item)"></a>
					</div>
					<div class="mui-col-xs-3" @click="chooseImage">
						<a><span class="mui-icon mui-icon-plusempty"></span></a>
					</div>
				</div>
			</div>
			<div class="vongi-btn">
				<button class="mui-btn mui-btn-primary" type="button" @click="submit">
					提交
				</button>
			</div>
		</div>

		<loading :visible="isLoading"></loading>
	</div>
</template>

<script>
	require('$project/assets/js/mui.picker.min.js');
	import * as API_Attendance from '@/apis/Master/attendance'
	import Common from '$project/components/Common.vue'
	import Loading from '$project/components/Loading.vue'
	import TopHeader from '$project/components/TopHeader.vue'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import * as WxJsApi from '$project/utils/wxJsApi'
	import * as types from '$project/store/mutation-types'
	export default {
		name: 'MasterAttendanceApply',
		components: {
			Common,
			Loading,
			TopHeader
		},
		data() {
			return {
				pageTitle: '补卡申请',

				isLoading: false,

				subForm: {
					workAttendanceId: this.$route.query.id
				},
				tjForm: {
					workAttendanceDate: '',
					workAttendanceTime: '',
					type: '2', //外勤打卡/补卡（1/2）
					content: '补卡申请',
					approvalPersonId: '',
					workAttendanceId: this.$route.query.id,
					photoFile: '',
					longitude: '',
					latitude: '',
				},
				//typeName: '',
				approvalPersonName: '',
				personList: [],
				picList: [],
			}
		},
		created() {

		},
		methods: {
			//获取详情
			getTime() {
				this.isLoading = true;
				API_Attendance.patchCard(this.subForm).then(response => {

					this.tjForm.workAttendanceDate = response.workAttendanceDate;
					this.tjForm.workAttendanceTime = response.workAttendanceTime;

					this.isLoading = false;
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//获取审核人列表
			getPsersonList() {
				this.isLoading = true;
                //补卡1，外出2，出差3 ，请假4
                var parameter={type:1};
                API_Attendance.getApprovalList(parameter).then(response => {

					this.personList = response.data;

					this.isLoading = false;

					//设置默认审核人
					this.setDefaultExaminePerson();
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//微信选择图片
			chooseImage() {
				WxJsApi.chooseImage().then(res => {
					var localData = res.localData;

					if (localData.indexOf('data:image') != 0) {
						//判断是否有这样的头部
						localData = 'data:image/jpeg;base64,' + localData
					}
					localData = localData.replace(/\r|\n/g, '').replace('data:image/jgp', 'data:image/jpeg')
					this.imgBase64 = localData;
					//显示裁剪图片
					//_this.showCropper(field);
					this.uploadpic();
				}).catch(error => {
					mui.toast(error);
				})
			},
			//上传图片
			uploadpic() {
				this.isLoading = true;
				WxJsApi.uploadPic(this.imgBase64).then(response => {
					this.isLoading = false;

					this.picList.push(response);
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//选择审核人
			selectPerson() {
				var _this = this;
				var picker = new mui.PopPicker();
				picker.setData(_this.syPersonList);
				if (this.default_examine_person) {
					picker.pickers[0].setSelectedValue(this.default_examine_person);
				}
				picker.show(function(selectItems) {
					_this.tjForm.approvalPersonId = selectItems[0].value;
					_this.approvalPersonName = selectItems[0].text;
					_this.set_default_examine_person(selectItems[0].value);
				})
			},
			//设置默认审核人
			setDefaultExaminePerson() {
				if (this.default_examine_person) {
					for (var i = 0; i < this.syPersonList.length; i++) {
						if (this.syPersonList[i].value == this.default_examine_person) {
							this.tjForm.approvalPersonId = this.default_examine_person;
							this.approvalPersonName = this.syPersonList[i].text;
						}
					}
				}
			},
			//类型选择
			selectType() {
				var picker = new mui.PopPicker();
				if (this.tjForm.type = 2) {
					var data = [{
						value: '2',
						text: '补卡'
					}]
				} else {
					var data = [{
						value: '1',
						text: '外勤打卡'
					}]
				}
				picker.setData(data);
				var _this = this;
				picker.show(function(selectItems) {
					_this.tjForm.type = selectItems[0].value;
					//_this.typeName = selectItems[0].text;
				})
			},
			//表单检测
			checkFrom() {
				if (!this.tjForm.type) {
					mui.toast('请选择打卡类型');
					return false;
				} else if (!this.tjForm.approvalPersonId) {
					mui.toast('请选择审批人');
					return false;
				} else if (!this.tjForm.content) {
					mui.toast('请输入申请内容');
					return false;
				} else {
					return true;
				}
			},
			//提交
			submit() {
				this.tjForm.photoFile = this.picList.join(',');
				if (this.checkFrom()) {
					this.isLoading = true;
					API_Attendance.postPatchCard(this.tjForm).then(response => {
						this.isLoading = false;

						mui.toast('提交成功');
						this.$router.go(-1);
					}).catch(error => {
						this.isLoading = false;
						mui.toast(error);
					})
				}
			},
			//删除图片
			delPic(item) {
				let picList = this.picList;
				let index = picList.indexOf(item);
				if (index > -1) {
					picList.splice(index, 1);
				}
				this.picList = picList;
			},
			asynCallBack() {

			},
			...mapMutations({
				set_default_examine_person: types.SET_DEFAULT_EXAMINE_PERSON,
			})
		},
		mounted() {
			//获取微信配置
			WxJsApi.getWxConfig();
			this.getTime();
			this.getPsersonList();
		},
		destroyed() {},
		computed: {
			syPersonList: {
				// getter
				get: function() {
					let list = [];
					this.personList.forEach(function(item, index) {
						list.push({
							value: item.id,
							text: item.personName
						});
					})
					return list;
				},
				// setter
				set: function(newValue) {
					console.log(newValue)
				}
			},
			typeName: {
				// getter
				get: function() {
					if (this.tjForm.type == '1') {
						return '外勤打卡';
					} else if (this.tjForm.type == '2') {
						return '补卡';
					} else {
						return '无';
					}
				},
				// setter
				set: function(newValue) {
					console.log(newValue)
				}
			},
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				default_examine_person: 'default_examine_person'
			})
		}
	}
</script>

<style scoped src="$project/assets/css/xpwyfyy.css"></style>
<style src="$project/assets/css/iconfont.css"></style>
<style src="$project/assets/css/mui.picker.min.css"></style>
<style scoped>
</style>
