<?php
// session_start();
include '../Auth.php';
include '../../class/ClassForm.php';
$type = isset($_POST['inv_type']) ? $_POST['inv_type'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$C_name = isset($_POST['C_name']) ? $_POST['C_name'] : '';
$C_Addr1 = isset($_POST['C_Addr1']) ? $_POST['C_Addr1'] : '';
$C_Addr2 = isset($_POST['C_Addr2']) ? $_POST['C_Addr2'] : '';
$C_Addr3 = isset($_POST['C_Addr3']) ? $_POST['C_Addr3'] : '';
$C_Addr4 = isset($_POST['C_Addr4']) ? $_POST['C_Addr4'] : '';
$C_gstin = isset($_POST['C_gstin']) ? $_POST['C_gstin'] : '';
$C_pan = isset($_POST['C_pan']) ? $_POST['C_pan'] : '';
$C_footer = isset($_POST['C_footer']) ? $_POST['C_footer'] : '';
$C_term = isset($_POST['C_term']) ? $_POST['C_term'] : '';
$C_extra = isset($_POST['C_extra']) ? $_POST['C_extra'] : ''; // Payment Details
$ImpExpCode = isset($_POST['ImpExpCode']) ? $_POST['ImpExpCode'] : '';
$ord_desc = isset($_POST['ord_desc']) ? $_POST['ord_desc'] : '';
$top_desc1 = isset($_POST['top_desc1']) ? $_POST['top_desc1'] : '';
$top_desc2 = isset($_POST['top_desc2']) ? $_POST['top_desc2'] : '';

if($C_name != '' && $C_gstin !='' && $C_pan != ''){

    if (isset($_FILES['logoFile'])){
        $entry = array(
            id => 0,      
            type => $type,  
            t_name => $name,
            c_name => $C_name,
            c_addr1 => $C_Addr1,
            c_addr2 => $C_Addr2,
            c_addr3 => $C_Addr3,
            c_addr4 => $C_Addr4,
            c_gstin => $C_gstin,
            c_pan => $C_pan,
            t_footer => $C_footer,
            t_terms => $C_term,                 // terms & conditions
            t_pamtment => $C_extra,             // Payment Details
            t_InpExpCode => $ImpExpCode,
            t_contract => $ord_desc,
            t_topDesc1 => $top_desc1,
            t_topDesc2 => $top_desc2,
            t_logo => '',
            t_extra1 => '',              // Payment Detalis
            t_extra2 => '',
            client_id => $_SESSION['user_id'], // Client id            
            status => 1                // 0 -> inactive, 1 -> Active 
        );
        $result = $ClassForm->f_mysql_insert_array('Einv_template',$entry); 
        if(!$result['mysqli_error']){
            $Inserted_id = $result['mysqli_insert_id'];

            $info = pathinfo($_FILES['logoFile']['name']);
            $ext = $info['extension']; // get the extension of the file
            $newname = $name.'.'.$ext; 
    
            // $target = 'logo/'.$_SESSION['user_id'].'/'.$newname;
            $target = 'logo/'.$_SESSION['user_id']; //.'/'.$newname;;
            if (!file_exists($target)) {
                mkdir($target, 0777, true);
            }
            $target = $target.'/'.$newname;            
            if(move_uploaded_file( $_FILES['logoFile']['tmp_name'],$target)){

                $updateEntry = array(
                    logo => $target
                );
                $condition = " id = ".$Inserted_id;

                $status1 = $ClassForm->Function_Update_SQL($updateEntry,'Einv_template',$condition);
                

                $result = array(
                    error_code => '0',
                    error_derail => 'no error found',
                    success => true    
                );
                echo json_encode($result);
            }
            else{
                $result = array(
                    error_code => '3',
                    error_derail => 'unable to upload log file',
                    success => false    
                );
                echo json_encode($result);
            }
        }
        else{
            $result = array(
                error_code => '2',
                error_derail => 'unable to create template. Please contact Administrator',
                success => false    
            );
            echo json_encode($result);
        }
    }
    else{
        $result = array(
            error_code => '3',
            error_derail => 'unable to upload log file',
            success => false    
        );
        echo json_encode($result);
    }
    //$ClassForm->f_mysql_insert_array('dc_details', $entry);       
}
else{
    $result = array(
        error_code => '1',
        error_derail => 'data canot be null or empty',
        success => false    
    );
    echo json_encode($result);
}

?>