"use strict";

appCore.controller("MyCertificateAddCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", function ($state, $scope, $http, $timeout, vServices, filterFilter) {
    $scope.params = $state.params;
    $scope.$root.page.title = "Edit My Certificate";
    $scope.createButton = {heading: "Add", text: "Save", icon: "fa-save", today: new Date()};
    $scope.btn = {text: "Edit My Certificate"};
    // $scope.myCertificateImage = null;
    $scope.certificateDomains = [];
    $scope.ec = [];
    $scope.isEmployeeCertified = '';
    $scope.tenYearFromToday = moment().add(10,'years').format('DD-MM-YYYY');
    $scope.today = moment().format('DD-MM-YYYY');

    $scope.gstatus = [{label: "Active", value: "active"}, {label: "Inactive", value: "inactive"}];

    $scope.selectSkills = [];
    $scope.Save = () => {
        let data = $scope.ec;
        data.cert_date_utc = $scope.$root.convertDate2DateTimeUTC(data.certification_date);
        data.cert_expiry_utc = $scope.$root.convertDate2DateTimeUTC(data.certification_expiry);
        let attachment = {'attachment': data.myCertificateImage};
        delete data.myCertificateImage;
        if (data.certification_number && data.certification_number !== '' && data.certification_name && data.certification_name !== '' && data.certification_date && data.certification_date !== '' && attachment && attachment !== null) {
            vServices.callApi("employee/updateMyCertificate", "POST", data, attachment).then((res) => {
                if (res) {
                    $state.go("myCertificatesList", {}, {reload: true});
                }
            });
        }
        else toastr.error('Unable to submit partially filled data, kindly validate.');
    }, (e) => {
        console.log(e);
    };
    const init = () => {
        vServices.callApi("employee/isEmployeeCertified?employeeId="+ $scope.$root.gUser.id, "GET", {}).then(res => {
            if (res) {
                $scope.isEmployeeCertified = res[0].is_certified;
            }
        });
        vServices.callApi("/employee/allCertificateDomains", "GET", {}).then((res) => {
            if (res) {
                $scope.certificateDomains = res;
                // $scope.logTypes.unshift({mydiarylogtypeid: "0", lt_name: "Select", lt_description: "", lt_status: "active"});
            }
        });
    };

    $scope.yesNoButton = (trueOrfalse) => {
        vServices.callApi("employee/certifiedAcknowledgement?employeeId="+ $scope.$root.gUser.id + "&answer="+trueOrfalse, "GET", {}).then((res) => {
            if (res) {
                window.location.reload();
            }
        });
    };

    if ($scope.$root.gUser.id > 1)
        init();
}]);
