"use strict";
appCore.controller("MyDiaryReportHeroViewCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "$uibModal", "$document", function ($state, $scope, $http, $timeout, vServices, $uibModal, $document) {
    $scope.$root.page.title = "My Diary Report";
    $scope.employee = [];
    $scope.myDiary = {default: [], project: [], heroes: [], datatable: []};
    $scope.filter = {
        fromDate: moment().subtract(2, "days").format("DD-MM-YYYY"),
        toDate: moment().format("DD-MM-YYYY"),
        employee: 0,
        pageName: "My Diary Report",
        selType: "Default",
        type: [{val: "View Type"}, {val: "Default"}, {val: "Project",}]
    };

    $scope.$watch("filter.selType", (newValue, oldValue) => {
        $scope.filter.pageName = "My Diary Report of " + newValue + " View";
        $scope.$root.page.title = "My Diary Report of " + newValue + " View " + $scope.filter.fromDate + " - " + $scope.filter.toDate;
    });
    let numOr0 = n => isNaN(n) ? 0 : n;

    //$scope.totalHoursNew = (a, k, d) => d.map(o => o[k]).reduce((a, b) => parseFloat(numOr0(a)) + parseFloat(numOr0(b)));
    $scope.totalHoursNew = (a, k, d) => d && d.length > 0 ? d.map(o => o[k]).reduce((a, b) => parseFloat(numOr0(a)) + parseFloat(numOr0(b))) : null;
    $scope.totalHours = (a, k, d) => d.map(o => o[k]).reduce((a, b) => parseFloat(numOr0(a)) + parseFloat(numOr0(b)));
    $scope.condition = (i, d, data, c) => i === 0 ? true : (i > 0 && parseInt(d[c]) !== parseInt($scope.myDiary[data][i - 1][c]));

    const convertDataForProject = res => {
        if (res) {
            $scope.employee = [];
            $scope.employee.push({id: 0, name: " Select Employee", emp_status: 'active'});
            res = res.filter(emp => emp.emp_status === 'active');
            res.map((o) => $scope.employee.push({id: o.employeeid, name: o.emp_first_name +(o.emp_middle_name ? ' '+o.emp_middle_name+' ' : ' ')+o.emp_last_name+' ('+o.emp_code+')', emp_status: o.emp_status}));
            $scope.employee = $scope.$root.sort($scope.employee,'name');
        }
    };

    const convertDataForMyDiary = res => {
        if (res) {
            let newResult = null, newResult1 = null, resHero = null;
            $scope.myDiary.datatable.map((o) => {
                o.destroy();
            });

            $scope.myDiary = {default: [], project: [], heroes: [], datatable: []};
            let res1 = $scope.$root.sort(res, "emp_first_name");
            $scope.myDiary.default = $scope.$root.sort(res1, "proj_name");

            newResult = res.map(o => o.proj_name + "~" + o.projectid).filter((v, i, s) => s.indexOf(v) === i).sort();

            //create data for project view
            newResult.forEach(o => {
                const proj = o.split("~");
                $scope.myDiary.project.push({name: proj[0], id: proj[1], data: res.filter(j => j.projectid === parseInt(proj[1]))});
            });

            newResult1 = res.map(o => o.emp_first_name + " " + o.emp_last_name + "-" + o.employeeid).filter((v, i, s) => s.indexOf(v) === i).sort();
            resHero = $scope.$root.sort(res, "projectid");
            newResult1.forEach(o => {
                const hero = o.split("-");
                $scope.myDiary.heroes.push({name: hero[0], id: hero[1], data: resHero.filter(j => j.employeeid === parseInt(hero[1]))});
            });
            $scope.$root.callDataTable(".myDiaryDataTableView", $scope.filter.selType === 'Default' ? 500 : 1000, (t) => {
                $scope.myDiary.datatable.push(t);
            }, {orientation: "landscape"});
        }
    };

    $scope.selType = (val) =>{
        console.log(val);
        $scope.$root.callDataTable(".myDiaryDataTableView", $scope.filter.selType === val ? 500 : 1000, (t) => {
            $scope.myDiary.datatable.push(t);
        }, {orientation: "landscape"});
    }

    const getData = (p, f, t) => vServices.callApi("mydiary/all?employee=" + p + "&fromDate=" + f + "&toDate=" + t, "GET", {}).then(res => convertDataForMyDiary(res));
    $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 getData($scope.filter.employee, $scope.filter.fromDate, $scope.filter.toDate);
        }
    }

    const init = () => {
        $scope.dateChanged(true);
        vServices.callApi("employee/limited", "GET", {}).then(res => convertDataForProject(res));
    };

    init();

}]);
