<?php 
    include 'inc/header.php'; 
    include("functions.php");
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);
    $update = false;
    $branch = "";
    if(isset($_GET['branch_id'])){
      $update = true;
      $branch = get_single_branch($_GET['branch_id']);
    }


    $imgLink = "";
    $required = "";

    // $company_data = get_company_data();
    if($update == true){
      $location = "../".$branch['location'];
      $img = explode(',', $branch['logo']);
      $imgLink = 'feedback/'.$location.'/'.$img[0];
    }
?>

    <!--== BODY CONTNAINER ==-->
    <div class="container-fluid sb2">
        <div class="row">
            <?php include 'inc/sidebar.php';?>
            <div class="sb2-2">
                <div class="sb2-2-2">
                    <ul>
                        <li><a href="#"><i class="fa fa-home" aria-hidden="true"></i> Home</a>
                        </li>
                        <li class="active-bre"><a href="#"> Adding Branch</a>
                        </li>
                    </ul>
                </div>
                <div class="sb2-2-3">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="box-inn-sp">
                                <div class="inn-title">
                                    <h4>Add New Branch</h4>
                                </div>
                                <div class="tab-inn">
                                    <form id="branchForm" action="ajax-request.php" method="POST" enctype="multipart/form-data">
                                        <div class="row">
                                            <div class="input-field col s6">
                                                <input type="text" class="validate" id="name" name="name" placeholder="Name" value="<?php echo (($update === true) ? ($branch['name']) : ""); ?>" />
                                                <label for="name">Branch Name</label>
                                            </div>
                                        </div>

                                        <!-- logo -->
                                         <div class="row">
                                            <div class="input-field col s6">
                                            <?php
                                            if($update === true){
                                            ?>
                                                <img src="<?php echo (($update === true) ? ($imgLink) : ""); ?>" alt="Image"  width='100px'><br>
                                              <?php } ?>
                                                <input class="form-control" type="file" 
                                                  name="company_logo[]" id="company_logo" accept="image/gif, image/jpeg, image/png, image/jpg, image/webp" multiple >
                                                  <p class="small mb-0 mt-2">Leave Blank to stay same images</p>
                                                  <p class="small mb-0 mt-2"><b>Note:</b> Only JPG, JPEG, and PNG. Our suggested dimensions are 600px * 450px. The larger image will be cropped to 4:3 to fit our thumbnails/previews.
                                                  </p>
                                            </div>
                                        </div>

                                        <!-- Info -->
                                         <div class="row">
                                            <div class="input-field col s6">
                                                <input type="text" class="validate" id="title" name="title" placeholder="Name" value="<?php echo (($update === true) ? ($branch['title']) : ""); ?>" />
                                                <label for="title">Title</label>
                                            </div>
                                        </div>
                                         <div class="row">
                                            <div class="input-field col s6">
                                                <input type="text" class="validate" id="about" name="about" placeholder="Name" value="<?php echo (($update === true) ? ($branch['about']) : ""); ?>" />
                                                <label for="about">About</label>
                                            </div>
                                        </div>


                                        <!-- Feedbacks -->
                                         <div class="row">
                                            <div class="input-field col s6">
                                                <input type="text" class="validate" id="feed_1" name="feed_1" placeholder="Name" value="<?php echo (($update === true) ? ($branch['feed_1']) : ""); ?>" />
                                                <label for="feed_1">Feedback 1 Label</label>
                                            </div>
                                        </div>

                                         <div class="row">
                                            <div class="input-field col s6">
                                                <input type="text" class="validate" id="feed_2" name="feed_2" placeholder="Name" value="<?php echo (($update === true) ? ($branch['feed_2']) : ""); ?>" />
                                                <label for="feed_2">Feedback 2 Label</label>
                                            </div>
                                        </div>

                                         <div class="row">
                                            <div class="input-field col s6">
                                                <input type="text" class="validate" id="feed_3" name="feed_3" placeholder="Name" value="<?php echo (($update === true) ? ($branch['feed_3']) : ""); ?>" />
                                                <label for="feed_3">Feedback 3 Label</label>
                                            </div>
                                        </div>

                                         <div class="row">
                                            <div class="input-field col s6">
                                                <input type="text" class="validate" id="feed_4" name="feed_4" placeholder="Name" value="<?php echo (($update === true) ? ($branch['feed_4']) : ""); ?>" />
                                                <label for="feed_4">Feedback 4 Label</label>
                                            </div>
                                        </div>

                                         <div class="row">
                                            <div class="input-field col s6">
                                                <input type="text" class="validate" id="feed_5" name="feed_5" placeholder="Name" value="<?php echo (($update === true) ? ($branch['feed_5']) : ""); ?>" />
                                                <label for="feed_5">Feedback 5 Label</label>
                                            </div>
                                        </div>
                                        <!-- Feedbacks -->

                                        
                                        <div class="row">
                                            <div class="input-field col s12">
                                          <?php if($update == false){ ?>
                                                <input type="submit" class="waves-effect waves-light btn-large" value="Add Now">
                                          <?php }else{?>
                                                <input type="submit" class="waves-effect waves-light btn-large" value="Update Now">
                                          <?php }?>
                                            </div>
                                        </div>
                                        
                                        <?php
                                            if($update == false){
                                        ?>
                                           <input type="hidden" name="branch_id" id="branch_id" value="">
                                           <input type="hidden" name="add-branch-request" id="add-branch-request" value="1">
                                        <?php
                                        }else{?>
                                           
                                           <input type="hidden" name="branch_id" id="branch_id" value="<?=$_GET['branch_id'];?>">
                                           <input type="hidden" name="admin-update-branch-request" id="admin-update-branch-request" value="1">
                                        <?php }
                                        ?>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php include 'inc/footer.php';?>
<script type="text/javascript">
   $(document).ready(function() {

  $("#branchForm").on('submit', (function(e) {

    console.log('yes clicked form');
   e.preventDefault();
    $.ajax({
      url: $(this).attr('action'),
      type: "POST",
      data: new FormData(this),
      dataType: "json",
      contentType: false,
      cache: false,
      processData: false,

      success: function(data) {
        console.log('add-branch response:', data);
        if(data && data.success == 1){
            promptMsg(1,data.msg || 'Branch added.');
            window.location.href = "all-branches.php";
        } else {
            promptMsg(0, data && data.msg ? data.msg : 'Unable to add branch.');
        }
      },
      error: function(e) {
        console.log('add-branch error:', e.responseText || e);
        promptMsg(0, 'Server error while adding branch.');
      }
    });

  }));

  });

</script>