webpackJsonp([7],{

/***/ 192:
/***/ (function(module, exports, __webpack_require__) {

"use strict";
/* WEBPACK VAR INJECTION */(function(Vue, Promise) {

var _regenerator = __webpack_require__(10);

var _regenerator2 = _interopRequireDefault(_regenerator);

var _config = __webpack_require__(9);

var _config2 = _interopRequireDefault(_config);

var _public = __webpack_require__(6);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
* v_index.js
* Version: 0.1
* User: zjx
* Date: 2017-09-29
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 门锁用户
*/
_public.iot.ready(init);
function init() {
    //通用 图片轮播、跑马灯、开关、按钮、dialog、loading组件
    _public.uPublic.componentsInit(['u-swipe-list', 'u-dialog', 'u-loading']);
    _public.uPublic.closeSlideBack();
    var app = new Vue({
        data: {
            //配置组件
            componentsConfig: initComponentsConfig(),
            //用户列表
            userList: [],
            userListIndex: null,
            loadShowFlag: false,
            loadFlag: false,
            loadText: '',
            pagesize: 10,
            tip: '无数据',
            extras: {},
            userListData: [],
            startId: 0,
            role: null,
            //当前用户id
            currentId: null
        },
        mounted: function mounted() {
            this.extras = _public.iot.navigator.getExtras();
            //获取用户列表
            this.role = this.extras.role;
            getUserList(this);
            var self = this;
            window.addEventListener('returnPage', function (event) {
                console.log("returnPage");
                self.setUserList([]);
                getUserList(self);
            });
        },

        methods: {
            getUserList: function getUserList() {
                return this.userList;
            },
            setUserList: function setUserList(list) {
                this.userList = list;
            },
            onBackTap: function onBackTap() {
                backTap();
            },
            onAddUserTap: function onAddUserTap() {
                addUserTap(this);
            },
            setLoadShowFlag: function setLoadShowFlag(number) {
                this.loadShowFlag = number >= this.pagesize;
            },
            getUserListData: function getUserListData() {
                return this.userListData;
            },
            setUserListData: function setUserListData(data) {
                this.userListData = data;
            },
            getStartId: function getStartId() {
                return this.startId;
            },
            setStartId: function setStartId(id) {
                this.startId = id;
            },
            getRole: function getRole() {
                return this.role;
            },
            setRole: function setRole(role) {
                this.role = role;
            },
            onTapLoad: function onTapLoad() {
                //      		tapLoad();
            },
            onTapUserList: function onTapUserList(index) {
                _onTapUserList(this, index);
            },
            onTapButton: function onTapButton(index) {
                tapButton(this, index);
            }
        }

    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }
    };
}

//获取用户列表
function getUserList(self) {
    _public.uComponents.showLoading(self);
    _public.iot.business.api.sendCustom('lock/getLockUser', {
        data: {
            device_id: self.extras.deviceId
        },
        success: function success(response) {
            console.log(response);
            var data = _public.uPublic.checkResponseData(response.data);
            console.log(data);
            if (data) {
                sortUsers(self, data);
            }
        },
        error: function error(_error) {
            console.log(_error);
            _public.uPublic.openRequestErrorAlert(self);
        },
        complete: function complete() {
            _public.uComponents.hideLoading(self);
        }
    });
}

function sortRule(a, b) {
    return a.role - b.role;
}

function getUserInfoMap() {
    return new Promise(function (resolve, reject) {
        _public.iot.business.websql.getMap('userinfo', function (res) {
            if (res) {
                resolve(res.id);
            } else {
                reject(res);
            }
        }, function (err) {
            console.log(err);
        });
    });
}

function sortUsers(self, data) {
    var users, manage, i;
    return _regenerator2.default.async(function sortUsers$(_context) {
        while (1) {
            switch (_context.prev = _context.next) {
                case 0:
                    _context.prev = 0;
                    _context.next = 3;
                    return _regenerator2.default.awrap(getUserInfoMap());

                case 3:
                    self.currentId = _context.sent;
                    _context.next = 9;
                    break;

                case 6:
                    _context.prev = 6;
                    _context.t0 = _context['catch'](0);

                    console.log(_context.t0);

                case 9:
                    users = [];
                    manage = [];

                    for (i = 0; i < data.length; i++) {
                        if (data[i].user_id == null || data[i].role && data[i].user_id != self.currentId) {
                            users.push(data[i]);
                        } else {
                            manage.push(data[i]);
                        }
                    }
                    manage = manage.sort(sortRule);
                    console.log(manage);
                    data = manage.concat(users);
                    setUserData(self, data);

                case 16:
                case 'end':
                    return _context.stop();
            }
        }
    }, null, this, [[0, 6]]);
}

function setUserData(self, data) {
    var list = [];
    for (var i = 0; i < data.length; i++) {
        list[i] = {
            value: data[i].id,
            role: data[i].role,
            imgClass: data[i].role == 0 ? 'user-list-image-admin' : data[i].user_id == null ? 'user-list-image-unbind' : 'user-list-image-bind',
            title: data[i].nickname,
            subtitle: data[i].user_id ? Vue.t('userList.binded') : Vue.t('userList.unbind')
        };
    }
    console.log(data);
    self.setUserListData(data);
    self.setUserList(list);
}

//返回上一页
function backTap() {
    _public.iot.navigator.aback();
}

mui.back = function () {
    _public.iot.navigator.fire(plus.webview.currentWebview().opener(), 'returnPage', {});
    console.log("fire");
    _public.iot.navigator.back();
};

//添加用户
function addUserTap(self) {
    _public.iot.navigator.openWindow({
        url: '../doorlockUsers/addUser.html',
        id: 'addUser',
        extras: {
            deviceId: self.getUserListData()[0].device_id,
            uuid: self.extras.uuid,
            lockId: self.extras.lockId,
            role: self.extras.role
        }
    });
}

// 点击组件列表 通知事件和当前点击列表下标
function _onTapUserList(self, listIndex) {
    _public.iot.navigator.openWindow({
        url: './doorlockUser.html',
        id: 'doorlockUser',
        extras: {
            id: self.getUserListData()[listIndex].id,
            uuid: self.extras.uuid,
            userId: self.getUserListData()[listIndex].user_id,
            lockId: self.extras.lockId,
            role: self.extras.role
        }
    });
    if (self.userListIndex != null) {
        self.userListIndex = null;
    }
}

//点击按钮
function tapButton(self, index) {
    console.log(index);
    //tap 删除该用户
    _public.uComponents.openConfirm(self, Vue.t('doorlockUser.deleteUserDialogTip'), { text: Vue.t('dialog.cancel'), callback: function callback() {} }, { text: Vue.t('dialog.delete'), callback: function callback() {
            deleteUser(self, index);
        } });
}

//删除用户
function deleteUser(self, index) {
    _public.uComponents.showLoading(self);
    _public.iot.business.api.sendCustom('lock/delLockUser', {
        data: {
            rel_id: self.getUserListData()[index].id,
            user_id: self.getUserListData()[index].user_id,
            device_sn: self.getUserListData()[index].device_id,
            device_id: self.extras.lockId
        },
        success: function success(response) {
            console.log(response);
            if (response.data.success) {
                _public.uComponents.openAlert(self, Vue.t('doorlockUser.deleteSuccess'), {
                    text: Vue.t('btn.confirm'), callback: function callback() {
                        self.setUserList([]);
                        getUserList(self);
                    }
                });
            }
        },
        error: function error(_error2) {
            console.log(_error2);
            _public.uComponents.openAlert(self, Vue.t('doorlockUser.deleteFailure'), {
                text: Vue.t('btn.confirm'), callback: function callback() {}
            });
        },
        complete: function complete() {
            _public.uComponents.hideLoading(self);
        }
    });
}
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(4)["default"], __webpack_require__(14)))

/***/ }),

/***/ 302:
/***/ (function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(192);


/***/ })

},[302]);
//# sourceMappingURL=index.js.map