(function(KDApi, $){
    function TimeCard (model) {
	
        this._setModel(model)
    }

    TimeCard.prototype = {
        _setModel: function(model) {
            this.model = model
        },
        init: function(props){
            this.initFunc(this.model, props);
        },
        update: function(props){
            // 头部日期或人员变化：
            if(props.data && props.data.event === 'rangeChange'){
                this.rangeChange(this.model, props);
            }
            // 底部日期范围变化：
            if(props.data && props.data.event === 'dateChange'){
                this.dateChange(this.model, props);
            }
            // 全屏按钮
            if(props.data && props.data.event === 'fullScreen'){
                this.fullScreen();
            }
            // 全部展开按钮
            if(props.data && props.data.event === 'openAll'){
                this.openAll();
            }
            // 全部折叠按钮
            if(props.data && props.data.event === 'foldAll'){
                this.foldAll();
            }
        },

        initFunc: function(model, props) {
            let $this = this;
            // KDApi.loadFile可以通过路径加载js或css文件，并且在html文件头生成script或者link标签，第一个参数是路径，第二个参数是model，第三个参数是加载完成后执行的回调函数
            KDApi.loadFile(['./css/timeCard.css', './js/timeCard.js', './datePicker/css/datePicker.css', './datePicker/js/manifest.js', './datePicker/js/vendor.js', './datePicker/js/datePicker.js'], model, function() {
                // 通过路径去获取html字符串，第一个参数是路径，第二个参数是model，第三个参数是HTML模板中变量的值
                KDApi.getTemplateStringByFilePath('./html/timeCard.art', model, {
                    date: KDApi.getLangMsg(model, 'date'),
                    Scheduling: KDApi.getLangMsg(model, 'Scheduling'),
                    start: KDApi.getLangMsg(model, 'start'),
                    end: KDApi.getLangMsg(model, 'end'),
                    attendanceItems: KDApi.getLangMsg(model, 'attendanceItems'),
                    duration: KDApi.getLangMsg(model, 'duration'),
                    company: KDApi.getLangMsg(model, 'company'),
                    shiftTime: KDApi.getLangMsg(model, 'shiftTime'),
                    defaultRange: KDApi.getLangMsg(model, 'defaultRange'),
                    custom: KDApi.getLangMsg(model, 'custom'),
                    attendanceTotal: KDApi.getLangMsg(model, 'attendanceTotal'),
                    operationLog: KDApi.getLangMsg(model, 'operationLog'),
                    total: KDApi.getLangMsg(model, 'total'),
                    number: KDApi.getLangMsg(model, 'number'),
                    time: KDApi.getLangMsg(model, 'time'),
                    type: KDApi.getLangMsg(model, 'type'),
                    subType: KDApi.getLangMsg(model, 'subType'),
                    remarks: KDApi.getLangMsg(model, 'remarks'),
                    dateOfDataGeneration: KDApi.getLangMsg(model, 'dateOfDataGeneration'),
                    operator: KDApi.getLangMsg(model, 'operator'),
                    dataSource: KDApi.getLangMsg(model, 'dataSource'),
                    open: KDApi.getLangMsg(model, 'open'),
                    fold: KDApi.getLangMsg(model, 'fold'),
                    modifyCard: KDApi.getLangMsg(model, 'modifyCard'),
                    abnormalConfirm: KDApi.getLangMsg(model, 'abnormalConfirm'),
                    confirm: KDApi.getLangMsg(model, 'confirm'),
                    cancel: KDApi.getLangMsg(model, 'cancel'),
                    confirmAbnormal: KDApi.getLangMsg(model, 'confirmAbnormal'),
                }).then(function(result) {
                    model.dom.innerHTML = result;
                    // 初始化
                    $this.initTimeCard(model, props);

                })
            })
        },

        initTimeCard: function(model, props){
            // 后端插件通过setData传给前端的数据，前端可以通过props.data去获取
            var $this = this;
       
            var para = {
                cardRecord: props.data.cardRecord,
                attendanceTotal: props.data.attendanceTotal,
                operationLog: props.data.operationLog,
                dateValue: props.data.dateValue,
                model: model,
                KDApi: KDApi
            };

            this.objPicker = {
                datePickerId: new Date().getTime(),
                dateValue: para.dateValue,
                model: model,
                pickerOptions: {
                    disabledDate: function  (time) {
                        return time.getTime() > para.dateValue[1].getTime() || time.getTime() < para.dateValue[0].getTime();
                    }
                },
                callback: function (value) {
                    $this.timeCard.userDateRange = value;  // 修改默认日期范围
                }
            };

            this.curVue = new KdWtcDatePicker(this.objPicker);
            this.timeCard = new KdWtcTimeCard(para);
        },

        rangeChange: function(model, props) {
            this.curVue.$vue.$children[0].dateSelect([props.data.dateValue[0], props.data.dateValue[1]]);
            // 通过vue的数据绑定，修改日期选择范围：
            this.objPicker.pickerOptions.disabledDate = function (time) {
                return time.getTime() > props.data.dateValue[1].getTime() || time.getTime() < props.data.dateValue[0].getTime();
            };

            this.timeCard.defaultDateRange = props.data.dateValue;  // 修改默认日期范围
            this.timeCard.refresh_cardRecord(props.data.cardRecord || []);
            this.timeCard.refresh_attendanceTotal(props.data.attendanceTotal || []);
            this.timeCard.refresh_operationLog(props.data.operationLog || []);
        },

        dateChange: function(model, props) {
            this.timeCard.refresh_attendanceTotal(props.data.attendanceTotal || []);
            this.timeCard.refresh_operationLog(props.data.operationLog || []);
        },

        fullScreen: function() {
            this.timeCard.fullScreen();
        },

        openAll: function() {
            this.timeCard.dateSelectAll(1);
        },

        foldAll: function() {
            this.timeCard.dateSelectAll(2);
        },
    };

    KDApi.register('wtctimecard', TimeCard, {isMulLang: true})
})(window.KDApi, jQuery);