starter.factory('CompManage', function ($http, $q, $filter, $ionicPlatform, $cordovaPreferences, cfg, formatFilter, global, Tool) {

    var complist = [];

    return {
        post_Register: function (data) {
            console.log(data)
            var url = formatFilter('{0}register/', cfg.api);
            return Tool.post(url, data);
        },
        post_Joinin: function (data) {
            console.log(data)
            var url = formatFilter('{0}joinin/', cfg.api);
            return Tool.post(url, data);
        },
        getUserAuditstatus: function (auditstatus, compid) {
            var url = formatFilter('{0}joinin/?auditstatus={1}&compid={2}', cfg.api, auditstatus, compid);
            return Tool.get(url);
        },
        auditUser: function (user, status, compid) {
            var url = formatFilter('{0}joinin/{1}/?status={2}&compid={3}', cfg.api, user.user_id, status, compid);
            return Tool.put(url);
        },
        get_Compdata: function () {
            var url = formatFilter('{0}comps/', cfg.api);
            return Tool.get(url, true, 'complist');
        },
        getComplist: function () {
            complist = Tool.getTempData('complist');
            return complist;
        },
        editComp: function (data) {
            var url = formatFilter('{0}comps/{1}/', cfg.api, data.id);
            return Tool.put(url, data);
        },
        post_authcheck: function (imid, compid) {
            var url = formatFilter('{0}authcheck/', cfg.api);
            var d = $filter('format')("imid={0}&compid={1}&password={2}", imid, compid, global.user.password);
            return Tool.post(url, d, false);
        },
        store_user: function () {
            var deferred = $q.defer();
            var user = Tool.cloneObj(global.user);
            user.cfg = {'api': global.api};
            $cordovaPreferences.store('user', ionic.Platform.isAndroid() ? JSON.stringify(user) : user)
            .success(function (value) {
                deferred.resolve(value);
                // alert("store Success: " + value);
            })
            .error(function (error) {
                deferred.reject(err);
                alert("store Error: " + error);
            });
            return deferred.promise;
        },
        leave_comp: function (compid) {
            var url = formatFilter('{0}comps/{1}/?type={2}', cfg.api, compid, "leave");
            var d = $filter('format')("imid={0}&compid={1}", global.user.im_usrid, compid);
            return Tool.put(url, d);
        },
        dismiss_comp: function (compid) {
            var url = formatFilter('{0}comps/{1}/?type={2}', cfg.api, compid, "dismiss");
            var d = $filter('format')("compid={0}", compid);
            return Tool.put(url, d);
        }
    };
})
