"use strict";

app.controller("ProjectHeroEditCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", "$uibModal", "$document", function ($state, $scope, $http, $timeout, vServices, filterFilter, $uibModal, $document) {
  $scope.$root.page.title = "Manage assignment of Hero in Project";
  $scope.params = $state.params;
  $scope.designationOptions = [];
  $scope.minTeamEffort = [];
  $scope.managers = [];
  $scope.heroes = [];
  $scope.billingRatioOption = [{label: "Select", value: ""}, {label: "Full", value: "Full"}, {label: "Partial", value: "Partial"}];
  $scope.billing_type = [{'value':1,'label':'Hourly'},{'value':2,'label':'Daily'}];
  $scope.emp_allocation_type = [{'value':1,'label':'Billing'},{'value':2,'label':'Shadow (Available)'},{'value':3,'label':'Shadow-I (Not Available)'},{'value':4,'label':'No Billing'}];
  $scope.allocation = {
    emp_id: 0,
    proj_id: 0,
    line_mngr_id: 0,
    proj_mngr_id: 0,
    isBillable: false,
    billingRatio: "",
    billableHours:0,
    partialHours: null,
    fromDate: moment().format("DD-MM-YYYY"),
    emp_allocation_type : 0,
    billing_rate:0, 
    billing_type : $scope.billing_type[0].value,
    profile_id:0,
  };
  $scope.forecastProfile = [];
  
/*Check BillingType----*/ 
$scope.billingCondition = true; $scope.checkBilling = (type) => { 
  if(type==1){ 
      $scope.billingCondition = true; 
  }else{ 
      $scope.billingCondition = false; 
  } } 
  /*End Check BillingType*/
  const forFocus = (d) => $(document).find("#" + d).focus();
  $scope.addAllocation = () => {
    if (!$scope.allocation.line_mngr_id || typeof $scope.allocation.line_mngr_id === "object") {
      toastr.warning("Please select the manager", "Required", {timeOut: 5000});
      forFocus("line_mngr_id");
      return false;
    }
    if (!$scope.allocation.proj_mngr_id || typeof $scope.allocation.proj_mngr_id === "object") {
      toastr.warning("Please select the project manager", "Required", {timeOut: 5000});
      forFocus("proj_mngr_id");
      return false;
    }
    /*if (!$scope.allocation.billingRatio) {
      toastr.warning("Please select the billing ratio", "Required", {timeOut: 5000});
      forFocus("billingRatio");
      return false;
    }
    else if ($scope.allocation.isBillable && $scope.allocation.billingRatio === "Partial" && !$scope.allocation.partialHours) {
      toastr.warning("Please fill the no. of hours", "Required", {timeOut: 5000});
      forFocus("partialHours");
      return false;
    }
    else if ($scope.allocation.billingRatio === "Full") {
      $scope.allocation.partialHours = 8;
    }*/
    $scope.allocation.isBillable  = $scope.allocation.billableHours > 0;
    $scope.allocation.billingRatio = "Partial";
    vServices.callApi("allocation/edit", "PUT", $scope.allocation).then(function (res) {
      if (res) {
        toastr.success(res, "Success!");
        // $state.go("projectView", {projectid: $scope.allocation.projectid});
        window.location = '/projects/view/'+$scope.allocation.projectid;
      }
    });
  };
  const init = function () {
    if ($state.params && $state.params.allocationid) {
      vServices.callApi("designation/all", "GET", {}).then((res) => {
        if (res && res.length > 0) {
          $scope.designationOptions = res;
          $scope.designationOptions.unshift({"id": "0", "desg_name": "Select", "desg_description": null});
        }
      });

      vServices.callApi("employee/manager/all", "GET", {}).then((res) => {
        if (res) {
          $scope.managers = res;
          $scope.managers.unshift({"employeeid": "0", "emp_first_name": "Select", "emp_last_name": ""});
          vServices.callApi("allocation/" + $state.params.allocationid, "GET", {}).then((res) => {
            if (res) {
              $scope.allocation = res[0];
              $scope.allocation.billing_rate = parseFloat($scope.allocation.billing_rate);
              $scope.allocation.partialHours = parseFloat($scope.allocation.partialHours);
              $scope.allocation.billableHours = parseFloat($scope.allocation.billableHours);
              $scope.allocation.willActAs = ($scope.allocation.willActAs ? $scope.allocation.willActAs : $scope.designationOptions[0].id).toString().trim();
            }
          });
        }
      });
      vServices.callApi("project/projectForecastProfile", "GET").then(function (res) {
        if (res.length>0){
            $scope.forecastProfile = res;
        }
    });
    }
  };
  init();
}]);
