app.controller('controladorDoctor', ['$scope','$http','$window' ,'$timeout','$rootScope','$interval', '$sce','$location', function ($scope,$http,$window,$timeout,$rootScope,$interval,$sce,$location) {
    // Preguntamos los permisos que tiene para ver en la barra lateral

    $scope.muestraSpinner=false;
    $scope.bool_nota=false;

     var hoy = new Date();
    var fecha = ( hoy.getMonth() + 1 ) + '/' + hoy.getDate() + '/' + hoy.getFullYear();
    //console.log(fecha);

    $scope.fec_fin = fecha;

    var date = new Date();
    date.setDate(date.getDate() - 7);

    var fecha_ini = ( date.getMonth() + 1 ) + '/' + date.getDate() + '/' + date.getFullYear();

    $scope.fec_ini = fecha_ini;

    $("#main").addClass("main-doctor")
    $('.select2').select2();

    var parametros={};
      parametros["cual"]="consultaUsuarioDoctor";
      $http.post('app/ws/ws_dashboarddr.php',JSON.stringify(parametros)).then(function(msg){
          $scope.doctors=msg.data.doctors;
          //$scope.estados_academicos=msg.data.estados_academicos;
      });

       var parametros={};
      parametros["cual"]="traerClinics";
      $http.post('app/ws/ws_dashboarddr.php',JSON.stringify(parametros)).then(function(msg){
          $scope.clinics=msg.data.clinics;
      });

    $scope.traeSolicitudDoctor = function(){
         $scope.BuscaDatos = false;


        var parametros={};
        parametros["cual"]="consultaDoctor";


        $http.post('app/ws/ws_dashboarddr.php',JSON.stringify(parametros)).then(function(msg){

            $scope.doctores=msg.data.doctores;
            $scope.valores=msg.data.valores;
            //console.log($scope.historia);
            //console.log("hola");
            //console.log($scope.doctores);
        });
    }

    $scope.traeResultadoDoctor = function(){
        $scope.BuscaDatos = true;

        if($scope.fec_ini)
        {
          var fecha=$scope.fec_ini;
          var fecha_ini = fecha.split("/");
          var fec_ini=fecha_ini[2]+"-"+fecha_ini[0]+"-"+fecha_ini[1];
        }

        if($scope.fec_fin)
        {
          var fecha=$scope.fec_fin;
          var fecha_fin = fecha.split("/");
          var fec_fin=fecha_fin[2]+"-"+fecha_fin[0]+"-"+fecha_fin[1];
        }

        var parametros={};
        parametros["cual"]="consultaDoctor";
        parametros["des_login"]=$scope.des_login;
        parametros["cod_clinic"]=$scope.cod_clinic;
        parametros["fec_ini"]=fec_ini;
        parametros["fec_fin"]=fec_fin;


        $http.post('app/ws/ws_dashboarddr.php',JSON.stringify(parametros)).then(function(msg){

            $scope.doctores=msg.data.doctores;
            $scope.valores=msg.data.valores;


            $templateRequest('app/templates/doctors/doctoradmin/ventanaDoctor.html').then(function(html){
              var template = angular.element(html);
              $("#doctors").html(template);
              $compile(template)($scope);
            });
        });
    }

    $scope.descargarDoctor = function () {
      if ($scope.fec_ini) {
        var fecha = $scope.fec_ini;
        var fecha_ini = fecha.split("/");
        var fec_ini = fecha_ini[2] + "-" + fecha_ini[0] + "-" + fecha_ini[1];
      }

      if ($scope.fec_fin) {
        var fecha = $scope.fec_fin;
        var fecha_fin = fecha.split("/");
        var fec_fin = fecha_fin[2] + "-" + fecha_fin[0] + "-" + fecha_fin[1];
      }

      var fec_ini = fec_ini;
      var fec_fin = fec_fin;

      let doctor = ''
      if ($scope.des_login) {
        doctor = `&des_login=${$scope.des_login}`;
      }

      let clinica = '';
      if ($scope.cod_clinic) {
        clinica = `&cod_clinic=${$scope.cod_clinic}`
      }

      location.href = 'app/excel/bbdd_doctor.php?fec_ini=' + fec_ini + '&fec_fin=' + fec_fin + clinica + doctor;
    }

  $scope.traeFicha = function(des_login){

      $scope.cliente=des_login;

      //$location.path('/ficha_cliente/'+$scope.cliente);
      window.open('#!/ficha_cliente/'+$scope.cliente, '_blank');

  }

  $scope.descargaVideo = function(id_solicitud){
     
        var ventanaDescarga = window.open('', '_blank');

        var parametros={};
        parametros["cual"]="linkVideo";
        parametros["id_solicitud"]=id_solicitud;
        parametros["versionApi"]=2;



        $http.post('app/ws/ws_api.php',JSON.stringify(parametros)).then(function(msg){
             console.log(msg);
           if (msg.data.url) {
              ventanaDescarga.location.href = msg.data.url;
                
            } else {
                alert('Could not generate download link.');
                ventanaDescarga.close();
            }
        });
    }

   // $scope.traeSolicitudDoctor();


}]);