"use strict";
appCore.controller("ProjectDiaryListCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "$uibModal", "$document", function ($state, $scope, $http, $timeout, vServices, $uibModal, $document) {
    $scope.$root.page.title = "My Diary";
    $scope.projectdiary = [];
    $scope.filter = {
        fromDate: moment().subtract(1, "weeks").format("DD-MM-YYYY"),
        toDate: moment().format("DD-MM-YYYY")
    };

    const convertDataToDisplay = res => {
        $scope.projectdiary = null;
        if (res) {
            $scope.projectdiary = [];
            let newResult = res.map(o => o.proj_name + "-" + o.proj_id).filter((v, i, s) => s.indexOf(v) === i).sort();

            for (let i = 0; i < newResult.length; i++) {
                const o = newResult[i];
                const proj = o.split("-");
                $scope.projectdiary.push({proj_name: proj[0], proj_id: proj[1], data: res.filter(j => j.proj_id === proj[1])});
                if (i === newResult.length - 1) {
                    $scope.$root.callDataTable("#projectView", 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);
                        }
                    });
                }
            }
        }
    };
    $scope.dateChanged = () => vServices.callApi("mydiary/me_project_diary?fromDate=" + $scope.filter.fromDate + "&toDate=" + $scope.filter.toDate, "GET", {}).then(res => res ? convertDataToDisplay(res) : false);
    const init = () => $scope.dateChanged();
    init();

    /* Model code start 9899222772 */
    $scope.openEditProjectdiaryEntry = (obj) => {
        let modalInstance = $uibModal.open({
            animation: true,
            ariaLabelledBy: "modal-title",
            ariaDescribedBy: "modal-body",
            templateUrl: "EditProjectdiaryEntry.html",
            controller: "EditProjectdiaryEntryCtrl",
            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("EditProjectdiaryEntryCtrl", function ($state, $uibModalInstance, data, $scope, vServices) {
    $scope.data = data;
    $scope.pd_project_status_options = [{label: "On Track", value: "On Track"}, {label: "Falling behind", value: "Falling behind"}];
    $scope.boolean_options = [{label: "Yes", value: true}, {label: "No", value: false}];
    $scope.change = (d) => {
        vServices.callApi("mydiary/edit_project_diary", "PUT", $scope.data.diary).then(function (res) {
            if (res) {
                toastr.success(res, "Success");
                $uibModalInstance.close(true);
                $state.go($state.current, {}, {reload: true});
            }
        });

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

