"use strict";
appCore.controller("ProjectListCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "$uibModal", "$document",
    function ($state, $scope, $http, $timeout, vServices, $uibModal, $document) {
        $scope.projectsDatatable = [];
        $scope.myId=$scope.$root.gUser.id;
        $scope.$root.page.title = "List of Projects";
        $scope.projects = [];
        $scope.projStatus = [
            {label: "Select", value: ""},
            {label: "Active", value: "active"},
            {label: "Inactive", value: "inactive"},
            {label: "Hold", value: "hold"},
            {label: "Delivered", value: "delivered"},
            {label: "Upcoming", value: "upcoming"},
        ];
        $scope.filter = {
            account: [],
            selaccount: 'All',
            selstatus: 'active',
            status: [{val: "All"}, {val: "active"}, {val: "inactive",}, {val: "hold",}, {val: "delivered",}, {val: "upcoming",}]
        };
        const convertDataForAccount = res => {
            if (res) {
                $scope.filter.account = [];
                $scope.filter.account.push({accountid: 0, name: "All"});
                res.map((o) => $scope.filter.account.push({accountid: o.accountid, name: o.acc_company_name}));
            }
        };
        const getProjects = (account, status) => vServices.callApi("project/all?accountid=" + account + "&status=" + status, "GET", {}).then(function (res) {
            if (res && res.length > 0) {
                res.map((o) => {
                    o.proj_start_date = o.proj_start_date ? moment(o.proj_start_date) : "";
                    o.proj_expected_end_date = o.proj_expected_end_date ? moment(o.proj_expected_end_date) : "";
                    o.lastAllocationVerified = o.lastAllocationVerified ? moment(o.lastAllocationVerified) : "No Previous Allocation";
                });
                $scope.projectsDatatable.map((o) => {o.destroy();});
                $scope.projects = res;
                $scope.$root.callDataTable("#projectList", 500,(t) => {
                    $scope.projectsDatatable.push(t);
                }, {orientation: "landscape"});
            }
            else{
                toastr.error("Sorry, Did not find any data for your Query !");
            }
        });
        $scope.selectChanged = () => getProjects($scope.filter.selaccount, $scope.filter.selstatus);
        const init = function () {
            $scope.selectChanged();
            vServices.callApi("account/all", "GET", {}).then(res => convertDataForAccount(res));
        };
        init();
        $scope.verifyAllocation=(proj_id)=>{
            vServices.callApi('project/verifyAllocationByManager/'+proj_id, "PUT").then(function (res) {
                if (res) {
                    toastr.success(res, "Success");
                    $state.go($state.current, {}, {reload: true});
                }
            });
        }

        $scope.changeStatus = (obj) => {
            vServices.callApi("project/change_status", "PUT", {proj_id: obj.projectid, proj_status: obj.status,proj_status_change_comment:obj.change_comment}).then(function (res) {
                if (res) {
                    toastr.success(res, "Success");
                    $state.go($state.current, {}, {reload: true});
                }
            });
        };
        /* Model code start*/
        $scope.openProjectStatusModel = (obj) => {
            let modalInstance = $uibModal.open({
                animation: true,
                ariaLabelledBy: "modal-title",
                ariaDescribedBy: "modal-body",
                templateUrl: "ProjectStatus.html",
                controller: "ProjectStatusCtrl",
                controllerAs: "$scope",
                size: "md",//sm md lg
                backdrop: "static",
                resolve: {
                    data: () => {
                        return {
                            project: obj,
                            projStatus: $scope.projStatus
                        };
                    }
                }
            });

            modalInstance.result.then((data) => {
                $scope.changeStatus(data);
            }, () => {
                toastr.warning("Action has been canceled", "Cancel");
            });
        };
        /* Model code end*/

    }
]).controller("ProjectStatusCtrl", function ($uibModalInstance, data, $scope) {
    $scope.data = data;
    $scope.change = (d) => $uibModalInstance.close(d.project);
    $scope.cancel = () => $uibModalInstance.dismiss("cancel");
});
