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

		<div class="mui-content">
			<div class="mui-content-padded">
				<h5>请选择设备</h5>
				<form class="mui-input-group">
					<div v-for="(item,index) in recordList" class="mui-input-row mui-checkbox">
						<label v-text="item.aliasName"></label>
						<input name="id" :value="item.id" type="checkbox" v-model="checkVal">
					</div>
				</form>
			</div>
		</div>
		<div class="fyy-footer">
			<div class="bindfyy-btn"><button type="submit" class="mui-btn mui-btn-primary" @click="submit">提交</button></div>
		</div>

		<div v-show="successVisible" class="mui-backdrop mui-active" style=""></div>
		<div v-show="successVisible" class="fyy-popup" @click="successTo">
			<span class="mui-icon iconfont icon-dui"></span>
			<p>提交成功</p>
		</div>
	</div>
</template>

<script>
	import * as API_Company from '@/apis/Master/company'
	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'
	export default {
		name: 'MasterCompanySelectDevice',
		components: {
			Common,
			Loading,
			TopHeader
		},
		data() {
			return {
				pageTitle: '增加人员选择设备',

				isLoading: false,

				subForm: {
					name: '',
					phone: '',
					faceImageUrl: '',
					bindDevices: '',
				},

				checkVal: [],
				recordList: [],
				successVisible: false,
			}
		},
		created() {
			if (this.add_person_data) {
				this.subForm = this.add_person_data;
			}
			if (!this.subForm.name || !this.subForm.phone || !this.subForm.faceImageUrl) {
				this.$router.push({
					name: 'MasterCompanyAddPerson',
					query: {}
				})
			}
		},
		methods: {
			//获取设备列表
			getList() {
				this.isLoading = true;
				API_Company.getDeviceList().then(response => {
					this.isLoading = false;
					this.recordList = response;
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//表单检测
			checkForm() {
				if (!this.subForm.bindDevices) {
					mui.toast('请选择设备');
					return false;
				} else {
					return true;
				}
			},
			//提交
			submit() {
				this.subForm.bindDevices = this.checkVal.join(',');

				if (this.checkForm()) {
					this.isLoading = true;
					API_Company.addPerson(this.subForm).then(response => {
						this.isLoading = false;
						this.successVisible = true;
					}).catch(error => {
						this.isLoading = false;
						mui.toast(error);
					})
				}
			},
			//成功后的点击
			successTo() {
				this.$router.push({
					name: 'Master',
					query: {}
				})
			},
			asynCallBack() {

			},
		},
		mounted() {
			//获取设备列表
			this.getList();
		},
		destroyed() {},
		computed: {
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				add_person_data: 'add_person_data'
			})
		}
	}
</script>

<style scoped src="$project/assets/css/sczpfyy.css"></style>
<style scoped src="$project/assets/css/xpwyfyy.css"></style>
<style src="$project/assets/css/iconfont.css"></style>
<style scoped>
</style>
