starter.factory('Daily', function ($resource, cfg, formatFilter, Tool, showPopup) {
    var daily = {};
    daily.templateData = {};
    daily.templateId = -1;
    daily.fieldItems = []; //存放模板字段集合
    daily.templateDepts = [];
    daily.currentTemplate = {};
    daily.daily = {};
    daily.permissionUsers = [];
    daily.permissionUser = {};
    daily.previewData = {};
    daily.toDayViewUsers = [];
    
    daily.templatefield = $resource(formatFilter('{0}daily/templatefield/', cfg.api));
    daily.templateitem = $resource(formatFilter('{0}daily/templateitem/', cfg.api));
    daily.templatedept = $resource(formatFilter('{0}daily/templatedept/', cfg.api));
    daily.template = $resource(formatFilter('{0}daily/template/:id/', cfg.api));
    daily.dailytf = $resource(formatFilter('{0}daily/dailytf/', cfg.api), {}, {'save': {method: 'POST', isArray: true}, 'update': {method: 'PUT', isArray: true}});
    daily.dailymf = $resource(formatFilter('{0}daily/dailymf/:id/', cfg.api));
    daily.dailypermission = $resource(formatFilter('{0}daily/dailypermission/', cfg.api), {}, {'save': {method: 'POST', isArray: true}, 'patch': {method: 'PATCH', isArray: true}});
    daily.dailyread = $resource(formatFilter('{0}daily/dailyread/:id/', cfg.api));
    daily.dailyfile = $resource(formatFilter('{0}daily/dailyfile/:id/', cfg.api));
    daily.dailysummary = $resource(formatFilter('{0}daily/dailysummary/', cfg.api));
    daily.dailyview = $resource(formatFilter('{0}daily/dailyview/', cfg.api));
    daily.mouthsummary = $resource(formatFilter('{0}daily/monthsummary/', cfg.api));
    daily.dailydeptview = $resource(formatFilter('{0}daily/dailydeptview/', cfg.api));
    daily.todaysummary = $resource(formatFilter('{0}daily/todaysummary/', cfg.api));
    daily.readview = $resource(formatFilter('{0}daily/readview/', cfg.api));
    
    daily.get_name = function () {
        return daily.templateDepts.map(function (dept) {
            return dept.depname;
        }).join(',');
    }

    daily.get_fieldItems = function () {
        if (daily.templateId == -1) {
            return _.map(daily.fieldItems, function (fi) {
                return _.pick(fi, 'name', 'daily_template_field_id', 'required', 'extra');
            })
        } else {
            return _.map(daily.fieldItems, function (fi) {
                return _.pick(fi, 'id', 'name', 'daily_template_field_id', 'required', 'extra');
            })
        }
    }

    daily.get_description = function () {
        return _.map(daily.templateData.items, function (fi) {
            return fi.name;
        }).join(',')
    }
    
    daily.checkDaily = function (dailys) {
        var flag = true;
        var note = null;
        
        daily = _.find(dailys, function(daily){
            return (!daily.text || daily.text == '') && daily.required
        });
        
        if (daily) {
            if (parseInt(daily.t__type) in [1, 2, 4]) {
            	note = '请填写';
            } else if (parseInt(daily.t__type) in [3, 5]) {
                note = '请选择';
            }
            note += daily.name;
            flag = false;
            showPopup.PopupWindow(0, note, false);
        }
        return flag;
    }
    
    daily.getDate = function (timer) {
        timer = timer || 7;
        var newDate = [];
        timer = timer - 2;
        for (var i = -timer; i < 2; i++) {
            var id = i + timer;
            var day = new Date();
            day.setDate(day.getDate() + i);
            var week_day = Tool.getTranslateByKey("weekdays", day.getDay());
            newDate.push({
                'id':id,
                'day':day,
                'week':week_day
            });
        }
        return newDate.reverse();
    }
    
    return daily;
});