<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: 'MasterCompanyRegisterSelectDevice',
		components: {
			Common,
			Loading,
			TopHeader
		},
		data() {
			return {
				pageTitle: '注册审核选择设备',

				isLoading: false,

				subForm: {
					ids: '',
					content: '',
					status: '',
					deviceIds: ''
				},

				checkVal: [],
				recordList: [],
				successVisible: false,

				hasSceneId4: this.$route.query.hasSceneId4
			}
		},
		created() {
			if (this.register_examine_form) {
				this.subForm = this.register_examine_form;
			}
			if ((!this.subForm.ids || !this.subForm.status) && this.hasSceneId4 == '0') {
				this.$router.go(-1);
			}

			//如果是居家养老场景则自动审核，不需要选设备
			if (this.hasSceneId4 == '1') {
				this.submit();
			}
		},
		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 && this.hasSceneId4 == '0') {
					mui.toast('请选择设备');
					return false;
				} else {
					return true;
				}
			},
			//提交
			submit() {
				this.subForm.bindDevices = this.checkVal.join(',');

				if (this.checkForm()) {
					this.isLoading = true;
					API_Company.doApply(this.subForm).then(response => {
						this.isLoading = false;
						//this.successVisible = true;
						mui.toast('处理成功');
						this.successTo();
					}).catch(error => {
						this.isLoading = false;
						mui.toast(error);
					})
				}
			},
			//成功后的点击
			successTo() {
				this.$router.go(-1);
			},
			asynCallBack() {

			},
		},
		mounted() {
			//获取设备列表
			this.getList();
		},
		destroyed() {},
		computed: {
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				register_examine_form: 'register_examine_form'
			})
		}
	}
</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>
