<?php 
    /*-------------------------
    Autor: Amner Saucedo Sosa
    Web: www.abisoftgt.net
    E-Mail: waptoing7@gmail.com
    ---------------------------*/
    $active2="active";
    $active3="active";
    include "header.php";
    include "sidebar.php";

    
    

if (isset($_GET['id']) && !empty($_GET['id'])){
        $id=intval($_GET["id"]);
    } else {
       print "<script>window.location='tickets.php?error'</script>";
    }


    //$id = intval($_GET['id']);
    $sql = mysqli_query($con, "select * from tickets where id=$id");
   $rows= mysqli_fetch_array($sql);
        $id_ticket= $rows['id'];
        $name= $rows['name'];
        $email= $rows['email'];
        $phone= $rows['phone'];
        $cell_phone= $rows['cell_phone'];
        $contact_schedule= $rows['contact_schedule'];
        $kind_id= $rows['kind_id'];
        $comment= $rows['comment'];
        $image= $rows['image'];
        $area= $rows['area'];
        $asunt= $rows['asunt'];
        $id_team= $rows['id_team'];
        $pass_team= $rows['pass_team'];
        $client_id= $rows['client_id'];
        $created_at= $rows['created_at'];
        $status_id= $rows['status_id'];
        $number_ticket= $rows['number_ticket'];
        $asigned_id= $rows['asigned_id'];
        $atendido= $rows['date_atendid'];
        $sistema= $rows['sistema'];
        $finalizado=$rows['finalizado'];
        $tipo_requerimiento=$rows['tipo_requerimiento'];
        $image=$rows['image'];
        $date_asigned=$rows['date_asigned'];
    
    

    if (mysqli_num_rows($sql)==0) {
       //header("location: tickets.php");
        print "<script>window.location='registrados.php'</script>";
    }

    //tipo requerimiento
    //if ($tipo_requerimiento!=0) {
        $requerimientos=mysqli_query($con, "select * from tipos_requerimientos where id=$tipo_requerimiento");
        $rows=mysqli_fetch_array($requerimientos);
           $name_require=$rows['name'];
        
   // }   

    if ($asigned_id!=0) {
        $user_id=$asigned_id;
        $user=mysqli_query($con, "select * from user where id=$user_id");
        $row=mysqli_fetch_array($user);
           $name_user=$row['name'];
           $lastname_user=$row['lastname'];

        
    }

    $status=mysqli_query($con, "select * from status where id=$status_id");
    $rows=mysqli_fetch_array($status);
       $name_status=$rows['name'];

    $fechaactual = date('Y-m-d H:i:s');
    
    $dtefechacreado = new DateTime($created_at);
    $dtefechaactual = new DateTime($fechaactual);
    $diferenciafechas = $dtefechacreado->diff($dtefechaactual);
    $diferenciafechas1 = $diferenciafechas->format("%d %H:%i:%s");

    $consulta = mysqli_query($con, "select * from area where id=$area");
    $resultado= mysqli_fetch_array($consulta);
    $prioridad_area = $resultado['priority_id'];
    $nombre = $resultado['name'];

    $service=mysqli_query($con, "select * from area where id=$area");
    $rows1=mysqli_fetch_array($service);
    $servicio=$rows1['name'];

    $consulta1 = mysqli_query($con, "select * from priority where id=$prioridad_area");
    $resultado1= mysqli_fetch_array($consulta1);
    $tiempo_prioridad = $resultado1['time'];

    /*Usando la variable de $prioridad_area entramos a cada uno de los casos del switch en donde se harán una serie de pasos. Entre ellos sumar tiempo."*/
    switch($prioridad_area){
        case 1:
            /*a continuacion sumamos las horas de la prioridad 1, que son 2 */
            $sumatiempo=date("Y-m-d H:i:s", strtotime($created_at . ' +2 hour'));
            $dtesumatiempo=new DateTime($sumatiempo);
            $tiempofinal = $dtefechaactual->diff($dtesumatiempo);
            $tiempovida = $tiempofinal->format("%r%d | %H:%i:%s");
            break;
        case 2: 
            /*a continuacion sumamos las horas de la prioridad 2, que son 4 */
            $sumatiempo=date("Y-m-d H:i:s", strtotime($created_at . ' +4 hour'));
            $dtesumatiempo=new DateTime($sumatiempo);
            $tiempofinal = $dtefechaactual->diff($dtesumatiempo);
            $tiempovida = $tiempofinal->format("%r%d | %H:%i:%s");
            break;
        case 3:
            /*a continuacion sumamos las horas de la prioridad 3, que son 24*/
            $sumatiempo=date("Y-m-d H:i:s", strtotime($created_at . ' +24 hour'));
            $dtesumatiempo=new DateTime($sumatiempo);
            $tiempofinal = $dtefechaactual->diff($dtesumatiempo);
            $tiempovida = $tiempofinal->format("%r%d | %H:%i:%s");
            break;
        case 4:
            /*a continuacion sumamos las horas de la prioridad 4, que son 48*/
            $sumatiempo=date("Y-m-d H:i:s", strtotime($created_at . ' +48 hour'));
            $dtesumatiempo=new DateTime($sumatiempo);
            $tiempofinal = $dtefechaactual->diff($dtesumatiempo);
            $tiempovida = $tiempofinal->format("%r%d | %H:%i:%s");
            break;
        case 5:
            /*a continuacion sumamos las horas de la prioridad 5, que son 720*/
            $sumatiempo=date("Y-m-d H:i:s", strtotime($created_at . ' +720 hour'));
            $dtesumatiempo=new DateTime($sumatiempo);
            $tiempofinal = $dtefechaactual->diff($dtesumatiempo);
            $tiempovida = $tiempofinal->format("%r%d | %H:%i:%s");
            break;
            
    }

?>

      <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <section class="content-header">
            <!-- <h1>
              <i class="fa fa-edit icon-title"></i> Agregar Ticket
            </h1> -->
            <ol class="breadcrumb">
              <li><a href="dashboard.php"><i class="fa fa-dashboard"></i> Dashboard </a></li>
              <li class="active"> Detalles ticket </li>
            </ol>
        </section>
        <?php if($is_admin!=1){?>
        <br><br>
        <a class="btn btn-default pull-right" href="mistickets.php"><i class="glyphicon glyphicon-arrow-left"></i> Regresar</a>
        <br><br>
        <?php } ?>
        <!-- Main content -->
        <section class="content">
            
            
            <?php if($is_admin==1){?>
            <!-- <div class="row">
                <div class="col-md-10">
                    <a href="tickets.php" class="btn btn-default  pull-right"><i class="glyphicon glyphicon-arrow-left"></i> Regresar</a>
                </div>
                <br>
                <br>
            </div> -->
            <?php } ?>
            <br>
            <div class="row">
                <div class="col-md-3"></div>
                <div class="panel panel-warning col-md-6">
                    <?php if($status_id=='4') {?>
                    <div class="panel-heading">
                        <h3 class="panel-title">TICKET FINALIZADO</h3>
                    </div>
                    
                    <?php }else{?>
                    <div class="panel-heading">
                        <h3 class="panel-title">TIEMPO RESTANTE PARA CERRAR EL TICKET</h3>
                    </div>
                    <div class="panel-body">
                        <p class="text-center">
                            [Días | H : M : S] <br>
                            <b>
                            <?php 
                            if(strpos($tiempovida, '-') !== false){
                                echo "<p class='text-danger text-center'>[00 | 00:00:00] Ticket vencido.</p>";
                            } else{
                                echo "[".$tiempovida."]";}   
                            ?>
                        </b>
                        </p>
                        
                        
                    </div>
                    <?php }?>
                </div>
                <div class="col-md-3"></div>
            </div>
            <div class="row">
                <div class="col-md-3"></div>
                <div class="panel panel-danger col-md-2">
                    <div class="panel-heading">
                        <h3 class="panel-title">REGISTRADO POR</h3>
                    </div>
                    <div class="panel-body">
                        <?php echo $name ?>
                    </div>
                </div>
                <div class="col-md-0"></div>
                <?php if($asigned_id!=0){ ?>
                <div class="panel panel-warning col-md-2">
                    <div class="panel-heading">
                        <h3 class="panel-title">ASIGNADO</h3>
                    </div>
                    <div class="panel-body">
                        <?php echo $date_asigned ?>
                    </div>
                </div>
                <?php }else{ ?>
                    <div class="panel panel-danger col-md-2">
                    <div class="panel-heading">
                        <h3 class="panel-title">ASIGNADO</h3>
                    </div>
                    <div class="panel-body">
                       Sin Asignar
                    </div>
                </div>
                <?php } ?>
                <div class="col-md-0"></div>
                    <?php if($atendido!="0000-00-00 00:00:00"){ ?>
                    <div class="panel panel-success col-md-2">
                        <div class="panel-heading">
                            <h3 class="panel-title">ATENDIDO</h3>
                        </div>
                        <div class="panel-body">
                            Atendido el dia: <?php echo $atendido ?>
                        </div>
                    </div>
                    <?php }else{ ?>
                    <div class="panel panel-danger col-md-2">
                        <div class="panel-heading">
                            <h3 class="panel-title">ATENDIDO</h3>
                        </div>
                        <div class="panel-body">
                           Sin Antender
                        </div>
                    </div>
                    <?php } ?>
            </div>
                <div class="row">
                    <div class="col-md-2"></div>
                    <?php if($sistema!=""){ ?>
                    <!--<div class="panel panel-info col-md-3">
                        <div class="panel-heading">
                            <h3 class="panel-title">MENSAJE</h3>
                        </div>
                        <div class="panel-body">
                            <?php echo $sistema ?>
                        </div>
                    </div>-->
                    <?php }else{ ?>
                    <!--<div class="panel panel-danger col-md-3">
                        <div class="panel-heading">
                            <h3 class="panel-title">MENSAJE</h3>
                        </div>
                        <div class="panel-body">
                            Sin actividad
                        </div> 
                    </div>-->
                    <?php } ?>
                </div>
                



            <div class="row">
                <div class="col-md-1"></div>
                <div class="col-md-10">
                    <div class="box box-success">
                        <!-- form start -->
                        <form role="form" class="form-horizontal" enctype="multipart/form-data" action="action/addticket.php" method="POST" >
                            <div class="box-body">
                                <?php if($asunt!=""){ 
                                    $kinds=mysqli_query($con, "select * from kind where id=$kind_id");
                                    if ($row=mysqli_fetch_array($kinds)) {
                                        $name_kind=$row['name'];
                                    }
                                ?>
                                <div class="form-group" style="display:none;">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >ID Ticket:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input name="id_update" id="id_update" type="text" class="form-control col-md-7 col-xs-12" value="<?php echo $id_ticket ?>" readonly>
                                        </div>
                                    </div>
                                <div class="form-group">
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Descripción:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <textarea style="resize: none;" class="form-control col-md-7 col-xs-12" readonly><?php echo  $comment ?></textarea>
                                        </div>
                                    </div>
                                    <label class=" control-label col-md-3 col-sm-3 col-xs-12" for="kind_id">Área <span class="required">*</span>
                                    </label>
                                    <div class="col-md-6 col-sm-6 col-xs-12">
                                        <select class="chosen-select form-control" name="kind_id"  id="kind_id" required data-placeholder="-- Seleccionar --" autocomplete="off">
                                             
			
				<option value="2">CCTV</option>
				<option value="3">Celulares</option>
				<option value="4">Compras IT</option>
				<option value="5">CONTPAQ</option>
				<option value="6">HelpDesk</option>
				<option value="7">Office 365</option>
				<option value="8">Redes</option>
				<option value="9">SAI</option>
				<option value="10">SEINTRA</option>
				<option value="11">Servidores</option>
				<option value="12">Soporte Técnico</option>
				<option value="13">Telefonía</option>
				
				<option value="1">Otro</option>
				
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class=" control-label col-md-3 col-sm-3 col-xs-12" for="area">Servicio <span class="required">*</span>
                                    </label>
                                    <div class="col-md-6 col-sm-6 col-xs-12">
                                        <div id="select2lista">
                                            
                                        </div>
                                    </div>
                                    </div>
                                   <!-- <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Área:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="<php echo $name_kind ?>">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Servicio:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="<php echo $servicio ?>">
                                        </div>
                                    </div>-->
                                 
                                    
                                <?php }else{ 
                                    $kinds=mysqli_query($con, "select * from kind where id=$kind_id");
                                    if ($row=mysqli_fetch_array($kinds)) {
                                        $name_kind=$row['name'];
                                    }
                                ?>
                                <div class="form-group" style="display:none;">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >ID Ticket:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input name="id_update" id="id_update" type="text" class="form-control col-md-7 col-xs-12" value="<?php echo $id_ticket ?>" readonly>
                                        </div>
                                    </div>
                                <div class="form-group">
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Descripción:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <textarea style="resize: none;" class="form-control col-md-7 col-xs-12" readonly><?php echo  $comment ?></textarea>
                                        </div>
                                    </div>
                                    <label class=" control-label col-md-3 col-sm-3 col-xs-12" for="kind_id">Área <span class="required">*</span>
                                    </label>
                                    <div class="col-md-6 col-sm-6 col-xs-12">
                                        <select class="chosen-select form-control" name="kind_id"  id="kind_id" required data-placeholder="-- Seleccionar --" autocomplete="off">
                                             
			
				<option value="2">CCTV</option>
				<option value="3">Celulares</option>
				<option value="4">Compras IT</option>
				<option value="5">CONTPAQ</option>
				<option value="6">HelpDesk</option>
				<option value="7">Office 365</option>
				<option value="8">Redes</option>
				<option value="9">SAI</option>
				<option value="10">SEINTRA</option>
				<option value="11">Servidores</option>
				<option value="12">Soporte Técnico</option>
				<option value="13">Telefonía</option>
				
				<option value="1">Otro</option>
				
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class=" control-label col-md-3 col-sm-3 col-xs-12" for="area">Servicio <span class="required">*</span>
                                    </label>
                                    <div class="col-md-6 col-sm-6 col-xs-12">
                                        <div id="select2lista">
                                            
                                        </div>
                                    </div>
                                    </div>
                                   <!-- <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Área:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="<php echo $name_kind ?>">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Servicio:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="<php echo $servicio ?>">
                                        </div>
                                    </div>-->
                                <?php } ?>
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Usuario:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="<?php echo $name ?>" readonly>
                                        </div>
                                    </div>
                                    <!--Añadir la extensión, correo, horario y teamviewer (ID y contraseña) -->
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Correo:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="<?php echo $email  ?>" readonly>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Extensi&oacute;n:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="<?php echo $phone  ?>" readonly>
                                        </div>
                                    </div>
                                     <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Teléfono:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="<?php echo $cell_phone  ?>" readonly>
                                        </div>
                                    </div>

                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Horario:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="<?php echo $contact_schedule  ?>" readonly>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >ID AnyDesk:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="<?php echo $id_team  ?>" readonly>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Contrase&ntilde;a:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="<?php echo $pass_team  ?>" readonly>
                                        </div>
                                    </div>

                                        

                                    <!--<div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Version y Modulo:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="<?php echo "2.0" ?>" readonly>
                                        </div>
                                    </div> 
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Descripción:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <textarea style="resize: none;" class="form-control col-md-7 col-xs-12" readonly><?php echo  $comment ?></textarea>
                                        </div>
                                    </div>
                                    <h2>ASIGNACIÓN DEL REQUEMIENTOS</h2>
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Tipo del requerimiento reportado:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                        <?php if($tipo_requerimiento==0){?>
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="" readonly>
                                        <?php }else{ ?> 
                                            <input type="text" class="form-control col-md-7 col-xs-12" value="<?php echo $name_require ?>" readonly>
                                        <?php } ?>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Analista designado:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                        <?php if($asigned_id==0){ ?>
                                            <input type="text"  class="form-control col-md-7 col-xs-12" value="Sin Asignar" readonly>
                                        <?php }else{ ?> 
                                            <input type="text"  class="form-control col-md-7 col-xs-12" value="<?php echo $name_user." ".$lastname_user ?>" readonly>
                                        <?php } ?>
                                        </div>
                                    </div>-->
                                     <h2>ATENCIÓN BRINDADA</h2>
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Tipo de atención:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <input type="text"  class="form-control col-md-7 col-xs-12" value="<?php echo "SOPORTE" ?>" readonly>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Detalle de la atención:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                         <?php if($finalizado==""){?>
                                            <textarea style="resize: none;" class="form-control col-md-7 col-xs-12" readonly><?php echo "EN ESPERA..." ?></textarea>
                                        <?php
                                        }else{
                                        ?> 
                                            <textarea style="resize: none "style="color: red" class="alert alert-warning form-control col-md-7 col-xs-12" readonly><?php echo $finalizado ?></textarea>
                                        <?php
                                        }
                                        ?>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-3 col-sm-3 col-xs-12" >Descargar adjunto:
                                        </label>
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <!-- <input type="text"  class="form-control col-md-7 col-xs-12" value="<?php echo $image ?>" readonly> -->
                                           <!--  <p class="text-muted">Descargar</p> -->
                                           <?php if($image!=""){?>
                                            <a target="_blank" class="form-control col-md-7 col-xs-12" href="downloadfile.php?id=<?php echo $id_ticket ?>"><i class="fa fa-download"></i> <?php echo " ".$image ?></a>
                                            <?php }else{?>
                                                <input type="text"  class="form-control col-md-7 col-xs-12" value="<?php echo "Sin archivo adjunto" ?>" readonly>
                                            <?php }?>
                                        </div>
                                    </div>
                               </div>
                             <!-- /.box body -->
                            <div class="box-footer" style="text-align: center;">
                                <div class="form-group">
                                    <div>
                                        <input type="submit" class="btn btn-success btn-submit" name="token" value="Guardar">
                                        <a href="registrados.php" class="btn btn-default btn-reset">Cancelar</a>
                                    </div>
                                </div>
                            </div><!-- /.box footer -->
                        </form>
                    </div><!-- /.box -->
                </div><!--/.col -->
            </div>   
             <?php if($is_admin!=1){?>
            
            <div style="text-align: center;">
                <p>Si necesitas pausar un ticket comunícate con Pamela o Eduardo.</p>
                <a class="btn btn-success" href="atender_ticket.php?id=<?php echo $id_ticket ?>"><i class="fa fa-check"></i> Finalizar Ticket</a>
                
                <!-- /.row
                <a class="btn btn-danger" href="incidencias.php?id=<?php echo $id_ticket ?>"><i class="fa fa-ban"></i>Pausar Ticket</a> -->
            </div>
            <?php } ?>
           
        </section><!-- /.content -->
    </div>

  <?php include "footer.php" ?>
<script
	src="https://code.jquery.com/jquery-3.3.1.min.js"
	integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
	crossorigin="anonymous">
    
</script>
<script type="text/javascript">
	$(document).ready(function(){
		$('#kind_id').val(2);
		recargarLista();

		$('#kind_id').change(function(){
			recargarLista();
		});
	})
</script>
<script type="text/javascript">
	function recargarLista(){
		$.ajax({
			type:"POST",
			url:"datos.php",
			data:"continente=" + $('#kind_id').val(),
			success:function(r){
				$('#select2lista').html(r);
			}
		});
	}
</script>
