"use strict";

app.controller("HeroProjectAddCtrl", ["$state", "$scope", "$http", "$timeout", "vServices", "filterFilter", "$uibModal", "$document", function ($state, $scope, $http, $timeout, vServices, filterFilter, $uibModal, $document) {
  $scope.$root.page.title = "Assign Hero to Project";
  $scope.params = $state.params;
  $scope.designationOptions = [];
  $scope.minTeamEffort = [];
  $scope.managers = [];
  $scope.selProject = {};
  $scope.projects = [];
  $scope.hero = [];
  $scope.billingRatioOption = [{label: "Select", value: ""}, {label: "Full", value: "Full"}, {label: "Partial", value: "Partial"}];
  $scope.allocation = {
    emp_id: $state.params.employeeid,
    proj_id: 0,
    line_mngr_id: 0,
    proj_mngr_id: 0,
    isBillable: true,
    billingRatio: "",
    billableHours:0,
    partialHours: null,
    fromDate: moment().format("DD-MM-YYYY")
  };

  const forFocus = (d) => $(document).find("#" + d).focus();
  $scope.projectChange = (d) => $scope.selProject = $scope.projects.filter((o) => o.projectid === d);
  $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.partialHours || typeof $scope.allocation.partialHours === "object") {
      toastr.warning("Please fill the total hours ", "Required", {timeOut: 5000});
      forFocus("totalHours");
      return false;
    }
    /*if (!$scope.allocation.billingRatio) {
      toastr.warning("Please select the billing ratio", "Required", {timeOut: 5000});
      forFocus("billingRatio");
      return false;
    }
    else if ($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/add", "POST", $scope.allocation).then(function (res) {
      if (res) {
        toastr.success(res, "Success!", {timeOut: 5000});
        $state.go("projectView", {projectid: $scope.allocation.proj_id}, {reload: true});
      }
    });
  };

  const init = function () {
    if ($state.params && $state.params.employeeid) {
      vServices.callApi("project/all", "GET", {}).then(function (res) {
        if (res && res.length > 0) {
          res = $scope.$root.sort(res,'proj_name');
          $scope.projects = res;
          //$scope.projects.unshift({"projectid": 0, "proj_name": "Select"});
          forFocus('project');
        }
      });
      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});
          $scope.allocation.willActAs = $scope.designationOptions[0].id;
        }
      });
      vServices.callApi("employee/" + $state.params.employeeid, "GET", {}).then((res) => {
        if (res) $scope.hero = res;
      });
      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": ""});
        }
      });
    }
  };
  init();
}]);
