"use strict";
appCore.controller("ManPowerPlanningDiscussionsListCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "$uibModal", "$document", function ($state, $scope, $http, $timeout, vServices, $uibModal, $document) {
    $scope.$root.page.title = "Manpower Planning / Resource requirement discussions";
    $scope.manPowerPlanningDiscussions = [];
    $scope.filter = {
        fromDate: moment().subtract(2, "weeks").format("DD-MM-YYYY"),
        toDate: moment().add(1, "day").format("DD-MM-YYYY")
    };

    const convertDataToDisplay = res => {
        $scope.manPowerPlanningDiscussions = null;
        if (res && res.length > 0) {
            $scope.manPowerPlanningDiscussions = res;
            $scope.$root.callDataTable(".manPowerPlanningDiscussionsList", 1000, (t) => {
                try {
                    $scope.dTable = t;
                    //t.columns(6).data().flatten().filter(function (value, index) {return value === 1;});
                    //t.column(6).search(0).draw();
                    //$scope.dTable.column(6).search('^(?![,.0]*$)\\d{1,4}(?:[.,]\\d{1,2})?$', true, false).draw();
                    $scope.viewConditionsHandler();
                }
                catch (e) {
                    console.error(e);
                }
            });
        }
        else {
            toastr.info('', "No data found");
        }
    };
    $scope.repeatDiaryCurrentWeek = (diaryObj) => {
        vServices.callApi("mydiary/repeat_diary_current_week", "PUT", diaryObj).then(function (res) {
            if (res) {
                toastr.success(res, "Success");
                $state.go($state.current, {}, {reload: true});
            }
        });
    }
    /* Model code start*/
    $scope.openManPowerPlanningDiscussionsDeleteModal = (obj) => {
        let modalInstance = $uibModal.open({
            animation: true,
            ariaLabelledBy: "modal-title",
            ariaDescribedBy: "modal-body",
            templateUrl: "ManPowerPlanningDiscussionsDelete.html",
            controller: "ManPowerPlanningDiscussionsDeleteCtrl",
            controllerAs: "$scope",
            size: "md",//sm md lg
            backdrop: "static",
            resolve: {
                data: obj
            }
        });
        modalInstance.result.then((data) => {
            $scope.deleteManPowerPlanningDiscussions(data);
        }, () => {
            toastr.warning("Action has been canceled", "Cancel");
        });
    };
    /* Model code end*/
    $scope.deleteManPowerPlanningDiscussions = (e) => {
        vServices.callApi("employee/manPowerPlanningDiscussion/" + e.manpowerplanningdiscussionsid, "DELETE", {}).then((res) => {
            toastr.success(res, "Success");
            $state.go($state.current, {}, {reload: true});
        });
    };

    $scope.dateChanged = (first = false) => {
        if (!first) {
            if (!$scope.filter.fromDate) toastr.error("Please provide valid FromDate");
            else if (!$scope.filter.toDate) toastr.error("Please provide valid toDate");
            else vServices.callApi("employee/allManPowerPlanningDiscussion", "GET", {}).then(res => res ? convertDataToDisplay(res) : false);
        }
    }
    const init = () => $scope.dateChanged();
    init();

    /* Model code start 9899222772 */
    $scope.openEditMyDiaryEntry = (obj) => {
        let modalInstance = $uibModal.open({
            animation: true,
            ariaLabelledBy: "modal-title",
            ariaDescribedBy: "modal-body",
            templateUrl: "EditMyDiaryEntry.html",
            controller: "EditMyDiaryEntryCtrl",
            controllerAs: "$scope",
            size: "md",//sm md lg
            backdrop: "static",
            resolve: {
                data: () => {
                    return {diary: obj,};
                }
            }
        });
        modalInstance.result.then((data) => {
        }, () => {
            toastr.warning("Action has been canceled", "Cancel");
        });
    };
    /* Model code end*/

}]).controller("EditMyDiaryEntryCtrl", function ($state, $uibModalInstance, data, $scope, vServices) {
    $scope.data = data;
    vServices.callApi("mydiary/all_logtypes", "GET", {}).then((res) => {
        if (res) {
            $scope.logTypes = res;
            // $scope.logTypes.unshift({"mydiarylogtypeid": 0, "lt_name": "Select", "lt_description": ""});
        }
    });

    $scope.change = (d) => {
        if ($scope.data.diary.date && $scope.data.diary.hours && $scope.data.diary.description) {
            vServices.callApi("mydiary/edit", "PUT", $scope.data.diary).then(function (res) {
                if (res) {
                    toastr.success(res, "Success");
                    $uibModalInstance.close(true);
                    $state.go($state.current, {}, {reload: true});
                }
            });
        }
        else toastr.error("Please provide values for all required fields (marked with *)");

    };
    $scope.cancel = () => $uibModalInstance.dismiss("cancel");
}).controller("ManPowerPlanningDiscussionsDeleteCtrl", function ($uibModalInstance, data, $scope) {
    $scope.data = data;

    $scope.delManPowerPlanningDiscussions = (d) => $uibModalInstance.close(d);
    $scope.cancel = () => $uibModalInstance.dismiss("cancel");
});

