﻿
(function () {
	'use strict';

	angular
		.module('app')
		.constant(
			"REPORTDIRECTIVE_CONSTANTS", {
				"PAGE_NAME": "Reporte Directriz 01 2021",
				"PAGEIDENTITY": "reportDirective012021"
			})
		.controller('reportDirective012021Controller', reportDirective012021Controller);

	reportDirective012021Controller.$inject = ['$scope', '$http', '$filter', 'auth0Service', 'permissionsService', '__env', 'REPORTDIRECTIVE_CONSTANTS', 'reportsService', 'blockUI', 'CompanyService'];

	function reportDirective012021Controller($scope, $http, $filter, auth0Service, permissionsService, __env, REPORTDIRECTIVE_CONSTANTS, reportsService, blockUI, CompanyService) {
		var vm = this;

		vm.permissions = permissionsService.CurrentRoutePermissions();

		vm.$onInit = function () {
			vm.groupList = [];
			vm.companyListOriginal = [];
			vm.companyList = [];
			vm.clientList = [];
			vm.accountManagerList = [];
			vm.accountManagerOriginal = [];

			vm.exportHeaders = [];
			vm.exportData = [];
			vm.serviceList = [];
			vm.showRecords = false;
			vm.showEmptyDropDownRecord = false;
			vm.deactivateDDl = true;

			vm.GetScheduleClass = GetScheduleClass;
			vm.GetAssignmentClass = GetAssignmentClass;
			vm.GetDifferenceColor = GetDifferenceColor;
			vm.SetSelectedCompany = SetSelectedCompany;
			vm.SetSelectedGroup = SetSelectedGroup;
			vm.search = search;
			vm.clean = Clean;
			vm.ApplyClass = __env.ApplyStatusClass;
			vm.itemsByPage = 50;
			vm.selectedAccountMgr = undefined;
			vm.dateFormat = __env.dateFormat;
			vm.filters = new __env.ReportFilterObj();

			vm.modalAlert = __env.NewModal(__env.Form.CAPTION + 'Alert');

			let date = $filter('date')(new Date(), __env.timeFormat).replaceAll("/", "-");
			vm.exportFilename = `${REPORTDIRECTIVE_CONSTANTS.PAGE_NAME}_${date}.xls`;
			CreateHeaders();
			Init();
		};

		//events actualiza titulo de header de pagina
		$scope.$emit(__env.updateMainTitle, {
			data: REPORTDIRECTIVE_CONSTANTS.PAGE_NAME
		});

		function Init() {
			CompanyService.GetAllowedCompanyGroups(SetGroupList);
			CompanyService.GetAllowedCompanies(SetCompanyList);
			reportsService.GetAllSectors(SetSectorList);
			reportsService.GetAllAccountManagers(SetAccountMgrList);
			SetStatusList();
		}

		function SetSelectedCompany(company) {
			if (company !== "") {
				reportsService.GetClientsByCompany(company, SetClientList);
				reportsService.GetZonesByCompany(company, SetZoneList);
				reportsService.GetAllSubSectors(company, SetSubSectorList);
				vm.deactivateDDl = false;
				let comp = vm.companyListOriginal.filter(x => x.name === company);
				if (comp.length > 0) {
					vm.accountManagerList = vm.accountManagerOriginal.filter(x => x.companyGroup === comp[0].companyGroup);
					vm.accountManagerList.unshift({ id: "", name: "" });
				}
			}
			else {
				vm.clientList = [];
				vm.clientList.unshift({ id: "", name: "" });
				vm.filters.clientId = null;
				vm.zoneList = [];
				vm.zoneList.unshift({ id: "", name: "" });
				vm.subSectorList = [];
				vm.subSectorList.unshift({ id: "", name: "" });
				vm.filters.zone = null;
				vm.filters.sector = null;
				vm.filters.subSector = null;
				vm.deactivateDDl = true;
				vm.filters.company = null;
				vm.selectedAccountMgr = null;
				vm.accountManagerList = vm.accountManagerOriginal;
			}
		}

		function SetSelectedGroup(group) {
			SetSelectedCompany("");
			if (group !== "") {
				vm.companyList = vm.companyListOriginal.filter(x => x.companyGroup === group);
				vm.companyList.unshift({ id: "", name: "" });

				vm.accountManagerList = vm.accountManagerOriginal.filter(x => x.companyGroup === group);
				vm.accountManagerList.unshift({ id: "", name: "" });
			}
			else {
				vm.companyList = vm.companyListOriginal;
			}
		}

		function CreateHeaders() {
			vm.exportHeaders = [];
			vm.exportHeaders.push(new __env.GridHeaderItem("alphaId", "Alpha", 5, { sort: true, style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("company", "CIA", 5, { style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("alphaName", "Desc Alpha", 10, { style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("serviceNumber", "No Serv", 3, { style: "text-align:center;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("serviceName", "Servicio", 10, { style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("quantity", "Cantidad", 5, { style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("directPlacesQuantity", "Plazas fijas", 4, { style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("theoPlacesQuantity", "Plazas CL", 4, { style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("totalPlaces", "T Plazas", 4, { style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("province", "Provincia", 5, { style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("canton", "Cantón", 5, { style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("district", "Distrito", 5, { style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("otherSigns", "Otras señas", 14, { style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("coordinates", "Coordenadas", 6, { style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("weaponsQuantity", "Cant Armas", 4, { style: "text-align:left;" }));
			vm.exportHeaders.push(new __env.GridHeaderItem("googleMap", "GeoRef Maps", 4, { style: "text-align:left;" }));
		}

		function SetGroupList(list) {
			vm.groupList = [];
			if (list !== undefined && list !== "" && list.length > 0) {
				list.unshift({ id: "", name: "" });
				vm.groupList = list;
			}
		}

		function SetCompanyList(list) {
			vm.companyListOriginal = [];
			vm.companyList = [];
			vm.clientList = [];
			vm.clientList.unshift({ id: "", name: "" });
			vm.zoneList = [];
			vm.zoneList.unshift({ id: "", name: "" });
			vm.subSectorList = [];
			vm.subSectorList.unshift({ id: "", name: "" });
			if (list !== undefined && list !== "" && list.length > 0) {
				list.unshift({ id: "", name: "" });
				vm.companyListOriginal = list;
				vm.companyList = vm.companyListOriginal;
			}
		}

		function SetClientList(list) {
			vm.clientList = [];
			if (list !== undefined && list !== "" && list.length > 0) {
				list.sort((a, b) => (a.alias > b.alias) ? 1 : (b.alias > a.alias) ? -1 : 0);
				list.unshift({ id: "", name: "" });
				vm.clientList = list;
			}
		}

		function SetZoneList(list) {
			vm.zoneList = [];
			if (list !== undefined && list !== "" && list.length > 0) {
				list.unshift({ id: "", name: "" });
				vm.zoneList = list;
			}
		}

		function SetAccountMgrList(list) {
			vm.accountManagerList = [];
			if (list !== undefined && list !== "" && list.length > 0) {
				list.unshift({ id: "", name: "" });
				vm.accountManagerOriginal = list;
				vm.accountManagerList = vm.accountManagerOriginal;
			}
		}

		function SetSectorList(list) {
			vm.sectorList = [];
			if (list !== undefined && list !== "" && list.length > 0) {
				list.unshift({ id: "", name: "" });
				vm.sectorList = list;
			}
		}

		function SetSubSectorList(list) {
			vm.subSectorList = [];
			if (list !== undefined && list !== "" && list.length > 0) {
				list.unshift({ id: "", name: "" });
				vm.subSectorList = list;
			}
		}

		function SetStatusList() {
			vm.statusList = [];
			let temp = [];

			temp.push({ id: 0, name: "Todos" });
			temp.push({ id: 1, name: "Vencidos" });

			vm.statusList = temp;
		}

		function search() {
			if (vm.selectedAccountMgr) {
				vm.filters.accountMgr = `${vm.selectedAccountMgr.id}-${vm.selectedAccountMgr.companyGroup}`
			}
			findRecords();
		}

		function Clean() {
			vm.selectedAccountMgr = undefined;
			vm.filters = new __env.ReportFilterObj();
			vm.exportData = [];
			vm.serviceList = [];
			vm.showRecords = false;
			vm.showEmptyDropDownRecord = false;
			vm.deactivateDDl = true;
		}

		//find records of personal actions
		function findRecords() {
			let valid = false;

			if (vm.filters.company || vm.filters.from || vm.filters.to || vm.filters.alpha || vm.filters.clientId || vm.filters.zone || vm.filters.sector || vm.filters.subSector || vm.filters.status || vm.filters.concept || vm.filters.accountMgr || vm.filters.group) {
				valid = true;
			}
			if (valid) {
				blockUI.start();
				vm.showRecords = false;
				reportsService.GetDirective012021Report(vm.filters, processData);
			}
			else {
				vm.modalAlert.hideOkButton = true;
				vm.modalAlert.caption = `Seleccione al menos un filtro`;
				vm.modalAlert.Show('Alerta', __env.ModalSize.SMALL);
			}
		}

		function reloadData() {
			reportsService.GetDirective022021Report(processData);
		}

		//modal actions
		function processData(value) {
			vm.serviceList = value;
			vm.showRecords = true;
			vm.exportData = __env.PrepareToExcel(value, vm.exportHeaders, $filter);
			blockUI.stop();
		}


		function GetAssignmentClass(status) {
			if (status === __env.AssignStatus.Complete) {
				return "label-success";
			}
			else if (status === __env.AssignStatus.Pending) {
				return "label-info";
			}
			else if (status === __env.AssignStatus.Partial) {
				return "label-warning";
			}
			else {
				return "label-danger";
			}
		}

		function GetScheduleClass(status) {
			if (status === __env.AssignScheduleStatus.Complete) {
				return "label-success";
			}
			else if (status === __env.AssignScheduleStatus.incomplete) {
				return "label-danger";
			}
		}

		function GetDifferenceColor(value) {
			if (value < 0) {
				return "color:red";
			}
			else if (value === 0) {
				return "color:green";
			}
			return "";
		}
	}
})();
