"use strict";

appCore.controller("ProjectDiaryAddCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", function ($state, $scope, $http, $timeout, vServices, filterFilter) {
    $scope.$root.page.title = "Project Diary Entry";
    $scope.createButton = {heading: "Add", text: "Save", icon: "fa-save", today: new Date()};

    let dummyEntry = {
        proj_id: "",
        emp_id: $scope.$root.gUser.id,
        pd_date: null,
        pd_project_status: "",
        pd_status: "active",
        pd_is_escalation: "",
        pd_is_challange: "",
        pd_is_extra_hours: "",
        pd_issue_brief: "",
        pd_handling_plan: "",
    };
    const getDummyEntry = (id) => {
        let d = JSON.parse(JSON.stringify(dummyEntry));
        d.proj_id = id;
        d.date = null;
        return d;
    };
    let dupHandler = [];
    $scope.diaries = [];
    $scope.logTypes = [];
    $scope.addMore = (i, diary) => $scope.diaries[i].data.push(getDummyEntry(diary.projectid));
    $scope.removeSelf = (pIdx, rowIdx) => $scope.diaries[pIdx].data.splice(rowIdx, 1);
    $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}];

    const emptyHandler = (dataResult) => {
        let finalResult = [];
        return new Promise((resolve, reject) => {
            if (dataResult.length === 0) reject(false)
            for (let j = 0; j < dataResult.length; j++) {
                var data = dataResult[j];
                for (let i = 0; i < data.length; i++) {
                    let v = data[i];
                    if (v.pd_date && v.pd_project_status) finalResult.push(v);
                    else toastr.error("Please provide values for all required fields (marked with *)");
                    if (i === data.length - 1 && j === dataResult.length - 1) {
                        finalResult.length > 0 ? resolve(finalResult) : reject(false);
                    }
                }
            }
        });
    };

    $scope.Save = () => {
        let dataResult = $scope.diaries.map((o) => o.data);
        emptyHandler(dataResult).then((data) => {
            vServices.callApi("mydiary/add_projectdiary", "POST", {myval: JSON.stringify(data)}).then((res) => {
                if (res) {
                    toastr.success(res, "Message");
                }
                $state.go("projectDiaryList", {}, {reload: true});
            });
        }, (e) => {
            console.error(e);
        });
    };
    const createDiary = (res, cb) => {
        if (res) {
            res.map(o => {
                if (dupHandler.indexOf(o.projectid) === -1 && o.proj_status === 'active') {
                    dupHandler.push(o.projectid);
                    $scope.diaries.push({projectid: o.projectid, proj_name: o.proj_name, data: [getDummyEntry(o.projectid)]});
                }
            });
            if (cb) cb();
        }
        else if (cb) {
            cb();
        }
    };
    const init = function () {
        $scope.diaries = [];
        let dupHandler = [];
        vServices.callApi("allocation/listbyempid/" + $scope.$root.gUser.id, "GET", {}).then((res1) => createDiary(res1, () => {
            vServices.callApi("project/manager/" + $scope.$root.gUser.id, "GET", {}).then((res2) => createDiary(res2));
        }));
    };
    init();
}
]);
