"use strict";
appCore.controller("MasterCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", function ($state, $scope, $http, $timeout, vServices) {
    const accepted = ["skill", "designation", 'department', 'log type','certificatedomains','revenuecenter'];

    $scope.page = {allow: false, name: "Skill", type: "Add",group:'Salesforce', icon: "fa-users", color: "purple"};
    $scope.params = $state.params;
    $scope.$root.page.title = "Manage " + $state.params.type;
    $scope.dataList = [];
    $scope.skillsgroups = [];
    $scope.reset = () => {
        $scope.data = {name: "", desc: ""};
        $scope.page.type = "Add";
    };
    $scope.edit = (dl) => {
        $scope.data = JSON.parse(JSON.stringify(dl));
        $scope.page.type = "Edit";
        $(document).find("#name").focus();
        window.screen.width < 768 ? window.scrollTo(0, 0) : null;
        setTimeout(() => {
            $(document).find("#formCard").addClass("highlightme");
            setTimeout(() => {
                $(document).find("#formCard").removeClass("highlightme");
            }, 800);
        }, (window.screen.width < 768 ? 500 : 0));
    };
    $scope.DeptCounter = [];

    $scope.delete = (d) => {
        d["datatype"] = $state.params.type.replace(' ', '');
        vServices.callApi("manage/delete", "DELETE", d).then(function (res) {
            if (res) toastr.success(res, "Success!", {timeOut: 5000});
            $state.go($state.current, {}, {reload: true});
        });
    };
    $scope.add = (d) => {
        d["datatype"] = $state.params.type.replace(' ', '');
        vServices.callApi("manage/" + (d.id ? "edit" : "add"), d.id ? "PUT" : "POST", d).then(function (res) {
            if (res) toastr.success(res, "Success!", {timeOut: 5000});
            $state.go($state.current, {}, {reload: true});
        });
    };
    const init = function () {
            $scope.dataList = [];
            if ($state.params.type === "skill") {
                vServices.callApi("employee/allSkills", "GET", {}).then(function (res) {
                    vServices.callApi("skill/allskillsgroups", "GET", {}).then(function (res) {
                        if (res && res.length > 0)
                            $scope.skillsgroups = res;
                    });
                    if (res && res.length > 0) res.forEach((o) => {
                        $scope.dataList.push({id: o.skillid, name: o.skl_name, desc: o.skl_description, skillsgroup: o.skillgroupid, skillgroupname: o.sg_name});
                    });
                    $scope.$root.callDataTable("#masterList", 500);
                });
            }
            else if ($state.params.type === "designation") {
                vServices.callApi("designation/all", "GET", {}).then(function (res) {
                    if (res && res.length > 0) {
                        res.forEach((o) => {
                            $scope.dataList.push({id: o.id, name: o.desg_name, desc: o.desg_description});
                        });
                        $scope.$root.callDataTable("#masterList", 500);
                    }
                });
            }
            else if ($state.params.type === "department") {
                vServices.callApi("department/all", "GET", {}).then(function (res) {
                    if (res && res.length > 0) {
                        res.forEach((o) => {
                            $scope.dataList.push({id: o.id, name: o.dept_name, desc: o.dept_description});
                        });
                        $scope.$root.callDataTable("#masterList", 500);
                    }
                });
                vServices.callApi("department/getDeptCounter", "GET", {}).then(function (res) {
                    $scope.deptCounter = res;
                    $scope.$root.callDataTable("#masterList2", 500);
                });
            }
            else if ($state.params.type === "log type") {
                vServices.callApi("/mydiary/all_logtypes", "GET", {}).then(function (res) {
                    if (res && res.length > 0) {
                        res.forEach((o) => {
                            $scope.dataList.push({id: o.mydiarylogtypeid, name: o.lt_name, desc: o.lt_description});
                        });
                        $scope.$root.callDataTable("#masterList", 500);
                    }
                });
            }
            else if ($state.params.type === "certificatedomains") {
                vServices.callApi("/employee/allCertificateDomains", "GET", {}).then(function (res) {
                    if (res && res.length > 0) {
                        res.forEach((o) => {
                            $scope.dataList.push({id: o.certificatedomainid, name: o.domain_name, desc: ''});
                        });
                        $scope.$root.callDataTable("#masterList", 500);
                    }
                });
            }
            else if ($state.params.type === "revenuecenter") {
                vServices.callApi("/account/all_revenue_centers", "GET", {}).then(function (res) {
                    if (res && res.length > 0) {
                        res.forEach((o) => {
                            $scope.dataList.push({id: o.revenuecenterid, name: o.rc_name, desc: ''});
                        });
                        $scope.$root.callDataTable("#masterList", 500);
                    }
                });
            }

        }
    ;
    if (accepted.includes($state.params.type)) {
        $scope.page = {allow: true, name: $state.params.type, type: "Add", icon: "fa-users", color: "purple"};
        $scope.reset();
        init();
    }

}
]);



