﻿angular.module('registerTarifas', [])
    .controller('registerTarifasCtrl', ['$scope', '$http', 'growl', 'otrosServics', 'tarifasServices', function ($scope, $http, growl, otrosServics, tarifasServices) {

        $scope.city = "";
        $scope.Citys = [];
        $scope.banGuardando = false;
        $scope.TypesTarifas = [
            {
                Id: "1",
                Name: "1"
            },
            {
                Id: "2",
                Name: "2",
            },
            {
                Id: "3",
                Name: "3"
            },
            {
                Id: "4",
                Name: "4"
            },
            {
                Id: "5",
                Name: "5"
            },
            {
                Id: "6",
                Name: "6"
            }
        ];

        getCitys();

        $scope.deleteTarfica = function (indice) {
            $scope.Tarifas.splice(indice, 1);
        };

        $scope.agregarTarifa = function () {
            if ($scope.Tarifas != undefined) {
                $scope.Tarifas.push({
                    Name: "",
                    Distance: "",
                    Valor: 0,
                    VFactorCalidad: 0,
                    IdTypeTarifa: "1"
                });
            }

        };

        $scope.initTarifa = function (item) {
            item.IdTypeTarifa = item.IdTypeTarifa.toString();
        };

        $scope.getTarifasByCity = function () {
            var city = getIdVec($scope.Citys, $scope.city, "Name", "Id");
            if (city == null) {
                growl.error("Debe seleccionar una ciudad válida.", { title: 'Error!' });
                return;
            }
            var res = tarifasServices.getTarficasByCity(city);
            res.success(function (data, status, headers, config) {
                console.log(data);

                if (data == null) {
                    init();
                } else if (data.length == 0) {
                    init();
                } else {

                    $scope.Tarifas = data;
                }
            })
                .error(function (errorPl) {
                    growl.error(errorPl.data.MessageDetail, { title: 'Error!' });
                });
        };

        $scope.registerTarifas = function () {
            var city = getIdVec($scope.Citys, $scope.city, "Name", "Id");
            if (city == null) {
                growl.error("Debe seleccionar una ciudad válida.", { title: 'Error!' });
                return;
            }
            $scope.banGuardando = true;
            var res = tarifasServices.postTarifas($scope.Tarifas, city);
            res.success(function (data, status, headers, config) {
                $scope.banGuardando = false;
                if (data.state == 1) {
                    growl.success(data.msg, { title: 'Success!' });
                    $scope.city = "";
                    $scope.Tarifas = [];
                } else {
                    growl.error(data.msg, { title: 'Error!' });
                }
            })
                .error(function (errorPl) {
                    $scope.banGuardando = false;
                    growl.error(errorPl.data.MessageDetail, { title: 'Error!' });
                });
        };

        ///Consulta los parametros  del taximetro por ciudad 
        $scope.getPtaximetroByCity = function () {
            var city = getIdVec($scope.Citys, $scope.city, "Name", "Id");
            if (city == null) {
                growl.error("Debe seleccionar una ciudad válida.", { title: 'Error!' });
                return;
            }
            var res = tarifasServices.getPtaximetroByCity(city);
            res.success(function (data, status, headers, config) {
                if (data == null) {
                    initPtaxiometros();
                } else if (data.length == 0) {
                    initPtaxiometros();
                } else {

                    $scope.Ptaximetros = data;
                }
            })
                .error(function (errorPl) {
                    growl.error(errorPl.data.MessageDetail, { title: 'Error!' });
                });
        };

        ///registra los  parametros del taximetro
        $scope.registerPTaximetro = function () {
            var city = getIdVec($scope.Citys, $scope.city, "Name", "Id");
            if (city == null) {
                growl.error("Debe seleccionar una ciudad válida.", { title: 'Error!' });
                return;
            }
            $scope.banGuardando = true;
            var res = tarifasServices.postPtaximetro($scope.Ptaximetros, city);
            res.success(function (data, status, headers, config) {
                $scope.banGuardando = false;
                if (data.state == 1) {
                    growl.success(data.msg, { title: 'Parametros registrados !' });
                    $scope.city = "";
                    $scope.Ptaximetros = [];
                } else {
                    growl.error(data.msg, { title: 'Error! al registrar lso parametros' });
                }
            })
                .error(function (errorPl) {
                    $scope.banGuardando = false;
                    growl.error(errorPl.data.MessageDetail, { title: 'Error!' });
                });
        };


        $scope.agregarParametro = function () {
            var city = getIdVec($scope.Citys, $scope.city, "Name", "Id");
            if (city == null) {
                growl.error("Debe seleccionar una ciudad válida.", { title: 'Error!' });
                return;
            }

            if ($scope.Ptaximetros != undefined) {
                $scope.Ptaximetros.push({
                    IdCity: city,
                    Item: "",
                    Valor: 0,
                    Step: 0,
                    Until: 0,
                    TimerStart: "00:00:00",
                    TimerEnd: "23:59:59",
                    IsDefault: false,
                    Observation: ""
                });
            }

        };


        $scope.deleteParametro = function (indice) {
            $scope.Ptaximetros.splice(indice, 1);
        };

        function getCitys() {
            var res = otrosServics.getAllCitys();
            res.success(function (data, status, headers, config) {
                $scope.Citys = data;
            })
                .error(function (errorPl) {
                    growl.error(errorPl.data.MessageDetail, { title: 'Error!' });
                });
        };

        function init() {
            $scope.Tarifas = [
                {
                    Name: "Costo x Kilometro",
                    Distance: "",
                    Valor: 0,
                    VFactorCalidad: 0,
                    IdTypeTarifa: ""
                },
                {
                    Name: "Banderazo",
                    Distance: "",
                    Valor: 0,
                    VFactorCalidad: 0,
                    IdTypeTarifa: ""
                },
                {
                    Name: "R - Aeropuerto",
                    Distance: "",
                    Valor: 0,
                    VFactorCalidad: 0,
                    IdTypeTarifa: ""
                },
                {
                    Name: "R - Nocturno(20-5) D,F",
                    Distance: "",
                    Valor: 0,
                    VFactorCalidad: 0,
                    IdTypeTarifa: ""
                },
                {
                    Name: "Carrera Minima",
                    Distance: "",
                    Valor: 0,
                    VFactorCalidad: 0,
                    IdTypeTarifa: ""
                },
                {
                    Name: "Servicio P-P",
                    Distance: "",
                    Valor: 0,
                    VFactorCalidad: 0,
                    IdTypeTarifa: ""
                }
            ];
        }

        function initPtaxiometros() {
            var city = getIdVec($scope.Citys, $scope.city, "Name", "Id");
            if (city == null) {
                growl.error("Debe seleccionar una ciudad válida.", { title: 'Error!' });
                return;
            }

            $scope.Ptaximetros = [
                {
                    IdCity: city,
                    Item: "VALORKILOMETRO_500_KM",
                    Valor: 0,
                    TimerStart: "00:00:00",
                    TimerEnd: "23:59:59",
                    IsDefault: false,
                    Observation: ""
                },
                {
                    IdCity: city,
                    Item: "BANDERAZO",
                    Valor: 0,
                    TimerStart: "00:00:00",
                    TimerEnd: "23:59:59",
                    IsDefault: false,
                    Observation: ""
                }

            ];
        }
    }]);