starter.factory('workatd', function (cfg, global, formatFilter, Tool) {
    var _weeknames = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    var _today = new Date();
    var _current = {};
    var _urlbase = formatFilter('{0}workatd/settings/', cfg.api);
    return {
        titleArr: ['正常考勤', '上班迟到', '下班早退', '上班未签到', '下班未签退'],
        weeknames: _weeknames,
        todayweek: _weeknames[_today.getDay()],
        today: _today.getFullYear() + "年" + (_today.getMonth() + 1).toString() + "月" + _today.getDate() + "日",
        current: function (op) {
            op = op || 1;
            return op > 0 ? Tool.getTempData('workatddetail') : _current;
        },
        addnew: function () {
            _current = {
                "ischeckmonday": true,
                "ischecktuesday": true,
                "ischeckwednesday": true,
                "ischeckthursday": true,
                "ischeckfriday": true,
                "ischecksaturday": false,
                "ischecksunday": false,
                "am_ontime": null,
                "am_offtime": null,
                "pm_ontime": null,
                "pm_offtime": null,
                "isdoublecheck": 0,
                "workposition": null,
                "workaddress": null,
                "offset": 500,
                "ap_names": null,
                "isallowauto": 1,
                "isallcheck": 0,
                "tz": new Date().getTimezoneOffset(),
                "workatdreportusr": [],
                "workatddept": [],
                "workatdwithoutusr": []
            }
            return _current;
        },
        getSettings: function () {
            var url = formatFilter('{0}workatd/settings/', cfg.api);
            return Tool.get(url);
        },
        getDetail: function (id) {
            var url = formatFilter('{0}workatd/setting/{1}/', cfg.api, id);
            return Tool.get(url, true, 'workatddetail')
        },
        save: function (id, data) {
            var reportusr = data.workatdreportusr;
            var withoutusr = data.workatdwithoutusr;
            var dept = data.workatddept;
            data.workatdreportusr = [];
            data.workatdwithoutusr = [];
            data.workatddept = [];
            if (!data.isdoublecheck) {
                data.am_offtime = null;
                data.pm_ontime = null;
            }
            data = _.omit(data, 'am_ontime_view', 'am_offtime_view', 'pm_ontime_view', 'pm_offtime_view', 'ischeckpmoff', 'ischeckpmon', 'ischeckamon', 'ischeckamoff', "isdoublecheck");
            _.each(reportusr, function (item) {
                data.workatdreportusr.push(item.user_id);
            });
            _.each(withoutusr, function (item) {
                data.workatdwithoutusr.push(item.user_id);
            });
            _.each(dept, function (item) {
                if (item.dept__id) {
                    item.id = item.dept__id;
                }
                data.workatddept.push(item.id);
            });
            if (id == '-1' || id == '0') {
                //新增
                var url = formatFilter('{0}workatd/setting/', cfg.api);
                return Tool.post(url, data);
            } else {
                //修改
                var url = formatFilter('{0}workatd/setting/{1}/', cfg.api, id);
                return Tool.put(url, data);
            }
        },
        delete: function (id) {
            //删除
            var url = formatFilter('{0}workatd/setting/{1}/', cfg.api, id);
            return Tool.delete(url);
        },
        getDateList: function (workatdsetid, type) {
            var url = formatFilter('{0}workatd/dailyreportdatelist/?workatdsetid={1}&type={2}', cfg.api, workatdsetid, type);
            if (workatdsetid == 0)
                url = formatFilter('{0}workatd/dailyreportdatelist/?type={1}', cfg.api, type);
            return Tool.get(url);
        },
        getReport: function (workatdsetid, date, deptid) {
            var url = formatFilter('{0}workatd/dailyreport/?workatdsetid={1}&check_dd={2}&deptid={3}', cfg.api, workatdsetid, date, deptid);
            return Tool.get(url);
        },
        getCountRoot: function () {
            var url = formatFilter('{0}workatdgetcountroot/?usrid={1}', cfg.api, global.user.usrid);
            return Tool.get(url);
        },
        getCountList: function () {
            var url = formatFilter('{0}workatd/settings/', cfg.api);
            return Tool.get(url);
        },
        getReportDetail: function (id, date, atdtype, type, deptid) {
            var url = formatFilter('{0}workatd/dailyreportdetail/?workatdsetid={1}&date={2}&atdtype={3}&type={4}&deptid={5}', cfg.api, id, date, atdtype, type, deptid);
            return Tool.get(url)
        },
        getWorkatdDeptCount: function (workatdsetid) {
            var url = formatFilter('{0}workatd/deptcount/?workatdsetid={1}', cfg.api, workatdsetid);
            return Tool.get(url)
        },
        getCheckinDetail: function (check_dd) {
            var url = formatFilter('{0}workatd/checkindetail/?check_dd={1}', cfg.api, check_dd);
            return Tool.get(url);
        },
        getPersonMonthCount: function (year, month) {
            var url = formatFilter('{0}workatd/personmonthcount/?year={1}&month={2}', cfg.api, year, month);
            return Tool.get(url);
        },
        getPersonMonthCountDetail: function (year, month, type) {
            var url = formatFilter('{0}workatd/personmonthcountdetail/?year={1}&month={2}&type={3}', cfg.api, year, month, type);
            return Tool.get(url);
        },
        getNextPage: function (url) {
            return Tool.get(url)
        },
        getMonthCount: function (year, month) {
            var url = formatFilter('{0}workatd/monthcount/?year={1}&month={2}', cfg.api, year, month);
            return Tool.get(url);
        },
        getMonthCountDetail: function (year, month, type) {
            var url = formatFilter('{0}workatd/monthcountdetail/?year={1}&month={2}&type={3}', cfg.api, year, month, type);
            return Tool.get(url);
        }
    }
})

.factory('workatdcheck', function ($http, $q, cfg, global, formatFilter, Tool) {
    var _workatdcheck = {};
    _workatdcheck.getCheckInfo = function () {
        var url = formatFilter('{0}workatd/checkin/?usrid={1}', cfg.api, global.user.usrid);
        return Tool.get(url);
    };
    _workatdcheck.getDeptSetId = function () {
        var url = formatFilter('{0}depts/{1}/', cfg.api, global.user.deptno);
        return Tool.get(url);
    };
    _workatdcheck.checkin = function (data) {
        var url = formatFilter('{0}workatd/checkin/', cfg.api);
        var data = {
            'checktype': data.checktype,
            'workatdsetid': data.workatdsetid,
            'reason': !data.isoutside && data.content && data.isoutoftime ? data.content : null,
            'outside': data.isoutside && data.content && data.isoutoftime ? data.content : null,
            'isautocheck': false
        }
        return Tool.post(url, data);
    };
    _workatdcheck.getDeptWorkatdSet = function () {
        var url = formatFilter('{0}deptworkatdset/?usrid={1}&depid={2}', cfg.api, global.user.usrid, global.user.deptno);
        return Tool.get(url);
    };
    return _workatdcheck;
});
