<?php
global $wpdb;
if(isset($_POST['save_cs_list'])){
    $data = array();
    $list_data_arr = array();
    foreach($_POST['list_name'] as $key => $value){
        $list_data_arr[$key]['list_name'] = $value;
    }
    foreach($_POST['num_addr'] as $key => $value){
        $list_data_arr[$key]['num_addr'] = $value;
    }
    foreach($_POST['extra_charges'] as $key => $value){
        $list_data_arr[$key]['extra_charges'] = $value;
    }
    foreach($_POST['upload-file-id'] as $key => $value){
        $list_data_arr[$key]['upload-file-id'] = $value;
    }

    $pcl_customers = $wpdb->prefix . "pcl_customers";
    $list_customer_id = $_POST['customer_name'];
    $is_already = $wpdb->get_row($wpdb->prepare( "SELECT * FROM $pcl_customers WHERE `customer_id` = $list_customer_id" ));
    $customer_id = $list_customer_id;
    if($is_already){
        $customer_id = $is_already->id;
    }else{
        $data = array('customer_id' => $list_customer_id, 'assignment' => 0);
        $wpdb->insert($pcl_customers, array('customer_id' => $list_customer_id, 'assignment' => 0));
        $customer_id = $wpdb->insert_id;
    }
    if($customer_id){
        foreach($list_data_arr as $list_data){
            $pcl_customers_list = $wpdb->prefix . "pcl_customers_list";
            $data = array(
                'pcl_customers_id' => $customer_id, 
                'list_name' => $list_data['list_name'],
                'num_address' => $list_data['num_addr'],
                'extra_charges' => $list_data['extra_charges'],
                'file_url' => $list_data['upload-file-id'],
            );
            $wpdb->insert($pcl_customers_list,$data);
        }
        ?>
            <script>
                swal({
                    title: 'Added!',
                    text: 'Your customer list has been added.',
                    type: 'success'
                });
            </script>
        <?php
    }
}
?>
<div class="wrap pagetop">
    <h1 class="wp-heading-inline">Add Customer List</h1>
    <a href="admin.php?page=pcl_customers_list" class="page-title-action">Customer Lists</a>
    <hr class="wp-header-end">
    <form class="add_customer_form" name="add_customer_form" method="post">
        <div class="customer-form-group">
            <div class="customer-form-fields">
                <label for="customer_name">Customer Name</label>
                <select class="customer_name cs-form-field" required name="customer_name" id="customer_name">
                    <option value="">Select customer</option>
                    <?php
                        $alluser = get_users();
                        foreach($alluser as $cuser){
                            $cs_name = $cuser->display_name;
                            if($cuser->first_name){
                                $cs_name = $cuser->first_name.' '.$cuser->last_name;
                            }
                            echo '<option value="'.$cuser->ID.'">'.$cs_name.' ('.$cuser->user_email.')</option>';
                        }
                    ?>
                </select>
            </div>
            <div class="customer-list-repeater">
                <div class="customer-list-repeater-item">
                    <div class="customer-list-repeater-left">
                        <div class="customer-form-fields">
                            <label for="list_name">List Name</label>
                            <input type="text" required class="list_name cs-form-field" name="list_name[]" id="list_name" />
                        </div>
                        <div class="customer-form-fields">
                            <label for="num_addr">Number of Addresses</label>
                            <input type="number" required class="num_addr cs-form-field" name="num_addr[]" id="num_addr" min="0" />
                        </div>
                        <div class="customer-form-fields">
                            <label for="extra_charges">Extra Charges</label>
                            <input type="number" class="extra_charges cs-form-field" name="extra_charges[]" id="extra_charges" min="0" step="0.001" />
                        </div>
                        <div class="customer-form-fields">
                            <div class="upload-btn-wrapper">
                                <button class="upload_file">Upload File</button>
                                <input type="file" required name="upload_file" class="uploader_file cs-form-field" />
                                <input type="hidden" name="upload-file-id[]" class="upload-file-name" />
                            </div>
                            <div class="uploaded-file-view" style="display: none;">
                                <a href="javascript:void(0)" download><img src="<?=assets_url.'/images/file-icon.jpg'?>" /></a>
                            </div>
                        </div>
                    </div>
                    <div class="customer-list-repeater-right">
                        <div class="customer-list-repeater-plus">
                            <i class="fa fa-plus repeat-list"></i>
                        </div>
                    </div>
                </div>
            </div>
            <div class="customer-form-fields save-list">
                <input type="submit" name="save_cs_list" class="cs-form-field save_cs_list" id="save_cs_list" value="Save" />
            </div>
        </div>
    </form>
</div>
<style>
    .add_customer_form .select2-container{
        width: 300px !important;
    }
</style>
<script type="text/javascript">
    jQuery(document).ready(function($){
        $('.customer_name').select2();
    });
</script>