<?php
include 'connect/cms-config.php' ;
include 'requires/function.php' ;
include 'requires/session.php' ;

// include the class
include 'requires/class_resize.php' ;

// keep parameter in value
$page 				= escapeString($_GET['page']) ;
$page_mode 			= escapeString($_GET['page_mode']) ;
$type 				= escapeString($_GET['type']) ;
$search 			= escapeString($_GET['search']) ;

// active menu bar
$active_main_menu 	= 'setting' ;
$active_sub_menu 	= 'setting-app' ;
$active_menu 		= 'app-pop-up' ;

// check permission
if ( !permissionCheck($row_user, 'app-pop-up-view') ){
	header('Location: index.php') ;
	exit ;
}

// get all branch
$branch_all     = [] ;
$get_branch = $mysqli->query( "SELECT * FROM branch
                               WHERE deleted_at IS NULL " . $user_branch_permission_sql_123 ) ;
if ( $get_branch->num_rows > 0 ){
    while ( $row_branch = $get_branch->fetch_assoc() ){
        $branch_all[$row_branch['branch_id']] = $row_branch['branch_name'] ;
    }
}

// check query exsits
$submit_type = 'new' ;
$mysqli_page = $mysqli->query("SELECT * FROM setting_popup
							   WHERE setting_popup_id = '1' LIMIT 1");
if ($mysqli_page->num_rows > 0){
	// keep query value in array
	$row_page    = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
	$submit_type = 'edit' ;
}


// update database
if ( isset($type) && ( $type == 'new' || $type == 'edit' ) && $_POST['hide'] == 1 ){


	if ( $page == '' ){
		$mysqli->query( "INSERT INTO setting_popup 
					    ( created_at ) VALUES 
					    ( '".TODAYDATE."' )" ) ;
		$page = $mysqli->insert_id ;	
	}

	// resize image
    // set image in variable
	$image 		  = $_FILES["image"]["name"] ;
	$image_query  = '' ;
	$remove_photo = $_POST['remove_photo'] ;
	if ($remove_photo == 1){
		$image = '' ;	
		$image_query = "file = ''," ;
	}

	if ( $image != '' ){
		$get_image = pathinfo($image) ;

		$create_image = reCreateImage('Pop-up', $page, $page, '', $image, $_FILES["image"]["type"], $_FILES['image']['tmp_name']) ;
		// Image uploads when exists
		if ($create_image['result'] && is_array($create_image['crop']) && count($create_image['result']) > 0){
			$resizeObj = new resize($create_image['original']) ; // Initialise load image
			foreach($create_image['crop'] as $value){
				// Resize image (options: exact, portrait, landscape, auto, crop)
				$resizeObj -> resizeImage($value['width'], $value['height'], $value['type']) ;
				$resizeObj -> saveImage($value['source'], 70) ; // Save image	
			}
			$get_image   = pathinfo($create_image['image']) ;
			$image_query = "file = '".$create_image['image']."'," ;
		}
	}


	$branch 			 = $_POST['branch'] ;
	$array_branch 		 = [] ;
	foreach ( $branch as $k_branch => $v_branch ){
		$array_branch[]  = escapeString( $v_branch ) ;
	}

	// update database
	$mysqli->query( "UPDATE setting_popup SET
					".$image_query."
					branch 					= '/".implode('/', $array_branch)."/',
					title_name   			= '".escapeString($_POST['title_name'])."',
					status   			    = '".escapeString($_POST['status'])."'
					WHERE setting_popup_id  = '1'" ) ;

	// refresh page
	header("Location:app-pop-up.php?page_mode=edit&page=1&success=1") ;
	$_SESSION['system_result'] = 'success-updated' ;
	exit ;
}


// start header here
include 'requires/page_header.php';
include 'requires/page_top.php';

?>


<div class="warper container-fluid">
<div class='container' style="background-color: white; border-radius: 10px;">
    <div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">
    <h1>Pop Up <small><?= $lang[$page_mode] ?></small></h1></div>
	<?php
    if ($_SESSION['system_result'] != ''){
        switch($_SESSION['system_result']){
            case 'success-updated' :
                echo '<div class="result_success">'.$lang['Thank you details has been updated'].'</div>' ;	
            break ;
        }
        unset($_SESSION['system_result']) ;
    }
    ?>
    <div class="panel panel-default">
        <div class="panel-heading"></div>
        <div class="panel-body">
            <form method="post" class="form-horizontal" id="quotationForm" enctype="multipart/form-data" action="app-pop-up.php?page_mode=edit&page=1&type=edit" novalidate="novalidate">

            	<div class="form-group show_branch_action">
                    <div class="col-sm-2 control-label"><?= $lang['branch']?></div>
                    <div class="col-sm-9">
                        <select name="branch[]" class="form-control ui-search-input chosen-select select2-basic-single" multiple required>
                        	<?php
                    		foreach ( $branch_all as $key => $value ) {
                    			echo '<option value="'.$key.'" '. ( strpos( $row_page['branch'], '/'.$key.'/' ) !== false ? 'selected' : '' ) .' >'.$value.'</option>';
                    		}
                        	?>
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-2 control-label">Title</div>
                    <div class="col-sm-9">
                        <input type="text" name="title_name" class="form-control ui-search-input" value="<?= dataFilter($row_page['title_name']) ?>" placeholder="Title" />
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-2 control-label"><?= $lang['photo']?></div>
                    <div class="col-sm-9">
                        <div class="file_upload">
                            <div class="file_form">
                                <input type="hidden" name="hide_image" value="<?= dataFilter($row_page['file']) ?>" />
                                <input type="hidden" name="remove_photo" value="">
                                <input type="file" name="image" class="file_button control-label" <?= $input_block ?> />
                            </div>
                        </div>
                        <font color="red">* 800px x 1000px</font>
                    </div>
                </div>
                <?php if ($row_page['file'] != ''){ ?>
                    <div class="form-group form-group-pdf">
                        <div class="col-sm-2 control-label"><?= $lang['preview']?></div>
                        <div class="col-sm-9">
                        	<label class="remove_photo"><input type="checkbox" name="remove_photo" class="ui-checkbox tick" value="1">&nbsp;<?= $lang['Remove File']?></label>
                        	<a href="<?= PATH.'uploads/Pop-up/'.dataFilter($row_page['file']) ?>" target="_blank">
                        		<img src="<?= PATH.'uploads/Pop-up/'.dataFilter($row_page['file']) ?>" style="width:100px;" alt="no image"/>
                        	</a>
                        </div>
                    </div>
                <?php }else{ ?>
                    <input type="hidden" name="remove_photo" value="" />
                <?php } ?>


                <div class="form-group">
                    <div class="col-sm-2 control-label">Status</div>
                    <div class="col-sm-9">
                        <select name="status" id="status" class="form-control ui-search-input">
                        	<option value="active"   <?= ( $row_page['status'] == 'active' ? 'selected' : '' ) ?> >Active</option>
                        	<option value="inactive" <?= ( $row_page['status'] == 'inactive' ? 'selected' : '' ) ?> >Inactive</option>
						</select>
                    </div>
                </div>
                <?php if (  $page_mode == 'new'  || ( $page_mode == 'edit' && permissionCheck($row_user, 'app-pop-up-edit') ) ) { ?>
                <div class="form-group">
                    <div class="col-sm-2 control-label"></div>
                    <div class="col-sm-9">
                        <button type="submit" class="btn" style="color:white;background-color: #5e5bd0;float:right; margin-top: 5px;width: 100px;"><?= $lang['submit']?></button>
                        <input type="hidden" name="hide" value="1">
                        <input type="hidden" name="page_status" value="<?= $submit_type ?>">
                    </div>
                </div>   
                <?php } ?> 
            </form>
        </div>
    </div>
</div>
</div>