<?php
include_once (ROOT . FOLDER_CORE . '/auth.php'); 
if ($auth == 1) {
    $cli_user = $_SERVER["HTTP_CLIENT_ID"];
    if (isset($_POST["doc_fichero"])) {
        $coding_file = $_POST["doc_fichero"];
        if (isset($_POST["doc_name"])) {
            if (isset($_POST["doc_size"])) {
                include_once ('servAddDoc_uploadDoc.php');
                //VERIFICO SI CREA EXPEDIENTE AL CARGAR EL DOCUMENTO-----------
                if (isset($_POST["exp"]) AND $_POST["exp"] == 1) {
                    //VARIABLES------------------------------------------------
                    $cod_comercial = (isset($_POST["commercial"])) ? trim($_POST["commercial"]) : '0' ;
                    $exp_tipo = (isset($_POST["exp_type"])) ? trim($_POST["exp_type"]) : '0' ;
                    $exp_plantilla = (isset($_POST["exp_template"])) ? trim($_POST["exp_template"]) : '0' ;
                    if ( $cod_comercial !== '0' AND $exp_tipo !== '0' AND $exp_plantilla !== '0' ) {
                        define('TIPO_EVENTO', 'CRE_EXP');
                        define('ACTIVO', '1');
                        define('POS_EVENTO_CONCEPT', '1');
                        define('ALIAS_EVENTO_CONCEPT', 'COMERCIAL');
                        define('LISTA_EVENTO_CONCEPT', 'COMERCIALES'); 
                        $cod_reg = get_id_unico();
                        $tipo_exp = $exp_tipo;
                        $clave_doc = $clave;
                        $cod_plantilla = $exp_plantilla;
                        //CREO EL EVENTO=======================================
                        createEvento($bd_conn, $cod_reg, TIPO_EVENTO, ACTIVO, $tipo_exp, $clave_doc, $cod_plantilla);
                        //CREO EL EVENTO CONCEPTO==============================
                        createEventoConcepto($bd_conn, $cod_reg, POS_EVENTO_CONCEPT, ALIAS_EVENTO_CONCEPT, $cod_comercial, LISTA_EVENTO_CONCEPT);
                    }else {
                        $data[] = array ( 
                            "status" => "NOT OK",
                            "message" => "error : var is missing"
                        );
                    }
                }
            }else {
                $data[] = array ( 
                    "status" => "NOT OK",
                    "message" => "error : doc_size is missing"
                );
            }
        }else {
            $data[] = array ( 
                "status" => "NOT OK",
                "message" => "error : doc_name is missing"
            );
        }                       
    }elseif (isset($_POST["doc_fichero_json"])) {
        $file_json = json_decode($_POST["doc_fichero_json"]);
        if (isset($file_json[0]->file)) {
            $coding_file = $file_json[0]->file;
            if (isset($_POST["doc_name"])) {
                if (isset($_POST["doc_size"])) {
                    include_once ('servAddDoc_uploadDoc.php');
                }else {
                    $data[] = array ( 
                        "status" => "NOT OK",
                        "message" => "error : doc_size is missing"
                    );
                }
            }else {
                $data[] = array ( 
                    "status" => "NOT OK",
                    "message" => "error : doc_name is missing"
                );
            }
        }else {
            $data[] = array ( 
                "status" => $file_json[0]->file,
                "message" => "error : doc_fichero_json is missing"
            );
        }        
    }else {
        $data[] = array ( 
            "status" => "NOT OK",
            "message" => "error : doc_fichero is missing"
        );
    }
    header("Content-type: application/json; charset=utf-8");
    echo json_encode($data);
}elseif ($auth == 0) {
    $data = array();
    $data[] = array ( 
        "code" => "unauthorized_client",
        "message" => "Invalid authorization token"
    );
    header("Content-type: application/json; charset=utf-8");
    echo json_encode($data);
}
///////////////////////////////////////////////////////////////////////////////
//FUNCIONES////////////////////////////////////////////////////////////////////
function createEventoConcepto($bd, $cod_reg, $pos_evento, $alias, $cod_comercial, $lista)
{
	$sql =  " INSERT INTO CONFIG_EVENTOS_CONCEPTOS (EVENTO, POS, ALIAS, VALOR, LISTA) 
              VALUES ( ?, ?, ?, ?, ? )";
    $base = $bd->prepare($sql);
    $cod_reg = mysqli_real_escape_string($bd, $cod_reg);
    $pos_evento = mysqli_real_escape_string($bd, $pos_evento);
    $alias = mysqli_real_escape_string($bd, $alias);
    $cod_comercial = mysqli_real_escape_string($bd, $cod_comercial);
    $lista = mysqli_real_escape_string($bd, $lista);
    $base->bind_param('sisss', $cod_reg, $pos_evento, $alias, $cod_comercial, $lista);
    $base->execute();
}
function createEvento($bd, $cod_reg, $tipo_evento, $activo, $tipo_exp, $clave_doc, $cod_plantilla)
{
	$sql =  " INSERT INTO CONFIG_EVENTOS (CODIGO, TIPO, ACTIVO, PARAM1, PARAM2, PARAM3) 
              VALUES ( ?, ?, ?, ?, ?, ? )";
    $base = $bd->prepare($sql);
    $cod_reg = mysqli_real_escape_string($bd, $cod_reg);
    $tipo_evento = mysqli_real_escape_string($bd, $tipo_evento);
    $activo = mysqli_real_escape_string($bd, $activo);
    $tipo_exp = mysqli_real_escape_string($bd, $tipo_exp);
    $clave_doc = mysqli_real_escape_string($bd, $clave_doc);
    $cod_plantilla = mysqli_real_escape_string($bd, $cod_plantilla);
    $base->bind_param('ssisss', $cod_reg, $tipo_evento, $activo, $tipo_exp, $clave_doc, $cod_plantilla);
    $base->execute();
}
?>