<?php
@session_start();
class Xan extends MX_Controller{
    public function __construct(){
        if(!isset($_SESSION["app.gxc_sesion_id"]) || $_SESSION["app.gxc_sesion_id"]==0){
            header('Location: '.base_url().'main/no_conectado');
        }
        parent::__construct();
        $this->load->model('maestras/xdo_model');
        $this->load->model('maestras/xhc_model');
        $this->load->model('movimientos/xpn_model');
        $this->load->model('movimientos/xca_model');
        $this->load->model('movimientos/xan_model');
        $this->load->model('movimientos/xado_model');
        $this->load->model('movimientos/xamo_model');
        $this->load->model('movimientos/xaen_model');
        $this->load->model('seguridad/xppo_model');
        date_default_timezone_set('America/Lima');
    }

    public function index(){ }
    public function listar(){ }

    public function pdiario(){
        $_SESSION['app.gxc_sesion_xado']    = array();
        $_SESSION['app.gxc_sesion_xamo']    = array();
        $_SESSION['app.gxc_sesion_xaen']    = array();
        if($this->xppo_model->ver($_SESSION['app.gxc_sesion_id'],9)){
            $titulo             = $_SESSION['app.gxc_sesion_titulo'];
            $data['titulo']     = 'PARTE DIARIO :. '.$titulo;
            $datac['titulo']    = $titulo;
            $data['xco']        = modules::run('maestras/xco/combo','','');
            $data['cabecera']   = $this->load->view('main/cabecera', $datac, TRUE);
            $datam['menu']      = 'xan_pd';
            $data['menu']       = $this->load->view('main/menu', $datam, TRUE);
            $data['pie']        = $this->load->view('main/pie', '', TRUE);
            $this->load->view('xan/pdiario', $data);
        }else{
            $titulo             = $_SESSION['app.gxc_sesion_titulo'];
            $data['titulo']     = $titulo;
            $datac['titulo']    = $titulo;
            $data['cabecera']   = $this->load->view('main/cabecera', $datac, TRUE);
            $data['menu']       = $this->load->view('main/menu', '', TRUE);
            $data['pie']        = $this->load->view('main/pie', '', TRUE);
            $this->load->view('main/sin_permiso', $data);
        }
    }

    public function listar_pdiario(){
        $html_c = '';
        $html_a = '';
        $xpl    = $_SESSION['app.gxc_sesion_id'];
        $xco    = $this->input->post('xco');
        $fecha  = $this->util->fecha_ingles($this->input->post('fecha'));
        $ds     = $this->xca_model->listar($fecha,$fecha,$xco,$xpl);
        
        foreach ($ds->result() as $row) {
            if($row->cit_atendido==0){
                $html_c .= '
                    <tr>
                        <td>'.str_pad($row->hc_dni, 8 ,'0', STR_PAD_LEFT).'</td>
                        <td>'.$row->hc_apenombres.'</td>
                        <td>
                            <a href="'.base_url('movimientos/xan/nuevo/'.$row->cit_id).'" class="btn btn-primary btn-xs">
                                <span class="glyphicon glyphicon-edit"></span> <b>Atender</b>
                            </a>
                        </td>
                    </tr>
                ';
            }else{
                $html_a .= '
                    <tr>
                        <td>'.str_pad($row->hc_dni, 8 ,'0', STR_PAD_LEFT).'</td>
                        <td>'.$row->hc_apenombres.'</td>
                        <td>
                            <a href="'.base_url('movimientos/xan/ver/'.$row->ate_id).'" class="btn btn-primary btn-xs">
                                <span class="glyphicon glyphicon-edit"></span> <b>Editar</b>
                            </a>
                        </td>
                        <td>
                            <a href="'.base_url('movimientos/xan/ireceta/'.$row->ate_id).'" target="_blank" class="btn btn-info btn-xs">
                                <span class="glyphicon glyphicon-print"></span> <b>Receta</b>
                            </a>
                        </td>
                        <td>
                            <a href="'.base_url('movimientos/xan/ipdiario1/'.$row->ate_id).'" target="_blank" class="btn btn-info btn-xs">
                                <span class="glyphicon glyphicon-print"></span> <b>Imprimir</b>
                            </a>
                        </td>
                        <td>
                            <button type="button" class="btn btn-danger btn-xs" 
                                onclick="fn_iniciar_proceso_firma(\''.str_pad($row->hc_dni, 8 ,'0', STR_PAD_LEFT).'\', \''.$row->hc_apenombres.'\', '.$row->ate_id.')">
                                <span class="glyphicon glyphicon-pencil"></span> <b>Firmar</b>
                            </button>
                        </td>
                    </tr>
                ';
            }
        }
        echo json_encode(array($html_c, $html_a));
    }

    // ========================================================
    // FUNCIÓN DATOS PDF
    // ========================================================
    public function obtener_datos_pdf(){
        error_reporting(0);
        
        $ate_id = $this->input->post('ate_id');
        
        if(!$ate_id){
            echo json_encode(array('status'=>'error', 'msg'=>'ID no recibido'));
            return;
        }

        $ds = $this->xan_model->ver($ate_id);
        
        if(empty($ds)){
            echo json_encode(array('status'=>'error', 'msg'=>'Atención no encontrada'));
            return;
        }
        
        // Recetas (UTF-8 Encode)
        $ds_xamo = $this->xamo_model->listar($ate_id);
        $recetas = array();
        if($ds_xamo){
            foreach ($ds_xamo->result() as $row) {
                $recetas[] = array(
                    'med' => utf8_encode($row->med_descripcion . ' (Cant: ' . $row->amed_cantidad . ')'),
                    'ind' => utf8_encode($row->amed_indicacion)
                );
            }
        }

        // Diagnósticos
        $ds_xado = $this->xado_model->listar($ate_id);
        $diagnosticos = "";
        if($ds_xado){
            foreach ($ds_xado->result() as $rowdx) {
                $diagnosticos .= utf8_encode($rowdx->diag_codigo . ' - ' . $rowdx->diag_descripcion) . "\n";
            }
        }

        $response = array(
            'status'    => 'ok',
            'hc'        => str_pad($ds->hc_id, 6, '0', STR_PAD_LEFT),
            'paciente'  => utf8_encode($ds->hc_apenombres),
            'fecha'     => $this->util->fecha_espanol($ds->ate_fecha),
            'edad'      => $ds->ate_edad,
            'peso'      => $ds->ate_peso,
            'talla'     => $ds->ate_talla,
            'imc'       => $ds->ate_imc,
            'p_abd'     => $ds->ate_pabdominal,
            'anamnesis' => utf8_encode($ds->ate_anamnesis),
            'fisico'    => utf8_encode($ds->ate_fisico),
            'diag'      => $diagnosticos,
            'recetas'   => $recetas
        );

        header('Content-Type: application/json');
        echo json_encode($response);
    }

    public function nuevo($xca=0){
        $_SESSION['app.gxc_sesion_xado']    = array();
        $_SESSION['app.gxc_sesion_xamo']    = array();
        $_SESSION['app.gxc_sesion_xaen']    = array();
        if($xca>0){
            $ds                 = $this->xca_model->ver($xca);
            $ds_ult             = $this->xan_model->listar_utl_xhc($ds->hc_id);
            if($ds_ult){
                $ds_xamo            = $this->xamo_model->listar($ds_ult->ate_id);
                foreach ($ds_xamo->result() as $row) {
                    $_SESSION['app.gxc_sesion_xamo'][$row->med_id]  = array('cod' => $row->med_id, 'desc' => $row->med_descripcion, 'cant' => $row->amed_cantidad, 'indi' => $row->amed_indicacion);
                }
            }
            $titulo             = $_SESSION['app.gxc_sesion_titulo'];
            $data['titulo']     = $titulo;
            $datac['titulo']    = $titulo;
            $data['xco']        = modules::run('maestras/xco/combo','','');
            $data['xen']        = modules::run('maestras/xen/combo','0',' - BUSCAR EXÁMEN - ');
            $data['cabecera']   = $this->load->view('main/cabecera', $datac, TRUE);
            $datam['menu']      = 'xan_pd';
            $data['menu']       = $this->load->view('main/menu', $datam, TRUE);
            $data['pie']        = $this->load->view('main/pie', '', TRUE);
            $data['ds']         = $ds;
            $this->load->view('xan/nuevo', $data);
        }else
            header('Location: '.base_url('movimientos/xan/pdiario'));
    }

    public function insertar(){
        $xhc            = $this->input->post('txt_xhc');
        $xca            = $this->input->post('txt_xca');
        $xpn            = $this->input->post('txt_xpn');
        $fecha          = $this->util->fecha_ingles($this->input->post('txt_fecha'));
        $horai          = $this->input->post('txt_horai');
        $edad           = $this->input->post('txt_edad');
        $peso           = $this->input->post('txt_peso');
        $talla          = $this->input->post('txt_talla');
        $imc            = $this->input->post('txt_imc');
        $pabdominal     = $this->input->post('txt_pabdominal');
        $anamnesis      = mb_convert_case(trim($this->input->post('txt_anamnesis')), MB_CASE_UPPER, 'utf-8');
        $efisico        = mb_convert_case(trim($this->input->post('txt_efisico')), MB_CASE_UPPER, 'utf-8');
        $data           = array('ate_fecha'         => $fecha,
                                'ate_horai'         => $horai,
                                'ate_horaf'         => date('H:i:s'),
                                'ate_anamnesis'     => $anamnesis,
                                'ate_fisico'        => $efisico,
                                'ate_talla'         => $talla,
                                'ate_peso'          => $peso,
                                'ate_imc'           => $imc,
                                'ate_pabdominal'    => $pabdominal,
                                'ate_edad'          => $edad,
                                'cit_id'            => $xca,
                                'hc_id'             => $xhc,
                                'prog_id'           => $xpn);
        $id             = $this->xan_model->insertar($data);
        foreach ($_SESSION['app.gxc_sesion_xado'] as $row) {
            $this->xado_model->insertar($row['cod'], $id);
        }
        foreach ($_SESSION['app.gxc_sesion_xamo'] as $row) {
            $this->xamo_model->insertar($row['cod'], $id, $row['cant'], $row['indi']);
        }
        foreach ($_SESSION['app.gxc_sesion_xaen'] as $row) {
            $this->xaen_model->insertar($row['cod'], $id, $row['indi']);
        }
        $data_xca       = array('cit_atendido'  => 1);
        $this->xca_model->editar($xca, $data_xca);
        $data_xhc       = array('hc_peso'       => $peso,
                                'hc_talla'      => $talla,
                                'hc_imc'        => $imc,
                                'hc_pabdominal' => $pabdominal);
        $this->xhc_model->editar($xhc, $data_xhc);
        echo 0;
    }

    public function ver($id=''){
        if($id>0){
            $ds                 = $this->xan_model->ver($id);
            $titulo             = $_SESSION['app.gxc_sesion_titulo'];
            $data['titulo']     = $titulo;
            $datac['titulo']    = $titulo;
            $data['xco']        = modules::run('maestras/xco/combo','','');
            $data['xen']        = modules::run('maestras/xen/combo','0',' - BUSCAR EXÁMEN - ');
            $data['cabecera']   = $this->load->view('main/cabecera', $datac, TRUE);
            $datam['menu']      = 'xan_pd';
            $data['menu']       = $this->load->view('main/menu', $datam, TRUE);
            $data['pie']        = $this->load->view('main/pie', '', TRUE);
            $data['ds']         = $ds;
            $this->load->view('xan/ver', $data);
        }else
            header('Location: '.base_url('movimientos/xan/pdiario'));
    }

    public function editar(){
        $id             = $this->input->post('txt_id');
        $xhc            = $this->input->post('txt_xhc');
        $xca            = $this->input->post('txt_xca');
        $xpn            = $this->input->post('txt_xpn');
        $fecha          = $this->util->fecha_ingles($this->input->post('txt_fecha'));
        $horai          = $this->input->post('txt_horai');
        $edad           = $this->input->post('txt_edad');
        $peso           = $this->input->post('txt_peso');
        $talla          = $this->input->post('txt_talla');
        $imc            = $this->input->post('txt_imc');
        $pabdominal     = $this->input->post('txt_pabdominal');
        $anamnesis      = mb_convert_case(trim($this->input->post('txt_anamnesis')), MB_CASE_UPPER, 'utf-8');
        $efisico        = mb_convert_case(trim($this->input->post('txt_efisico')), MB_CASE_UPPER, 'utf-8');
        $data           = array('ate_fecha'         => $fecha,
                                'ate_horai'         => $horai,
                                'ate_horaf'         => date('H:i:s'),
                                'ate_anamnesis'     => $anamnesis,
                                'ate_fisico'        => $efisico,
                                'ate_talla'         => $talla,
                                'ate_peso'          => $peso,
                                'ate_imc'           => $imc,
                                'ate_pabdominal'    => $pabdominal,
                                'ate_edad'          => $edad,
                                'cit_id'            => $xca,
                                'hc_id'             => $xhc,
                                'prog_id'           => $xpn);
        $this->xan_model->editar($id, $data);
        $data_xhc       = array('hc_peso'       => $peso,
                                'hc_talla'      => $talla,
                                'hc_imc'        => $imc,
                                'hc_pabdominal' => $pabdominal);
        $this->xhc_model->editar($xhc, $data_xhc);
        echo 0;
    }

    public function listarh(){
        $xhc    = $this->input->post('d1');
        $op     = $this->input->post('d2');
        $html   = '';
        if($op==1){
            $ds = $this->xan_model->listarh1($xhc);
            foreach ($ds->result() as $row) {
                $html.= '
                    <tr>
                        <td>
                            <b>'.$this->util->fecha_espanol($row->ate_fecha).'</b>
                        </td>
                        <td>
                            <a href="'.base_url('movimientos/xan/ipdiario1/'.$row->ate_id).'" target="_blank">
                                <span class="glyphicon glyphicon-print"></span>
                                <b>Imprimir</b>
                            </a>
                        </td>
                    </tr>
                ';
            }
        }else{
            $ds = $this->xan_model->listarh2($xhc);
            foreach ($ds->result() as $row) {
                $html.= '
                    <tr>
                        <td>
                            <b>'.$this->util->fecha_espanol($row->fecha).'</b>
                        </td>
                        <td>
                            <a href="'.base_url('movimientos/xan/ipdiario2/'.$row->idatencion).'" target="_blank">
                                <span class="glyphicon glyphicon-print"></span>
                                <b>Imprimir</b>
                            </a>
                        </td>
                    </tr>
                ';
            }
        }
        $data['tabla']  = $html;
        return  $this->load->view('xan/histo', $data, FALSE);
    }

    public function ireceta($xan=0){
        if($xan>0){
            $ds                 = $this->xan_model->ver($xan);
            $ds_xamo            = $this->xamo_model->listar($xan);
            $html_xamo          = '';
            foreach ($ds_xamo->result() as $row) {
                $html_xamo.= '
                    <tr>
                        <td width="550">'.$row->med_descripcion.' | Cantidad: '.$row->amed_cantidad.'</td>
                        <td width="450">'.$row->amed_indicacion.'</td>
                    </tr>
                ';
            }
            $html               = '
                <table width=1000 style="font-size: 12px; margin-top: 175px;" border="0">
                    <tr>
                        <td>
                            <table style="margin-left: 15px;" border=1>
                                '.$html_xamo.'
                            </table>
                        </td>
                    </tr>
                </table>
                
            ';
            $data['titulo']     = 'RECETA: '.$ds->hc_apenombres;
            $data['html']       = $html;
            $this->load->view('xan/receta', $data);
        }else
            header('Location: '.base_url('movimientos/xan/pdiario'));
    }

    public function ipdiario1($xan=0){
        if($xan>0){
            $ds         = $this->xan_model->ver($xan);
            $ds_xado    = $this->xado_model->listar($xan);
            $ds_xamo    = $this->xamo_model->listar($xan);
            $ds_xaen    = $this->xaen_model->listar($xan);
            $html_xado  = '';
            $html_xamo  = '';
            $html_xaen  = '';
            foreach ($ds_xado->result() as $rowdx) {
                $html_xado.= '
                    <tr>
                        <td>
                            '.$rowdx->diag_codigo.' | '.$rowdx->diag_descripcion.'
                        </td>
                    </tr>
                ';
            }
            $html_xado  = ($html_xado!='')?'<table>'.$html_xado.'</table>':'';
            foreach ($ds_xamo->result() as $rowme) {
                $html_xamo.= '
                    <tr>
                        <td>
                            '.$rowme->med_descripcion.' <br/> Cantidad'.$rowme->amed_cantidad.'
                        </td>
                        <td>
                            '.$rowme->amed_indicacion.'
                        </td>
                    </tr>
                ';
            }
            $html_xamo  = ($html_xamo!='')?'<table width=100% border=1 cellpadding=3 cellspacing=0><tr><td align=center>MEDICAMENTO</td><td align=center>INDICACIÓN</td></tr>'.$html_xamo.'</table>':'';
            $html_xaen  = ($html_xaen!='')?'<table border=1 cellpadding=3 cellspacing=0>'.$html_xaen.'</table>':'';
            $html   = '
                <table width=790>
                    <tr>
                        <td colspan=3>
                            <table>
                                <tr>
                                    <td colspan=2>
                                        <table>
                                            <tr>
                                                <td>
                                                    <img src="'.base_url('img/salud.png').'" width=75>
                                                </td>
                                                <td>
                                                    CONSULTORIO VISIMAX
                                                    <br>
                                                    CALLE AGUA MARINA Mz.H LOTE 28
                                                    <br>
                                                    Urb. LOS CEDROS TRUJILLO
                                                    <br>
                                                    vigoayastah28@yahoo.es
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td align="center" colspan=3><h3>PARTE DIARIO</h3></td>
                    </tr>
                    <tr>
                        <td>FECHA: <b>'.$this->util->fecha_espanol($ds->ate_fecha).'</b></td>
                        <td>H/C: <b>'.str_pad($ds->hc_id, 6, '0', STR_PAD_LEFT).'</b></td>
                        <td>APELLIDOS Y NOMBRES: <b>'.$ds->hc_apenombres.'</b></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td colspan=3>
                            <table border="1" width=100%>
                                <tr>
                                    <td align="center">EDAD</td>
                                    <td align="center">PESO</td>
                                    <td align="center">TALLA</td>
                                    <td align="center">I.M.C.</td>
                                    <td align="center">P. ABDOMINAL</td>
                                </tr>
                                <tr>
                                    <td align="center">'.$ds->ate_edad.'</td>
                                    <td align="center">'.$ds->ate_peso.'</td>
                                    <td align="center">'.$ds->ate_talla.'</td>
                                    <td align="center">'.$ds->ate_imc.'</td>
                                    <td align="center">'.$ds->ate_pabdominal.'</td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td colspan=3>
                            <b>ANAMNESIS</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=3>
                            <p>'.$ds->ate_anamnesis.'</p>
                        </td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td colspan=3>
                            <b>EXAMEN FÍSICO</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=3>
                            <p>'.$ds->ate_fisico.'</p>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=3>
                            <b>DIAGNÓSTICO</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=3>
                        '.$html_xado.'
                        </td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td colspan="3">
                            <b>RECETA</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=3>
                        '.$html_xamo.'
                        </td>
                    </tr>
                </table>
            ';
            $data['titulo']     = 'PARTE DIARIO - '.$this->util->fecha_espanol($ds->ate_fecha);
            $data['tabla']      = $html;
            $this->load->view('xan/ipdiario', $data, FALSE);
        }else
            header('Location: '.base_url('movimientos/xan/pdiario'));
    }

    public function ipdiario2($xan=0){
        if($xan>0){
            $ds     = $this->xan_model->verh($xan);
            $html   = '
                <table width=790>
                    <tr>
                        <td colspan=3>
                            <table>
                                <tr>
                                    <td colspan=2>
                                        <table>
                                            <tr>
                                                <td>
                                                    <img src="'.base_url('img/salud.png').'" width=75>
                                                </td>
                                                <td>
                                                    CONSULTORIO VISIMAX
                                                    <br>
                                                    CALLE AGUA MARINA Mz.H LOTE 28
                                                    <br>
                                                    Urb. LOS CEDROS TRUJILLO
                                                    <br>
                                                    vigoayastah28@yahoo.es
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td align="center" colspan=3><h3>PARTE DIARIO</h3></td>
                    </tr>
                    <tr>
                        <td>FECHA: <b>'.$this->util->fecha_espanol($ds->fecha).'</b></td>
                        <td>H/C: <b>'.str_pad($ds->hc_id, 6, '0', STR_PAD_LEFT).'</b></td>
                        <td>APELLIDOS Y NOMBRES: <b>'.$ds->hc_apenombres.'</b></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td colspan=3>
                            <b>ANAMNESIS</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=3>
                            <p style="text-align: justify;">'.$ds->anamnesis.'</p>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=3>
                            <b>DIAGNÓSTICO</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=3>
                            <p style="text-align: justify;">'.$ds->diagnostico.'</p>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=3>
                            <b>RECETA</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=3>
                            <p style="text-align: justify;">'.$ds->receta.'</p>
                        </td>
                    </tr>
                </table>
            ';
            $data['titulo']     = 'PARTE DIARIO - '.$this->util->fecha_espanol($ds->fecha);
            $data['tabla']      = $html;
            $this->load->view('xan/ipdiario', $data, FALSE);
        }else
            header('Location: '.base_url('movimientos/xan/pdiario'));
    }

    public function todo($xhc=0){
        if($xhc>0){
            $ds     = $this->xan_model->listar_x_xhc($xhc);
            $html   = '';
            foreach ($ds->result() as $row) {
                $ds_xado    = $this->xado_model->listar($row->ate_id);
                $ds_xamo    = $this->xamo_model->listar($row->ate_id);
                $ds_xaen    = $this->xaen_model->listar($row->ate_id);
                $html_xado  = '';
                $html_xamo  = '';
                $html_xaen  = '';
                foreach ($ds_xado->result() as $rowdx) {
                    $html_xado.= '
                        <tr>
                            <td>
                                '.$rowdx->diag_cogido.' | '.$rowdx->diag_descripcion.'
                            </td>
                        </tr>
                    ';
                }
                $html_xado  = ($html_xado!='')?'<table>'.$html_xado.'</table>':'';

                foreach ($ds_xamo->result() as $rowme) {
                    $html_xamo.= '
                        <tr>
                            <td>
                                '.$rowme->med_descripcion.' <br/> Cantidad'.$rowme->amed_cantidad.'
                            </td>
                            <td>
                                '.$rowme->amed_indicacion.'
                            </td>
                        </tr>
                    ';
                }
                $html_xamo  = ($html_xamo!='')?'<table width=100% border=1 cellpadding=3 cellspacing=0><tr><td align=center>MEDICAMENTO</td><td align=center>INDICACIÓN</td></tr>'.$html_xamo.'</table>':'';
                $html_xaen  = ($html_xaen!='')?'<table border=1 cellpadding=3 cellspacing=0>'.$html_xaen.'</table>':'';
                $html.= '
                    <tr>
                        <td colspan=2 align=center>
                            <h2>'.$row->hc_apenombres.'</h2>
                        </td>
                    </tr>
                    <tr>
                        <td align=center>Fecha de Atención: '.$this->util->fecha_espanol($row->ate_fecha).'</td>
                    </tr>
                    <tr>
                        <td colspan=2>
                            <table width=100% border=1 cellspacing=0 cellpadding=3>
                                <tr>
                                    <td align=center>EDAD</td>
                                    <td align=center>PESO</td>
                                    <td align=center>TALLA</td>
                                    <td align=center>IMC</td>
                                </tr>
                                <tr>
                                    <td align=center>'.$row->ate_edad.'</td>
                                    <td align=center>'.$row->ate_peso.'</td>
                                    <td align=center>'.$row->ate_talla.'</td>
                                    <td align=center>'.$row->ate_imc.'</td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <b>ANAMNESIS</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <p style="text-align: justify;">'.$row->ate_anamnesis.'</p>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <b>EXAMEN FÍSICO</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <p style="text-align: justify;">'.$row->ate_fisico.'</p>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <b>DIAGNÓSTICOS</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=2>
                        '.$html_xado.'
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <b>RECETA</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=2>
                        '.$html_xamo.'
                        </td>
                    </tr>
                ';
            }
            $ds     = $this->xan_model->listar_x_xhc_h($xhc);
            foreach ($ds->result() as $row) {
                $html.= '
                    <tr>
                        <td colspan=2 align=center>
                            <h2>'.$this->util->fecha_espanol($row->fecha).'</h2>
                        </td>
                    </tr>
                    <tr>
                        <td>H/C: <b>'.str_pad($row->hc_id, 6, '0', STR_PAD_LEFT).'</b></td>
                        <td>APELLIDOS Y NOMBRES: <b>'.$row->hc_apenombres.'</b></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td colspan=2>
                            <b>ANAMNESIS</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=2>
                            <p style="text-align: justify;">'.$row->anamnesis.'</p>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=2>
                            <b>DIAGNÓSTICO</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=2>
                            <p style="text-align: justify;">'.$row->diagnostico.'</p>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=2>
                            <b>RECETA</b>
                        </td>
                    </tr>
                    <tr>
                        <td colspan=2>
                            <p style="text-align: justify;">'.$row->receta.'</p>
                        </td>
                    </tr>
                ';
            }
            //echo '<table width=790>'.$html.'</table>';
            $data['titulo']     = 'HISTORIA CLÍNICA - ATENCIONES';
            $data['tabla']      = $html;
            $this->load->view('xan/histo_all', $data, FALSE);
        }else
            header('Location: '.base_url('maestras/xhc'));
    }
}
?>