bindGate.vue 8.62 KB
<template>
    <el-dialog
      :title="$t('dialog.bindSite')"
      class="manage-dialog bind-middle-dialog"
      :visible.sync="bindVisible"
      :close-on-click-modal="false"
      @close="closeBindDialog">
        <el-row>
            <el-col :span="8" class="gate-col-left">
                <div class="gate-list-wrapper">
                    <div class="origin-input-box">
                        <input type="value" :placeholder="$t('pholder.Site')" @keyup="filterGateList" v-model="bindVal" class="origin-input__inner" />
                        <i class="el-icon-search bind-search"></i>
                    </div>
                    <el-scrollbar class="gate-list-scrollbar" wrap-class="list-scrollbar" :native="false" style="height:100%;">
                        <ul class="gate-list-left">
                            <li :class="item.className" @click="checkGate(index)" v-for="(item, index) in gateList" :key="item.value">
                                <span class="gate-item-text" :title="item.label">{{ item.label }}</span>
                                <i class="el-icon-circle-check checked-icon" v-if="item.isCheck"></i>
                            </li>
                            <li v-if="gateList.length == 0">{{ noDataText }}</li>
                        </ul>
                    </el-scrollbar>
                </div>
            </el-col>
            <el-col :span="6" class="gate-col-middle">
                <div class="bind-btn-box">
                    <div class="middle-wrapper">
                        <!-- <el-button class="bind-btn" @click="bindClick">出入口绑定<i class="el-icon-d-arrow-right el-icon--right right-icon"></i></el-button>
                        <el-button class="bind-btn" @click="bindClick2">外围绑定<i class="el-icon-d-arrow-right el-icon--right right-icon"></i></el-button> -->
						<el-button
						  class="bind-btn"
						  @click="bindClick('gates')"
						  :disabled="bindBtnDisabled">
						  {{$t('dialog.gateBind')}}<i class="el-icon-d-arrow-right el-icon--right right-icon"></i>
						</el-button>
						<el-button
						  class="bind-btn reverse-btn"
						  @click="bindClick('peripheral')"
						  v-if="$Project === 'store'"
						  :disabled="bindBtnDisabled">
						  {{$t('dialog.perBind')}}<i class="el-icon-d-arrow-right el-icon--right right-icon"></i>
						</el-button>
                    </div>
                </div>
            </el-col>
            <el-col :span="10" class="gate-col-right">
                <div class="gate-list-wrapper">
                    <p class="band-box">{{$t('dialog.arbindS')}}</p>
                    <el-scrollbar class="gate-list-scrollbar" wrap-class="list-scrollbar" :native="false" style="height:100%;">
                        <ul class="gate-list-left">
                            <li class="bind-gate-item" v-for="(gateItem, gateIndex) in bindGateList" :key="gateIndex" @click="cancelBind(gateIndex)">
                                <el-button class="gate-btn" :style="{ 'border-color': gateItem.btnColor }" :title="gateItem.label">{{ gateItem.label }}</el-button>
                                <i class="el-icon-circle-close gate-close-btn" :title="$t('dialog.cancelBind')"></i>
                            </li>
                        </ul>
                    </el-scrollbar>
                </div>
            </el-col>
        </el-row>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" class="dialog-btn dialog-confirm-btn" @click="bindVisible = false">{{$t('dialog.close')}}</el-button>
        </div>
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            bindVisible: false,
            bindVal: '',
            gateList: [],
            noDataText: '',
			bindGateList: [],
			bindBtnDisabled: false,
        }
    },
    methods: {
        dialogInit(row_id) {
            this.getMallGates(row_id);
            this.bindVisible = true;
        },
        getMallGates(mall_id) {
			this.gateList  = [];
			this.bindGateList  = [];
			this.$api.base.gate({
				mallId: mall_id,
				status: 1,
				// isMallGate: 0,
				// _t: Date.parse(new Date()) / 1000
			})
				.then((data) => {
					var result = data.data.data;
					if(result.length > 0) {
						// this.mallList = result;
						let obj = {};
						result.forEach(item => {
							obj = {};
							obj['label'] = item.name;
							obj['value'] = item.id;
							obj['isCheck'] = false,
							obj['className'] = 'gate-item';
							if(item.isMallGate == 1) {
								obj['btnColor'] = '#3BB8FF';
								this.bindGateList.push(obj);
							} else {
								this.gateList.push(obj);
							}
						})
					}
					if(this.gateList.length == 0) {
						this.noDataText = '数据为空'
					}
					window.sessionStorage.setItem('bindlist', JSON.stringify(this.gateList));
				})
				.catch(error => {})
		},
        filterGateList() {
			let sVal = this.bindVal.replace(/\s+/g, '').toLocaleLowerCase();
            let localList = JSON.parse(window.sessionStorage.getItem('bindlist'));
			if(sVal == '') {
				this.gateList = localList;
				return;
			}
			let filterArr = [];
			this.gateList.filter(item => {
				if(((item.label).toLocaleLowerCase()).indexOf(sVal) !== -1) {
					// if(!this.checkDup(filterArr, item.id)) {
						filterArr.push(item);
					// }
				}
			})
            if(filterArr.length == 0) {
				localList.forEach(item => {
					if(((item.label).toLocaleLowerCase()).indexOf(sVal) !== -1) {
						// if(!this.checkDup(filterArr, item.id)) {
							filterArr.push(item);
						// }
					}
				})
			}
			if(filterArr.length == 0) {
				this.noDataText = '未找到匹配数据';
			}
			this.gateList = filterArr;
		},
        checkDup(arr, val) {
			let isDup = false;
			arr.forEach(item => {
				if(item.id == val) {
					isDup = true;
				}
			})
			return isDup;
		},
        checkGate(index) {
			if(this.gateList[index].isCheck) {
				return;
			}
			this.curCheckGate = {};
			this.gateList.forEach(item => {
				item.isCheck = false;
				item.className = 'gate-item';
			})
			this.gateList[index].isCheck = true;
			this.gateList[index].className = 'gate-item gate-item-checked';
			this.curCheckGate = JSON.parse(JSON.stringify(this.gateList[index]));
		},
        bindClick(type) {
			if(type === 'peripheral') return;
			if(!this.curCheckGate.value) return;
			// return;
			let isDup = false, obj = {}, curIndex = null;
			this.$api.management.editGate(this.curCheckGate.value, {
				isMallGate: 1
			})
				.then((res) => {
					if(res.data.code == 200) {
						obj = JSON.parse(JSON.stringify(this.curCheckGate));
						obj['btnColor'] = '#3BB8FF';
						if(this.bindGateList.length) {
							this.gateList.forEach((item, index) => {
								if(item.value === obj.value) {
									curIndex = index;
							// 		isDup = true;
								}
							})
							// if(isDup) return;
							this.bindGateList.push(obj);
						} else {
							this.bindGateList.push(obj);
						}
						this.gateList.splice(curIndex, 1);
						window.sessionStorage.setItem('bindlist', JSON.stringify(this.gateList));
						if(this.gateList.length == 0) {
							this.noDataText = this.$t('echartsTitle.noData')
						}
						this.$parent.getTableData();
					} else {
						this.$message({
							showClose: true,
							type: 'info',
							message: this.$t('message.bind') + res.data.msg
						}) 
					}
				})
				.catch(err => {});
		},
		peripheralClick() {
			//
		},
        cancelBind(index) {
			let isDup = false, obj = {};
			this.$api.management.editGate(this.bindGateList[index].value, {
				isMallGate: 0
			})
				.then((res) => {
					if(res.data.code == 200) {
						obj = JSON.parse(JSON.stringify(this.bindGateList[index]));
						// this.gateList.forEach((item, i) => {
							// if(item.value === obj.value) {
								// this.gateList[i].isCheck = false;
								// this.gateList[i].className = 'gate-item';
								// obj.isCheck = false;
								// obj.className = 'gate-item';
								// isDup = true;
							// }
						// });
						// if(isDup) return;
						obj['isCheck'] = false;
						obj['className'] = 'gate-item';
						this.gateList.push(obj);
						this.bindGateList.splice(index, 1);
						// 删除列表中的元素
					} else {
						this.$message({
							showClose: true,
							type: 'info',
							message: this.$t('message.unbind') + res.data.msg
						})
					}
				})
				.catch(err => {})
			
		},
        closeBindDialog() {
			this.gateList.forEach(item => {
				item.isCheck = false;
				item.className = 'gate-item';
			})
			this.bindVal = '';
			this.bindGateList.length = 0;
		},
	},
	beforeDestroy() {
		window.sessionStorage.removeItem('bindlist');
	},
}
</script>

<style scoped>
	.bind-btn {
		width: 106px;
	}

	.bind-btn+.bind-btn {
		margin-left: auto;
	}
</style>