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


		<div class="mui-content vongi-fpbm">
			<div class="vongi-bread">
				<template v-for="(item,index) in navList">
					<a :class="navCanClick(item,index)?'color4fc5f7':''" @click="reloadItem(item,index)" v-text="item.name"></a>
					<span v-if="index!=navList.length-1" class="mui-icon mui-icon-forward"></span>
				</template>
			</div>
			<ul class="mui-table-view">
				<li v-for="(item,index) in companyList" class="mui-table-view-cell flew-sp flew-items">
					<div class="mui-radio mui-left mui-ellipsis">
						<input name="radio" :value="item.id" type="radio" v-model="subForm.companyId">
						{{item.name}}
					</div>
					<a v-if="item.hasNext" @click="reloadNext(item)" class="color4fc5f7">
						<span class="iconfont icon-xiaji"></span>
						下级
					</a>
					<!--灰色状态-->
					<a v-else class="color999">
						<span class="iconfont icon-xiaji"></span>
						下级
					</a>
				</li>
			</ul>
		</div>
		<div class="fyy-footer vongi-footer">
			<h5 class="margin10">已选择部门：</h5>
			<div class="vongi-yjbtn" v-if="companyName">
				<button type="submit" class="mui-btn mui-btn-primary mui-btn-outlined" v-text="companyName"></button>
			</div>
		</div>

		<loading :visible="isLoading"></loading>
	</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 NavMenu from '@/components/NavMenu.vue'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import * as types from '$project/store/mutation-types'
	export default {
		name: 'MasterCompanyRegisterSelectDepartment',
		components: {
			Common,
			Loading,
			TopHeader,
			NavMenu
		},
		data() {
			return {
				isLoading: false,

				pageTitle: '分配部门',

				companyId: '',
				companyList: [],
				nowLevel: 0,
				navList: [{
					name: '首页',
					id: '',
					hasNext: false,
				}],

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

				hasSceneId4: this.$route.query.hasSceneId4 ? this.$route.query.hasSceneId4 : '0'
			}
		},
		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);
			}
		},
		methods: {
			//获取公司部门列表
			getCompanyList() {
				this.isLoading = true;
				API_Company.getExamineCompanyList(this.companyId).then(response => {

					this.companyList = response.list;
					if (this.nowLevel == 0) {
						this.navList[0]['hasNext'] = response.list.length > 0 ? true : false;
					}

					this.isLoading = false;
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//加载下级
			reloadNext(item) {
				this.navList.push(item)
				this.nowLevel++;
				this.companyId = item.id;
			},
			//面包屑选择加载
			reloadItem(item, index) {
				this.navList = this.navList.slice(0, index + 1);
				this.nowLevel = index;
				this.companyId = item.id;
			},
			//确认
			doRightLink() {
				if (!this.subForm.companyId || !this.companyName) {
					mui.toast('请选择所属公司或部门');
				} else {
					this.set_register_examine_form(this.subForm);
					this.$router.push({
						name: 'MasterCompanyRegisterSelectDevice',
						query: {
							hasSceneId4: '0'
						}
					})
				}
			},
			//判断面包屑是否可点击
			navCanClick(item, index) {
				if (index > 0) {
					if (index == this.nowLevel) {
						return false;
					} else {
						return true;
					}
				} else {
					return true;
				}
			},
			asynCallBack() {

			},
			...mapMutations({
				set_register_examine_form: types.SET_REGISTER_EXAMINE_FORM,
			})
		},
		mounted() {
			this.getCompanyList();
		},
		destroyed() {

		},
		computed: {
			//公司名称
			companyName: {
				// getter
				get: function() {
					for (var i = 0; i < this.companyList.length; i++) {
						if (this.companyList[i]['id'] == this.subForm.companyId) {
							return this.companyList[i]['name'];
						}
					}
					return '';
				},
				// setter
				set: function(newValue) {
					console.log(newValue)
				}
			},
			rightLink: {
				// getter
				get: function() {
					var link = {
						show: true,
						icon: this.companyName ? 'mui-btn mui-btn-primary' : 'mui-btn mui-btn-grey mui-btn-primary',
						style: 'width: 55px;height: 25px;margin-top:5px;margin-right: 0px;line-height: 5px;color: #fff;font-size:16px;border-radius:20px;',
						title: '确认'
					}
					return link;
				},
				// setter
				set: function(newValue) {
					console.log(newValue)
				}
			},
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				person_data: 'person_data',
				person_popedom: 'person_popedom',
				register_examine_form: 'register_examine_form'
			})
		},
		watch: {
			'companyId': function(val) {
				this.getCompanyList();
			},
		}
	}
</script>

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