starter.factory('Notice', function ($http, $q, cfg, global, formatFilter, Tool) {
    var NoticeDetailsData = null;
    var curDate = new Date();
    var tmday = new Date((curDate / 1000 + 86400) * 1000);
    var savedata = {
        "user_id": global.user.usrid,
        "title": "",
        "content": "",
        "address": null,
        "create_dd": null,
        "readtimes": 0,
        "commenttimes": 0,
        "back_title": null,
        "back_enddd": tmday,
        "vote_title": null,
        "vote_selecttype": 1,
        "vote_selectdata": "",
        "vote_enddd": tmday,
        "allid": 0,
        "groupid": null,
        "readusrs": [],
        "noticefiles": [],
        "setbtnstyle": {
            vote: false,
            back: false
        },
        "selectitems": [],
        "deleteimages": {
            "oldimages": [],
            "newimages": []
        },
        "disablededit": {
            showadditem: true,
            showeditback: true
        }
    };

    var countDetails = {
        usrCountList: [],
        selectItem: null
    };

    function pick() {

    }

    function setdata(data) {
        if (Tool.trim(data.vote_title).length == 0) {
            data.vote_title = null;
            data.vote_enddd = null;
        }
        if (Tool.trim(data.back_title).length == 0) {
            data.back_title = null;
            data.back_enddd = null;
        }
    }

    return {
        getCreateUsrs: function () {
            var url = formatFilter('{0}notice/createusrs/', cfg.api);
            return Tool.get(url);
        },
        post_NoticeCreateUsrs: function (add_usrs) {
            var url = formatFilter('{0}notice/createusrs/', cfg.api);
            return Tool.post(url, add_usrs);
        },
        delete_NoticeCreateUsrs: function (delete_usrs) {
            var data = [{
                "op": "del",
                "values": delete_usrs
            }];
            var url = formatFilter('{0}notice/createusrs/', cfg.api);
            return Tool.patch(url, data);
        },
        post_NoticeMf: function (data) {
            console.log(Tool.cloneObj(data));
            console.log(data);
            data = _.pick(data,
                    "user_id",
                    "title",
                    "content",
                    "address",
                    "create_dd",
                    "commenttimes",
                    "back_title",
                    "back_enddd",
                    "vote_title",
                    "vote_selecttype",
                    "vote_selectdata",
                    "vote_enddd",
                    "allid",
                    "groupid",
                    "update_dd",
                    "readusrs");
            var url = formatFilter('{0}notice/', cfg.api);
            setdata(data);
            return Tool.post(url, data);
        },
        post_NoticeReadList: function (data) {
            var url = formatFilter('{0}notice/readlist/', cfg.api);
            return Tool.post(url, data);
        },
        getReceive: function () {
            var url = formatFilter('{0}notice/list/?type=receive', cfg.api);
            return Tool.get(url);
        },
        getRelease: function () {
            var url = formatFilter('{0}notice/list/', cfg.api);
            return Tool.get(url);
        },
        getNext: function(url) {
            return Tool.get(url);
        },
        getDetails: function (id) {
            var url = formatFilter('{0}notice/{1}/', cfg.api, id);
            var deferred = $q.defer();
            $http.get(url).then(function (res) {
                        deferred.resolve(res.data);
                        NoticeDetailsData = Tool.cloneObj(res.data);
                        NoticeDetailsData.setbtnstyle = {
                            vote: false,
                            back: false
                        };
                        NoticeDetailsData.selectitems = [];
                        if (NoticeDetailsData.vote_title !== null) {
                            NoticeDetailsData.setbtnstyle.vote = true;
                            var selectitems = NoticeDetailsData.vote_selectdata.split(';');
                            _.each(selectitems, function (item) {
                                NoticeDetailsData.selectitems.push({
                                    "text": item
                                });
                            });
                        }
                        if (NoticeDetailsData.back_title != null) {
                            NoticeDetailsData.setbtnstyle.back = true;
                        }

                        NoticeDetailsData.showvoteDelete = false;
                        NoticeDetailsData.showbackDelete = false;
                        if (NoticeDetailsData.vote_title != null) {
                            NoticeDetailsData.showvoteDelete = true;
                        } else {
                            NoticeDetailsData.vote_enddd = tmday;
                        }
                        if (NoticeDetailsData.back_title != null) {
                            NoticeDetailsData.showbackDelete = true;
                        } else {
                            NoticeDetailsData.back_enddd = tmday;
                        }
                        NoticeDetailsData.deleteimages = {
                            "oldimages": [],
                            "newimages": []
                        };
                        NoticeDetailsData.disablededit = {
                            showadditem: true,
                            showeditback: true
                        };
                        console.log(NoticeDetailsData)
                    },
                    function (err) {
                        deferred.reject(err);
                    });
            return deferred.promise;
        },
        getLoalDetails: function (op) {
            return NoticeDetailsData;
        },
        getsavedata: savedata,
        getstructdata: function () {
            var structdata = {
                "user_id": global.user.usrid,
                "title": "",
                "content": "",
                "address": null,
                "create_dd": null,
                "readtimes": 0,
                "commenttimes": 0,
                "back_title": null,
                "back_enddd": tmday,
                "vote_title": null,
                "vote_selecttype": 1,
                "vote_selectdata": "",
                "vote_enddd": tmday,
                "allid": 0,
                "groupid": null,
                "readusrs": [],
                "noticefiles": [],
                "setbtnstyle": {
                    vote: false,
                    back: false
                },
                "selectitems": [],
                "deleteimages": {
                    "oldimages": [],
                    "newimages": []
                },
                "disablededit": {
                    showadditem: true,
                    showeditback: true
                }
            };
            return Tool.cloneObj(structdata);
        },
        put_NoticeMf: function (data) {
            data = _.pick(data, "id",
                    "title",
                    "content",
                    "address",
                    "back_enddd",
                    "back_title",
                    "vote_title",
                    "vote_selecttype",
                    "vote_selectdata",
                    "vote_enddd",
                    "allid",
                    "groupid",
                    "update_dd",
                    "readusrs",
                    "del_files");
            var url = formatFilter('{0}notice/{1}/', cfg.api, data.id);
            setdata(data);
            return Tool.put(url, data);
        },
        delete_NoticeMf: function (id) {
            var url = formatFilter('{0}notice/{1}/', cfg.api, id);
            return Tool.delete(url);
        },
        getUsrCount: function (id, type, index) {
            var url = formatFilter('{0}notice/stat/detail/?notice_id={1}&type={2}', cfg.api, id, type);
            if (type == 7 && index != undefined) {
                url = formatFilter('{0}notice/stat/detail/?notice_id={1}&type={2}&index={3}', cfg.api, id, type, index);
            }
            return Tool.get(url, true, 'noticecount');
        },
        getCountDetails: function () {
            countDetails.usrCountList = Tool.getTempData('noticecount');
            return countDetails;
        },
        getCount: function (id) {
            var url = formatFilter('{0}notice/stat/?notice_id={1}', cfg.api, id);
            return Tool.get(url);
        },
        getRecord: function (id) {
            var url = formatFilter('{0}notice/record/?noticeid={1}', cfg.api, id);
            return Tool.get(url);
        },
        postRecord: function (data) {
            var url = formatFilter('{0}notice/record/', cfg.api);
            return Tool.post(url, data);
        },
        getCreateUsrsState: function () {
            var url = formatFilter('{0}notice/createusrs/?type={1}', cfg.api, 'state');
            return Tool.get(url);
        }
    }
});
