starter.controller('AccountManageCtrl', function ($scope, global, showPopup, CompManage) {
    var beforeEnter = $scope.$on("$ionicView.beforeEnter", function () {
        $scope.show = {
            isshow: false
        };
        global.fetch_user().then(function (data) {
            $scope.is_showcurrent = (global.user.compno!=null);
            if (global.user.token != "") {
                CompManage.get_Compdata().then(function (data) {
                    data = _.filter(data, function (item) {
                        return item.usr_status == 2;
                    });
                    if (data.length > 0) {
                        _.each(data, function (c) {
                            c.check = false;
                            if (c.id == global.user.compno) {
                                $scope.currentcomp = c;
                                c.check = true;
                            }
                        });
                    }
                    $scope.compdata = data;
                }, function (err) {
                    $scope.is_showcurrent = false;
                    alert(JSON.stringify(err))
                });
            } else {
                $scope.compdata = [];
            }
        });
    });

    $scope.change = function (comp) {
        if ($scope.compdata.length > 0) {
            if ($scope.currentcomp != comp) {
                showPopup.showLoading(1, '', false);
                var password = global.user.password;
                CompManage.post_authcheck(global.user.im_usrid, comp.id).then(function (data) {
                    $scope.currentcomp = comp;
                    $scope.show.isshow = false;
                    _.each($scope.compdata, function (_comp) {
                        if (comp.id != _comp.id) {
                            _comp.check = false;
                        }
                    });
                    global.user = data;
                    global.user.password = password;
                    CompManage.store_user().then(function (data) {
                        console.log(data);
                        showPopup.hideLoading();
                    }, function (error) {
                        showPopup.hideLoading();
                        console.log('store error:' + JSON.stringify(error));
                    });
                }, function (error) {
                    showPopup.hideLoading();
                    alert(JSON.stringify(error));
                });
            }
        }
    };

    $scope.goBack = function () {
        global.goBack();
    };

    $scope.showcomplist = function () {
        if ($scope.compdata.length > 0) $scope.show.isshow = !$scope.show.isshow;
    }
})

.controller('ManageCompCtrl', function ($scope, $state, $ionicPopup, $rootScope, $ionicHistory, $q, global, ImageManage, showPopup, CompManage) {
    $scope.newcompname = {
        "compname": ""
    };
    var beforeEnter = $scope.$on("$ionicView.beforeEnter", function () {
        $scope.loading = ($scope.compdata == undefined);
        global.fetch_user().then(function (data) {
            if (global.user.token != "") {
                getmycomplist();
            } else {
                $scope.compdata = [];
                $scope.loading = false;
            }
            $scope.popup = {
                isSetPopup: false
            };
        });
    });

    $scope.tocompinfo = function (compid, op) {
        $state.go('compinfo', {
            'compid': compid,
            'op': op
        });
    };

    $scope.showaddcompmodel = function () {
        showPopup.modalTemplate('templates/createcomp-modaltemplate.html', 'slide-in-right', $scope).then(function (modal) {
            $rootScope.commons.modal = modal;
            $rootScope.commons.modal.show();
        });
    };

    $scope.cancel = function () {
        $rootScope.commons.modal.hide();
        $scope.logourl = '../../../img/logo.png';
    };

    $scope.createcomp = function () {
        var newcompdata = {"compname": "", "cellphone": global.user.cellphone, "username": "", "imid": "", "password": global.user.password};
        newcompdata.username = global.user.usrname;
        newcompdata.compname = $scope.newcompname.compname;
        newcompdata.imid = global.user.im_usrid;
        showPopup.showLoading(1, '正在提交', true, $scope.logourl != "../../../img/logo.png" ? 20000 : 10000);
        CompManage.post_Register(newcompdata).then(function (compdata) {
            if ($scope.logourl != "../../../img/logo.png") {
                if (global.user.token == "") {
                    var password = global.user.password;
                    CompManage.post_authcheck(global.user.im_usrid, compdata.compid).then(function (data) {
                        global.user = data;
                        global.user.password = password;
                        uploadcomplogo(compdata);
                        CompManage.store_user().then(function () {
                        }, function (error) {
                            showPopup.hideLoading();
                            console.log('store error:' + JSON.stringify(error));
                        });
                    }, function (err) {
                        showPopup.hideLoading();
                        alert('post_authcheck error:' + JSON.stringify(err))
                    });
                } else {
                    uploadcomplogo(compdata)
                }
            } else {
                get_authcheckInfo(compdata.compid);
            }
        }, function (e) {
            showPopup.hideLoading();
            if (_.has(e.data, 'msg')) showPopup.PopupWindow(0, e.data.msg, false, 2000);
            else alert(JSON.stringify(e));
        });
    };

    $scope.$on('$destroy', function () {
        if ($rootScope.commons.modal != null) {
            $rootScope.commons.modal.remove();
        }
        beforeEnter = null;
    });

    $scope.logourl = '../../../img/logo.png';

    $scope.editlogo = function () {
        $scope.popup.optionsPopup = showPopup.showSelectImgPopup(Camera, ImagePicker, $scope);
        $scope.popup.isSetPopup = true;
    };

    function uploadcomplogo(comp) {
        ImageManage.uploadImage($scope.logourl, 'comp', comp.compid, 'complogo').then(function (res) {
            $q.all(res).then(function (res1) {
                get_authcheckInfo(comp.id);
            }, function (error) {
                showPopup.hideLoading();
                alert('upload error:' + JSON.stringify(error));
            });
        }, function (err) {
            showPopup.hideLoading();
            alert('upload token error:' + JSON.stringify(err));
        });
    }

    function get_authcheckInfo(compid) {
        if (global.user.token == '') {
            var password = global.user.password;
            CompManage.post_authcheck(global.user.im_usrid, compid).then(function (data) {
                global.user = data;
                global.user.password = password;
                getmycomplist();
                CompManage.store_user().then(function (data) {
                    console.log(data);
                    $ionicHistory.goBack();
                    showPopup.hideLoading();
                }, function (error) {
                    showPopup.hideLoading();
                    console.log('store error:' + JSON.stringify(error));
                });
            }, function (err) {
                showPopup.hideLoading();
                alert('post_authcheck error:' + JSON.stringify(err))
            });
        } else {
            showPopup.hideLoading();
            if ($rootScope.commons.modal != null) $rootScope.commons.modal.hide();
            getmycomplist();
        }
    }

    function getmycomplist() {
        CompManage.get_Compdata().then(function (data) {
            _.each(data, function (comp) {
                if (comp.file_thumbnail_path == null) {
                    comp.file_thumbnail_path = $scope.logourl;
                }
                var admin = _.find(comp.compadmin, function (adminitem) {
                    return adminitem.user_id == global.user.usrid;
                });
                comp.op = admin == undefined ? 0 : 1;
                comp.compadmins = _.pluck(comp.compadmin, 'username').join(',');
            });
            $scope.compdata = _.sortBy(data, 'op').reverse();
            $scope.loading = false;
            $scope.newcompname.compname = "";
            $scope.logourl = "../../../img/logo.png"
        }, function (err) {
            $scope.loading = false;
            alert(JSON.stringify(err));
        });
    }

    var ImagePicker = function () { //打开相册
        $scope.popup.optionsPopup.close();
        ImageManage.ImagePicker_getPictures(1).then(function (data) {
            if (data.length == 0) return;
            $scope.logourl = data[0];
        });
    };

    var Camera = function () {
        $scope.popup.optionsPopup.close();
        ImageManage.Camera_getPicture(true).then(function (result) {
            $scope.logourl = result;
        });
    };
})

.controller('JoinCompCtrl', function ($scope, $ionicHistory, $timeout, CompManage, global, showPopup) {
    $scope.data = {
        "compid": ""
    };
    var compdata = _.pick(global.user, 'cellphone', 'password');
    compdata.username = global.user.usrname;
    compdata.imid = global.user.im_usrid;
    $scope.addedcomp = {};
    $scope.is_show_jointip = false;
    $scope.joincomp = function () {
        var complist = CompManage.getComplist();
        var comp = _.findWhere(complist, {'id': $scope.data.compid});
        compdata.compid = $scope.data.compid;
        if (comp == undefined) { //未加入过
            showPopup.showLoading(1, '正在提交', true);
            CompManage.post_Joinin(compdata).then(function (data) {
                showPopup.hideLoading();
                $scope.is_show_jointip = true;
                $timeout(function () {
                    $scope.is_show_jointip = false;
                }, 5000);
            }, function (e) {
                showPopup.hideLoading();
                if (_.has(e.data, 'msg')) showPopup.PopupWindow(0, e.data.msg, false, 2000);
                else alert(JSON.stringify(e));
            });
        } else {
            if (comp.file_thumbnail_path == "") {
                comp.file_thumbnail_path = '../../../img/logo.png'
            }
            showPopup.PopupWindow(0, '你已加入该公司！', false, 2000);
            $scope.addedcomp = comp;
        }
    };
})

.controller('CompInfoCtrl', function ($scope, $ionicPopup, $state, $ionicHistory, $q, showPopup, ImageManage, CompManage, global) {
    var compid = $state.params['compid'];
    $scope.op = $state.params['op'];
    var beforeEnter = $scope.$on("$ionicView.beforeEnter", function () {
        var compdata = CompManage.getComplist();
        $scope.rdata = _.find(compdata, function (comp) {
            return comp.id == $state.params['compid'];
        });
        console.log($scope.rdata);
        $scope.data = _.clone($scope.rdata);
        $scope.popup = {
            isPopup: false
        };
    });

    $scope.$on("$destroy", function () {
        beforeEnter = null;
    });

    $scope.selectphoto = function () {
        if ($scope.op == 0) {
            return;
        }
        $scope.popup.optionsPopup = showPopup.showSelectImgPopup(Camera, ImagePicker, $scope);
        $scope.popup.isPopup = true;
    };

    $scope.disbandcomp = function () {
        showPopup.confirm('确定解散公司吗?', '确定', '取消').then(function (res) {
            if (res) {
                //解散
                showPopup.showLoading(1, '', false);
                CompManage.dismiss_comp($scope.data.id).then(function () {
                    CheckComp();
                    // alert('dismiss_comp success')
                }, function (error) {
                    showPopup.hideLoading();
                    alert('error disbandcomp:' + JSON.stringify(error));
                });
            }
        })
    };

    $scope.leavecomp = function () {
        showPopup.confirm('确定退出公司吗?', '确定', '取消').then(function (res) {
            if (res) {
                if ($scope.op == 1) { //admin
                    if ($scope.rdata.compadmin.length > 1) { //admin count>=2
                        //退出
                        showPopup.showLoading(1, '', false);
                        CompManage.leave_comp($scope.data.id).then(function () {
                            CheckComp();
                        }, function (error) {
                            showPopup.hideLoading();
                            alert('error leavecomp:' + JSON.stringify(error));
                        })
                    } else {
                        showPopup.PopupWindow(0, '你是该公司唯一的管理员不允许退出！', false);
                    }
                } else {
                    //退出
                    showPopup.showLoading(1, '', false, 20000);
                    CompManage.leave_comp($scope.data.id).then(function () {
                        CheckComp();
                    }, function (error) {
                        showPopup.hideLoading();
                        alert('error leavecomp:' + JSON.stringify(error));
                    });
                }
            }
        })
    };

    $scope.showeditpopup = function () {
        if ($scope.op == 0) {
            return;
        }
        $scope.editdata = _.clone($scope.data);
        var myPopup = $ionicPopup.show({
            template: '<div class="popup-edit-comp" ><input type="text" ng-model="editdata.name" ><label ></label></div>',
            title: '<div><h5>修改公司名称</h5></div>',
            scope: $scope,
            buttons: [{
                text: '取消',
                onTap: function () {
                    return false;
                }
            }, {
                text: '<b>保存</b>',
                type: 'button-positive',
                onTap: function (e) {
                    if (!$scope.editdata.name) {
                        e.preventDefault();
                    } else {
                        return true;
                    }
                }
            }]
        });

        myPopup.then(function (res) {
            if (res) {
                // alert('editComp');
                if ($scope.editdata.name != $scope.data.name) {
                    CompManage.editComp($scope.editdata).then(function (data) {
                        $scope.data.name = $scope.editdata.name;
                        // alert(1);
                    }, function (err) {
                        alert('Error:' + JSON.stringify(err));
                    });
                }
            }
        });
    };
    $scope.image_list = [];

    var ImagePicker = function () { //打开相册
        $scope.popup.optionsPopup.close();
        ImageManage.ImagePicker_getPictures(1).then(function (result) {
            if (result.length == 0) return;
            uploadimage(result[0]);
        });
    };

    var Camera = function () {
        $scope.popup.optionsPopup.close();
        ImageManage.Camera_getPicture(true).then(function (result) {
            uploadimage(result);
        });
    };

    function CheckComp() {
        var compdata = CompManage.getComplist();
        compdata.splice(_.findIndex(compdata, {'id': $scope.data.id}), 1);
        if (compdata.length > 0 && global.user.compno == $scope.data.id) { //退出/解散当前登录的公司
            var comp = compdata[0];
            var password = global.user.password;
            CompManage.post_authcheck(global.user.im_usrid, comp.id).then(function (data) {
                global.user = data;
                global.user.password = password;
                showPopup.hideLoading();
                $ionicHistory.goBack();
                CompManage.store_user().then(function (data) {
                    console.log(data);
                }, function (error) {
                    console.log('store error:' + JSON.stringify(error));
                });
            });
        } else if (compdata.length == 0) { //用户没有公司时
            global.user.token = "";
            global.user.compno = "";
            global.user.compname = "";
            global.user.deptno = "";
            global.user.deptname = "";
            global.user.roleid = "";
            showPopup.hideLoading();
            $ionicHistory.goBack();
            CompManage.store_user().then(function (data) {
                console.log(JSON.stringify(data));
            }, function (error) {
                console.log('store error:' + JSON.stringify(error));
            });
        } else {
            showPopup.hideLoading();
            $ionicHistory.goBack();
        }
    }

    function uploadimage(result) {
        showPopup.showLoading(1, '', false, 20000);
        ImageManage.uploadImage(result, 'comp', $scope.data.id, 'complogo').then(function (res) {
            $q.all(res).then(function (res1) {
                showPopup.hideLoading();
                $scope.data.file_thumbnail_path = JSON.parse(res1[0].response).file_thumbnail_path;
            }, function (error) {
                showPopup.hideLoading();
                alert('upload error:' + JSON.stringify(error));
            })
        }, function (error) {
            alert('get token error:' + JSON.stringify(error));
            showPopup.hideLoading();
        });
    }
})

.controller('ApplyCheckCtrl', function ($scope, $ionicPlatform, $cordovaPreferences, $location, global, CompManage, showPopup) {
    var compid = $location.search().compid;
    global.fetch_user().then(function (data) {
        getAuditUsers();
    }, function (error) {
        alert(JSON.stringify(error));
    });

    $scope.check = function (user, op) { //todo:y=0;n=1
        // alert(JSON.stringify($scope.audituserlst))
        showPopup.showLoading(1, '正在提交', false);
        CompManage.auditUser(user, op, compid).then(function (data) {
            showPopup.hideLoading();
            $scope.audituserlst.splice(_.indexOf($scope.audituserlst, user), 1);
        }, function (err) {
            showPopup.hideLoading();
            alert(JSON.stringify(err));
        });
    };
    $scope.doRefresh = function () {
        getAuditUsers();
    };

    $scope.goBack = function () {
        global.goBack();
    }

    function getAuditUsers() {
        if (global.user.token != "") {
            CompManage.getUserAuditstatus(1, compid).then(function (data) {
                $scope.audituserlst = data;
            }, function (err) {
                alert(JSON.stringify(err));
            }).finally(function () {
                $scope.$broadcast('scroll.refreshComplete');
            });
        }
    }
})

.controller('ContactsCompanylstCtrl', function ($scope, $cordovaPreferences, $ionicPlatform, $state, global, Dept, CompManage) {
    $scope.complistdata = [];
    $scope.logourl = '../../../img/logo.png';
    $scope.loading = true;
    var beforeEnter = $scope.$on('$ionicView.beforeEnter', function () {
        Dept.setdeptlst([]);
        // alert(Dept.all().length);
    });
    $scope.init = function () {
        global.fetch_user().then(function (data) {
            if (global.user.token != "") {
                CompManage.get_Compdata().then(function (data) {
                    // alert(JSON.stringify(data));
                    _.each(data, function (c) {
                        if (c.file_thumbnail_path == null) {
                            c.file_thumbnail_path = $scope.logourl;
                        }
                    })
                    $scope.complistdata = data;
                    $scope.loading = false;
                });
            } else {
                $scope.loading = false;
            }
        }, function (error) {
            $scope.loading = false;
            alert(JSON.stringify(error));
        });
    };

    $scope.goBack = function () {
        global.goBack();
    }

    $scope.tocompuser = function (compid, compname) {
        $state.go('deptlst', {
            id: -1,
            name: compname,
            compid: compid
        })
    };

    function getcompdata() {
        if (global.user.token != "") {
            CompManage.get_Compdata().then(function (data) {
                $scope.complistdata = data;
            });
        }
    }
})

.controller('DeptCtrl', function ($scope, $state, $http, $stateParams, Dept, $ionicViewSwitcher) {
    $scope.deptlst = [];
    $scope.emplst = [];
    $scope.titleName = '';
    $scope.checked = false;
    $scope.titleName = $stateParams.name;
    $scope.deptid = -1;
    $scope.$on('$ionicView.beforeEnter', function () {
        $scope.deptid = setdeptid();
        if (Dept.all().length == 0) {
            $scope.loading = true;
            Dept.getDep($stateParams.compid).then(function (data) {
                $scope.deptid = setdeptid();
                data = _.filter(data, function (item) {
                    return item.parent == $scope.deptid;
                });
                $scope.dept_data = data;
                $scope.deptlst = data;
                getemp();
            }, function (err) {
                $scope.loading = false;
            });
        } else {
            $scope.loading = false;
            var data = Dept.getChildDept($scope.deptid);
            $scope.dept_data = data;
            $scope.deptlst = data;
            getemp();
        }
    });

    function setdeptid() {
        var deptid = parseInt($stateParams.id);
        if (Dept.all().length > 0) {
            if (deptid < 0) {
                deptid = _.find(Dept.all(), function (item) {
                    return item.level == 0
                }).id;
            }
        }
        return deptid;
    }

    function getemp() {
        Dept.getEmp($scope.deptid, $stateParams.compid).then(function (data) {
            console.log('user json:' + JSON.stringify(data));
            $scope.emplst = data;
            $scope.loading = false;
        }, function (err) {
            $scope.loading = false;
            alert(JSON.stringify(err));
        });
    }

    $scope.deptdisplay = function () {
        if ($scope.deptlst.length > 0) {
            return true;
        } else {
            return false;
        }
    };

    $scope.empdisplay = function () {
        if ($scope.emplst.length > 0) {
            return true;
        } else {
            return false;
        }
    };

    $scope.search = function () {
        $state.go('search');
    };

    $scope.editDep = function () {
        $state.go('editDep', {
            id: $scope.deptid,
            compid: $stateParams.compid,
            name: $stateParams.name
        });
        $ionicViewSwitcher.nextDirection("back");
    };

    $scope.cancel = function () {
        $state.go('addGroup2');
    };

    $scope.lookMsg = function (im_id, name, cellphone) {
        var data = {
            'im_id': im_id,
            'name': name,
            'cellphone': cellphone
        }
        console.log(data)
        if (window.cordovaLinker != undefined) {
            window.cordovaLinker.startChat(data, function (s) {
                console.log(s);
            }, function (err) {
                console.error(err);
            });
        }
    };

    $scope.todept = function (dept) {
        $state.go('deptlst', {id: dept.id, name: dept.depname, compid: $stateParams.compid})
    };
})

.controller('EditDepCtrl', function ($scope, $ionicPopup, $stateParams, $timeout, $ionicHistory, Dept, showPopup, Tool) {
    $scope.deptlst = [];
    $scope.titleName = $stateParams.name;
    var del_deptids = [];
    var dept_data = [];
    var compid = $stateParams.compid;
    var beforeenter = $scope.$on('$ionicView.beforeEnter', function () {
        $scope.deptlst = Tool.cloneObj(Dept.getChildDept($stateParams.id));
        dept_data = Tool.cloneObj($scope.deptlst);
    });

    $scope.add = function () {
        $timeout(function () {
            $scope.deptlst.push({
                depname: '',
                parent_id: $stateParams.id
            })
        })
    };
    $scope.remove = function (dept, index) {
        if (_.has(dept, 'id')) del_deptids.push(dept.id);
        $scope.deptlst.splice(index, 1);
    };

    $scope.save = function () {
        var deptlst = _.filter($scope.deptlst, function (dep) {
            return Tool.trim(dep.depname).length > 0;
        });
        if (deptlst.length > _.uniq(_.pluck(deptlst, 'depname')).length) {
            showPopup.PopupWindow(0, '名称不能重复!', false);
            return;
        }
        var depts = _.partition(deptlst, 'id');
        var up_depts = _.map(_.difference(stringify(depts[0]), stringify(dept_data)), JSON.parse);
        var add_depts = depts[1];
        if (del_deptids.length > 0 || add_depts.length > 0 || up_depts.length > 0) {
            showPopup.showLoading(1, '', false);
            $scope.isdisabled_ok = true;
            Dept.save(up_depts, del_deptids, add_depts, compid).then(function (data) {
                Dept.setdeptlst([]);
                $ionicHistory.goBack();
            }, function (err) {
                console.log(err);
            }).finally(function (f) {
                showPopup.hideLoading();
                $scope.isdisabled_ok = false;
            }); //修改、删除、添加;
        }
    };

    function stringify(data) {
        return _.map(data, JSON.stringify);
    }

    $scope.cancel = function () {
        if (JSON.stringify($scope.deptlst) != JSON.stringify(dept_data)) {
            showPopup.confirm('是否退出编辑?', '是', '否').then(function (res) {
                if (res) {
                    $ionicHistory.goBack();
                }
            });
        } else $ionicHistory.goBack();
    };
})

.controller('PersonInfoCtrl', function ($scope, $state, $rootScope, $ionicPopup, $ionicModal, $q, $timeout, $ionicHistory, formatFilter, Dept, showPopup, global, ImageManage, Member) {
    $scope.personinfo = {value: ''};
    $scope.selectdatas = [{
        name: '女',
        id: 0
    }, {
        name: '男',
        id: 1
    }];
    var beforeEnter = $scope.$on("$ionicView.beforeEnter", function () {
        global.fetch_user().then(function (data) {
            $scope.loading = $scope.userinfo == undefined;
            Dept.getUsrDetail().then(function (data) {
                $scope.userinfo = angular.copy(data);
                $scope.userinfo.photo = data.photo == null ? '../../../img/panda.png' : data.photo;
                $scope.userinfo.compname = global.user.compname;
                $scope.userinfo.compno = global.user.compno;
                $scope.loading = false;
            }, function (err) {
                alert(JSON.stringify(err))
            });
        });
        $scope.popup = {
            isPopup: false,
            optionsPopup: null
        };
    });

    $scope.selectimg = function () {
        $scope.popup.optionsPopup = showPopup.showSelectImgPopup(Camera, ImagePicker, $scope);
        $scope.popup.isPopup = true;
    };

    $scope.editInfo = function (title, input_type, field_name, value) {
        $scope.userinfo[field_name] = value == undefined ? '' : value;
        $scope.personinfo.value = value == undefined ? '' : value;
        var template = formatFilter('<div rj-close-back-drop><label class="item item-input"> <input type="{0}" ng-model="personinfo.value" placeholder="输入{1}"></label></div>', input_type, title);
        $scope.popup.optionsPopup = $ionicPopup.show({
            template: template,
            title: '<div><h4>' + title + '</h4></div>',
            scope: $scope,
            buttons: [{
                text: '取消',
                onTap: function () {
                    return false;
                }
            }, {
                text: '<b>保存</b>',
                type: 'button-positive',
                onTap: function (e) {
                    if (!$scope.personinfo.value) e.preventDefault(); else  return true;
                }
            }]
        });
        $scope.popup.optionsPopup.then(function (res) {
            if (res) {
                $scope.userinfo[field_name] = $scope.personinfo.value;
                $scope.updateInfo();
                $scope.popup.optionsPopup.close();
            }
        });
        $scope.popup.isPopup = true;
    };

    var ImagePicker = function () {
        $scope.popup.optionsPopup.close();
        ImageManage.ImagePicker_getPictures(1).then(function (data) {
            if (data.length == 0) return;
            uploadimg(data);
        });
    };

    var Camera = function () {
        $scope.popup.optionsPopup.close();
        ImageManage.Camera_getPicture(true).then(function (result) {
            uploadimg([result])
        });
    };

    function uploadimg(files) {
        showPopup.showLoading(1, '', true);
        ImageManage.uploadImage(files, 'user', global.user.usrid, 'userfile').then(function (ps) {
            $q.all(ps).then(function (fs) {
                $scope.userinfo.photo = JSON.parse(fs[0].response).file_thumbnail_path;
                showPopup.hideLoading();
            }, function (err) {
                showPopup.hideLoading();
            })
        }, function (err) {
            showPopup.hideLoading();
        })
    }

    $scope.updateInfo = function () {
        showPopup.showLoading(1, '', false);
        Dept.putUsrInfo($scope.userinfo).then(function (res) {
            showPopup.hideLoading();
        }, function (err) {
            showPopup.hideLoading();
        })
    };

    $scope.$on("$destroy", function () {
        beforeEnter = null;
        Member.selecteddepts = [];
        if ($rootScope.commons.modal != null) $rootScope.commons.modal.remove();
    })
})

.controller('EditDeptDegreeCtrl', function ($scope, $state, $rootScope, $ionicHistory, $timeout, global, Member, showPopup, Dept, Tool) {
    $scope.userinfo = {};
    var beforeEnter = $scope.$on("$ionicView.beforeEnter", function () {
        global.fetch_user().then(function (data) {
            if (Member.selecteddepts.length > 0) {
                $scope.userinfo.depname = Member.selecteddepts[0].depname;
                $scope.userinfo.dept_id = Member.selecteddepts[0].id;
            } else $scope.userinfo = Tool.getTempData('userinfo');
        });
    });

    $scope.selectdept = function () {
        Member.routename = 'editdeptdegree';
        Member.titlename = '选择部门';
        Member.resourcemember = [{'id': $scope.userinfo.dept_id, 'depname': $scope.userinfo.depname}];
        Member.selecteddepts = [];
        $state.go('selectsingledept');
    };

    $scope.ok = function () {
        showPopup.showLoading(1, '', false);
        Dept.putUsrInfo($scope.userinfo).then(function (res) {
            showPopup.hideLoading();
            $ionicHistory.goBack();
        }, function (err) {
            showPopup.hideLoading();
            alert(JSON.stringify(err));
        })
    };

    $scope.cancel = function () {
        $ionicHistory.goBack();
    };

    $scope.$on("$destroy", function () {
        beforeEnter = null;
        Member.selecteddepts = [];
    })
})
;
