	<?php $this->load->view("common/header"); ?>
	<link rel="stylesheet" type="text/css" href="thems/css/util.css">
	<link rel="stylesheet" type="text/css" href="thems/css/send_sms.css">
	<!-- end: HEAD -->
       
		<style type="text/css">
			.table > thead:first-child > tr:first-child > th, .table > thead:first-child > tr:first-child > td, .table-striped thead tr.primary:nth-child(2n+1) th {
			    background-color: #428bca;
			    border-color: #357ebd;
			    border-top: 1px solid #357ebd;
			    color: white;
			    text-align: center;
			}
            .input2{
                /*display: block;*/
                width: 6%;
                background: #e6e6e6;
                font-family: Montserrat-Bold;
                font-size: 15px;
                line-height: 1.5;
                color: #666666;
                height: 50px;
                border-radius: 25px;
                padding: 0 30px;
                vertical-align: -22px;
            }
		</style>
		<!-- end: CSS REQUIRED FOR THIS PAGE ONLY -->
	<!-- end: HEAD -->
	<!-- start: BODY -->
	<body>
		<!-- start: HEADER -->
		<div class="navbar navbar-inverse navbar-fixed-top">
			<!-- start: TOP NAVIGATION CONTAINER -->
			<div class="container">
				<div class="navbar-header">
					<!-- start: RESPONSIVE MENU TOGGLER -->
					<button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button">
						<span class="clip-list-2"></span>
					</button>
					<!-- end: RESPONSIVE MENU TOGGLER -->
					<!-- start: LOGO -->
					<?php $this->load->view("common/logo"); ?>
					<!-- end: LOGO -->
				</div>
				<div class="navbar-tools">
					<!-- start: TOP NAVIGATION MENU -->
				<?php //$this->load->view("common/notifications.php"); ?>
					<!-- end: TOP NAVIGATION MENU -->
				</div>
			</div>
			<!-- end: TOP NAVIGATION CONTAINER -->
		</div>
		<!-- end: HEADER -->
		<!-- start: MAIN CONTAINER -->
		<div class="main-container">
			<div class="navbar-content">
				<!-- start: SIDEBAR -->
				<?php $this->load->view("common/navigation"); ?>
				<!-- end: SIDEBAR -->
			</div>
			<!-- start: PAGE -->
			<div class="main-content">
				<!-- end: SPANEL CONFIGURATION MODAL FORM -->
				<!--<div class="container">-->
				<!--    <br>-->
				<!--    <div class="row">-->
				<!--        <div class="col-md-3">-->
				<!--            <div class="panel">-->
				<!--                <div class="panel-heading">-->
				<!--                    <label>Select Customer type:</label>-->
				<!--                </div>-->
				<!--                <div class="panel-body">-->
				<!--                    <div class="col-md-12">-->
				                    
				<!--                    <div class="row">-->
				<!--                        <div class="col-md-12">-->
				<!--                            Retail Customer :<input type="radio" name="cus_type" val="hw">-->
				<!--                            <br>-->
				<!--                            Destribution Customer:<input type="radio" name="cus_type" val="dst">-->
				<!--                        </div>-->
				<!--                    </div>-->
				<!--                </div>-->
				<!--                </div>-->
				                
				<!--            </div>-->
				<!--        </div>-->
				<!--    </div>-->
				<!--</div>-->
				<div class="contact1">
            		<div class="container-contact1">
            			<div class="contact1-pic js-tilt" data-tilt>
            				<img src="thems/images/img-01.png" alt="IMG">
            			</div>
            
            			<form class="contact1-form validate-form" id="sms_form">
            				<span class="contact1-form-title">
            					Get in touch
            				</span>
            
            				<div class="wrap-input1 validate-input" data-validate = "Name is required">
            					<span class="shadow-input2"></span>
            					Retail Customer : <input class="input2" type="radio" name="cus_type" value="hw">
                                <br>
                                Wholesale Customer : <input class="input2" type="radio" name="cus_type" value="dst">
                                <br>
                                Custom Number : <input class="input2" type="radio" name="cus_type" value="cus">
            				</div>
            
            				<!--<div class="wrap-input1 validate-input" data-validate = "Valid email is required: ex@abc.xyz">-->
            				<!--	<input class="input1" type="text" name="email" placeholder="Email">-->
            				<!--	<span class="shadow-input1"></span>-->
            				<!--</div>-->
            
            				<div class="wrap-input1 validate-input" data-validate = "Number">
            					<input class="input1" type="text" name="cus_number" placeholder="Number" id ="cus_number" value="" readonly>
            					<span class="shadow-input1"></span>
            				</div>
            
            				<div class="wrap-input1 validate-input" data-validate = "Message is required">
            					<textarea class="input1" name="message" placeholder="Message" id="msg"></textarea>
            					<span class="shadow-input1"></span>
            				</div>
            
            				<div class="container-contact1-form-btn">
            					<button class="contact1-form-btn">
            						<span>
            							Send SMS
            							<i class="fa fa-long-arrow-right" aria-hidden="true"></i>
            						</span>
            					</button>
            				</div>
            			</form>
            		</div>
            	</div>
                <hr>
                <table class="table table-striped" id="sent_table">
                    <thead>
                        <tr>
                            <th>Sent to</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td></td><td class=""></td>
                        </tr>
                    </tbody>
                </table>

			</div>
        </div>
		<!-- start ajax model -->
		<div id="ajax-modal" class="modal fade" data-backdrop="static" data-keyboard="false" tabindex="-1" style="display: none;"></div>
		<!-- end ajax model -->

		<!-- start: MAIN JAVASCRIPTS -->
		<?php $this->load->view("common/footer"); ?>
		<!-- end: MAIN JAVASCRIPTS -->
		<script type="text/javascript" src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
		<script type="text/javascript" src="https://cdn.datatables.net/1.10.10/js/dataTables.bootstrap.min.js"></script>
        <script>
        
<?php
$php_array1 = $hw_cus;
$js_array = json_encode($php_array1);
echo "var hw_cus = ". $js_array . ";\n";


$php_array2 = $ds_cus;
$js_array = json_encode($php_array2);
echo "var ds_cus = ". $js_array . ";\n";

?>

function begin_process(){
    var type = "";
    if($('input:radio[name="cus_type"]').is(':checked')){
        type = $('input:radio[name="cus_type"]:checked').val();
    }else{
        bootbox.alert("Please Select Customer Type!");
        return false;
    }
    if($('#msg').val().length < 1){
        bootbox.alert("Messege field is empty!");
        return false;
    }
    
    bootbox.confirm({
        title: "Are You Sure ?",
        message: "send sms",
        buttons: {
            cancel: {
                label: '<i class="fa fa-times"></i> Cancel SMS'
            },
            confirm: {
                label: '<i class="fa fa-check"></i> Confirm SMS'
            }
        },
        callback: function(result) {
            if (result == true) {
                send_sms(type);
            }
        }
    });
}

function send_sms(type){
 
/* hw_cus.forEach(function(index){
    console.log(index);
});

hw_cus.forEach(function(index){
    console.log(index);
});*/
var SendInfo= "";
var cus_list ="";
if(type == "hw"){
    cus_list = hw_cus;
}
else if(type == "dst"){
    cus_list = ds_cus;
}else if(type == "cus"){
    if(phoneNum($('#cus_number').val())){
        SendInfo= {
            "dest":$('#cus_number').val(),
            "msg": $('#msg').val()
        };
        callAjax(SendInfo);
    }else{
        bootbox.alert("Invalid Phone number!");
    }
    return true;
}

    var time = 500;
    cus_list.forEach(function(index){
        setTimeout( function(){
            
                var dest = "";
                var msg = $('#msg').val();
                
                var tr = "";
                if(phoneNum(index['cus_sms_number'])){
                    tr = "<tr><td>"+index['cus_name']+":"+index['cus_sms_number']+"</td><td></td></tr>";
                    SendInfo= {
                        "dest":index['cus_sms_number'],
                        "msg": msg
                    };
                    //console.log(SendInfo);
                    callAjax(SendInfo);
                }else{
                    //tr = "<tr><td><span class=\"label label-warning\">"+index['cus_name']+":"+index['cus_sms_number']+"</span></td><td><button class=\"btn btn-sm btn-info\">Try Again</button></td></tr>";
                    tr = "<tr><td><span class=\"label label-warning\">"+index['cus_name']+":"+index['cus_sms_number']+"</span></td><td><span class=\"label lbl-sm btn-warning\">ERROR</span></td></tr>";
                }
                $('#sent_table > tbody').append(tr);
                
        }, time)
        time += 500;
    });
    
}

function callAjax(SendInfo){
    $.ajax({
        type: "POST", 
        url: "<?php echo base_url().'send_sms/set_params'?>",
        /*data:{
            "dest":dest,
            "msg" :msg
        },*/
        data: JSON.stringify(SendInfo),
        headers: {
            "Content-type": "application/json"
        },
        /*contentType: "application/json; charset=utf-8",*/
        cache: false,
        success: function(response) {
            var obj = JSON.parse(response);
            if (obj.status === "0") {
                var tr = "<tr><td><span class=\"label label-success\">"+obj.dest+"</span></td><td><span class=\"label lbl-sm btn-success\">SENT</span></td></tr>";
                $('#sent_table > tbody').append(tr);
            } else {
                bootbox.alert({
                    message: "Error!",
                    size: 'small'
                });
            }
            
        }
    });
}
function tryagain(btn_id){
    
}
function phoneNum(phoneNum) {
    let text = phoneNum;
    if(text.length > 10){
        return false;
    }else{
        let n = text.search(/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im);
        if(n < 0){
            return false;
        }else return true;
    }
}
$("#sms_form").on("submit",function(e){
    e.preventDefault();
    begin_process();
})
$('input[type=radio][name=cus_type]').change(function() {
    if (this.value == 'cus') {
        $('#cus_number').removeAttr('readonly');
    }
    else {
        $('#cus_number').attr('readonly', true);
    }
});
		</script>

     
	</body>
	<!-- end: BODY -->
</html>