var { BaseModel, BaseFormModel, FormBuilder } = require('../master/baseModels.js');
import { ApiService } from "../../util/apiService.js";
require('knockout.validation');
import { TableTemplateInfo } from '../master/model.js';
export class GroupPeriod extends BaseModel {
    constructor(model) {
        super(model);
    }
}

export class GroupPeriodForm extends BaseFormModel {
    constructor(viewModel, editMode, data) {
        super(viewModel, editMode, [], ["", "reviewFinancialCaption"]);
        this.width = data.tableTemplates.length > 6 ? '80%' : '50%';
        this.periodTemplates = ko.observableArray();
        this.magazineFileCode = ko.observable();
        this.magazineTitle = ko.observable();
        this.description = ko.observable();
        this.showDescription = ko.observable(false);
        if (editMode) {
            this.magazineFileCode = ko.observable(data.magazineFileCode);
            this.magazineTitle = ko.observable(data.magazineTitle);
            for (var p in data.tableTemplates) {
                if (data.tableTemplates.hasOwnProperty(p)) {
                    this.periodTemplates.push(
                        new TableTemplateInfo(data.tableTemplates[p]));
                }
            }
        }

        this.condFirst = ko.pureComputed(function () {
            return !this.showDescription() && this.submitState() !== 2 && this.submitState() !== 3;
        }, this);
        this.condSecond = ko.pureComputed(function () {
            return !this.showDescription() && this.submitState() !== 3;
        }, this);
        this.condThird = ko.pureComputed(function () {
            return this.showDescription();
        }, this);
        this.setValidationObject();
        this.createForm();

    }

    setValidationObject() {
        var self = this;
        self.validationObject = ko.validatedObservable({
            description: self.description.extend({
                required: {
                    onlyIf: function () {
                        return self.showDescription();
                    }
                }
            })
        });
    }

    toggleShowContent(item) {
        item.showContent(!item.showContent());
        this.getTemplateContent(item);
    }

    getTemplateContent(item) {
        if (_h.isEmpty(item.content())) {
            var model = {
                templateId: item.id(),
                periodId: this.id()
            };
            ApiService.request(
                {
                    url: "/api/groupperiod/gettabletemplatecontent",
                    data: model,
                    success: function (data) {
                        item.content(data.data);
                    },
                    error: function (data) {
                        var text = _h.getApiMessage("LoadGroupPeriodDetailError");
                        _h.failToast(text);
                    }
                });
        }
    }


    downloadTemplateExcel(item) {
        var model = {
            controllerName: 37,
            inputData: {
                periodId: this.id(),
                id: item.id()
            }
        };
        _h.kocnx().createExcel(model);
    }

    downloadTemplatePdf(item) {
        var model = {
            controllerName: 37,
            inputData: {
                periodId: this.id(),
                id: item.id()
            }
        };
        _h.kocnx().createPdf(model);
    }

    downloadPdf() {
        _h.downloadFile(this.magazineFileCode());
    }

    createForm() {
        this.bodyClass = "panel-body inside-tab";
        const controls = [
            this.createLine({ t: 'tm', templateName: 'groupperiod-form-t' }, undefined, true, 30, 30),
        ];
        this.formControls(controls);
        this.formButtons({
            name: 'footer-form-t', data: {
                buttons: [
                    { name: "custom", resText: 'confirmedBtn', vis: this.condFirst, click: this.parentVM.confirmedBtn },
                    { name: "custom", resText: 'rejectedBtn', vis: this.condSecond, click: this.parentVM.rejectedBtn },
                    {
                        name: "custom",
                        resText: 'afterDescSubmittedBtn',
                        vis: this.condThird,
                        click: this.parentVM.afterDescSubmittedBtn
                    },
                    { name: 'cancel', click: _h.kocnx().closeForm }]
            }
        });
    }
}

