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

  $scope.$watch("filter.selType", (newValue, oldValue) => {
    $scope.filter.pageName = "Project Diary Report of " + newValue + " View";
    $scope.$root.page.title = "Project Diary Report of " + newValue + " View " + $scope.filter.fromDate + " - " + $scope.filter.toDate;
  });

  $scope.totalHours = (d, k) => d.map(o => o[k]).reduce((a, b) => a + b, 0);
  $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.project = [];
      $scope.project.push({id: 0, name: "Select Project"});
      res.map((o) => $scope.project.push({id: o.projectid, name: o.proj_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();

      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"});
    }
  };

  const getData = (p, f, t) => vServices.callApi("mydiary/all_project_diary?project=" + p + "&fromDate=" + f + "&toDate=" + t, "GET", {}).then(res => convertDataForMyDiary(res));
  $scope.dateChanged = () => getData($scope.filter.project, $scope.filter.fromDate, $scope.filter.toDate);

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

  init();

}]);
