"use strict";
appCore.controller("TipReportCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "$uibModal", "$document", 
function ($state, $scope, $http, $timeout, vServices, $uibModal, $document) {
    $scope.$root.page.title = "Tip Report";
    $scope.tip = [];
    $scope.dTable = null;
    $scope.tipreports = null;
    $scope.helpComment = null;


	
	$scope.rmsAction = (reportID) => {	
	var   x=angular.element(document.getElementById("helpComment"));      
    $scope.textbox = x.val();
		$scope.tipr = { 
        helpComment  : $scope.textbox,        
        reportID  : reportID,        
    }; 
        console.log($scope.tipr);
			vServices.callApi("tip/rmsaction", "PUT", $scope.tipr).then((res) => {
          if (res) {
			  console.log($scope.tipr);
            toastr.success(res, "Success");
            $state.go($state.current, {}, {reload: true});
          }
        });
      };
	  
    $scope.filter = {
        fromDate: moment().subtract(2, "days").format("DD-MM-YYYY"),
        toDate: moment().format("DD-MM-YYYY"),
        tip: 0,
        pageName: "Tip Report",
        selType: "Default",
        type: [{val: "View Type"}, {val: "Default"}, {val: "Tip",}]
    };

    $scope.$watch("filter.selType", (newValue, oldValue) => {
        $scope.filter.pageName = "Tip Report ";
    });
    let numOr0 = n => isNaN(n) ? 0 : n;

    $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.tipreports[data][i - 1][c]));

    const convertDataForProject = res => {
        if (res) {
            $scope.tip = [];
            $scope.tip.push({id: 0, title: " Select Tip"});
           // res = res.filter(emp => emp.status === 'active');
            res.map((o) => $scope.tip.push({id: o.id, title: o.title}));
            $scope.tip = $scope.$root.sort($scope.tip, 'title');
        }
    };

    const convertDataForTipreport = res => {
        $scope.tipReport = [];
        if($scope.dTable) $scope.dTable.destroy();

        if (res && res.length > 0) {
          res.forEach(i=>{
            i.dateTime = i.dateTime.split('.')[0]
          })  
          $scope.tipReport = res;
			      
            console.log($scope.tipReport);
            $scope.$root.callDataTable(".tipreportDataTableView", 500, (t) => {
                $scope.dTable = t;
            }, {orientation: "landscape"});
        }
		else{
			$scope.$root.callDataTable(".tipreportDataTableView", 500, (t) => {
                $scope.dTable = t;
            }, {orientation: "landscape"});
		}
    };

    $scope.dateChanged = (p) => vServices.callApi("tip/allTipReport?tip=" + p, "GET", {}).then(res => convertDataForTipreport(res));

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

	 /* Model code start*/
      $scope.updateRmsModel = (obj) => {
        let modalInstance = $uibModal.open({
          animation: true,
          ariaLabelledBy: "modal-title",
          ariaDescribedBy: "modal-body",
          templateUrl: "UpdateCa.html",
          controller: "UpdateCaCtrl",
          controllerAs: "$scope",
          size: "md",//sm md lg
          backdrop: "static",
          resolve: {
            data: () => {
              return {
                tipReport: obj
              };
            }
          }
        });

        modalInstance.result.then((data) => {
          $scope.rmsAction(data.tipReport.reportID);
        }, () => {
          toastr.warning("Action has been canceled", "Cancel");
        });
      };
}]);

 var tipreport = function ($uibModalInstance, data, $scope) {
    $scope.tipReport = data;
	
	console.log($scope.tipReport);
    $scope.ok = () => $uibModalInstance.close($scope.tipReport);
    $scope.cancel = () => $uibModalInstance.dismiss("cancel");
  }
