 <?php 

require_once '../Connections/courier.php';
require_once '../functions/functions.php';
set_time_limit(90000); 
$action = $_POST['act'];
$id = mysql_real_escape_string($_POST['id']);

if($action == 'get_sms_template'){
 $row = mysql_fetch_assoc(mysql_query("SELECT * FROM sms_templates WHERE id = '$id'", $courier));   
    ?>
 <table class="table table-striped table-bordered table-hover table-responsive">
                    <tbody>
                        <tr>
                            <td style="width:15%">Title / Name</td>
                            <td>
                                <input type="hidden" name="template_id" id="template_id" value="<?php echo $row['id'];?>">
                                <input <?php if($row['readonly'] == 'yes'){echo 'readonly=""';} ?> value="<?php echo $row['title'];?>" class="form-control" type="text" name="title" id="title">
                            </td>
                        </tr>
                        <?php if($row['title'] != 'BIRTHDAY WISHES'){ ?>
                        <tr>
                            <td style="width:15%">Delivery Method</td>
                            <td>
                                <select onchange="if(this.value == 'Automated'){$('#repeat_wrapper').show();}else{$('#repeat_wrapper').hide();}" class="form-control" name="delivery_method" id="delivery_method">
                                    <option value="">Please Select</option>
                                    <option <?php if($row['delivery_method'] == 'Manual'){echo 'selected=""';} ?> value="Manual">Manual</option>
                                    <option <?php if($row['delivery_method'] == 'Automated'){echo 'selected=""';} ?> value="Automated">Automated</option>
                                </select>
                            </td>
                        </tr>
                        <?php if($row['delivery_method'] == 'Automated'){ ?>
                        <tr id="repeat_wrapper">
                            <td>Date</td>
                            <td>
                                <input value="<?php echo $row['date'];?>" type="text" name="date_time" id="date_time" class="form-control" readonly placeholder="Click Me">
                                <select class="form-control" name="repeat_method" id="repeat_method">
                                    <option <?php if($row['repeat_duration'] == 'No'){echo 'selected=""';} ?> value="No">No Repeat</option>
                                    <option <?php if($row['repeat_duration'] == 'Year'){echo 'selected=""';} ?> value="Year">Every Year</option>
                                    <option <?php if($row['repeat_duration'] == 'Month'){echo 'selected=""';} ?> value="Month">Every Month</option>
                                    <option <?php if($row['repeat_duration'] == 'Week'){echo 'selected=""';} ?> value="Week">Every Week</option>
                                    <option <?php if($row['repeat_duration'] == 'Day'){echo 'selected=""';} ?> value="Day">Every Day</option>
                                </select>
                            </td>
                        </tr>
                        <?php }else{ ?>
                        <tr id="repeat_wrapper" style="display: none;">
                            <td>Date</td>
                            <td>
                                <input value="<?php echo $row['datei'];?>" type="text" name="date_time" id="date_time" class="form-control" readonly placeholder="Click Me">
                                <select class="form-control" name="repeat_method" id="repeat_method">
                                    <option <?php if($row['repeat_duration'] == 'No'){echo 'selected=""';} ?> value="No">No Repeat</option>
                                    <option <?php if($row['repeat_duration'] == 'Year'){echo 'selected=""';} ?> value="Year">Every Year</option>
                                    <option <?php if($row['repeat_duration'] == 'Month'){echo 'selected=""';} ?> value="Month">Every Month</option>
                                    <option <?php if($row['repeat_duration'] == 'Week'){echo 'selected=""';} ?> value="Week">Every Week</option>
                                    <option <?php if($row['repeat_duration'] == 'Day'){echo 'selected=""';} ?> value="Day">Every Day</option>
                                </select>
                            </td>
                        </tr> 
                        <?php } ?>
                        <?php } ?>
                        <tr>
                            <td>Message</td>
                            <td>
                                <b onclick="addtosms_replace('[First name]')"> [First name] </b> --
                                <b onclick="addtosms_replace('[Last name]')"> [Last name] </b> --
                                <b onclick="addtosms_replace('[Full name]')"> [Full name] </b> -- 
                                <b onclick="addtosms_replace('[Patient No]')"> [Patient No] </b> -- 
                                <b onclick="addtosms_replace('[Patient Visit No]')"> [Patient Visit No] </b>
                                <?php if($row['title']=='BIRTHDAY WISHES'){ ?>
                                -- <b onclick="addtosms_replace('[Age]')"> [Age] </b>
                                <?php } ?>
                                <br><br>
                                <textarea required name="msg" class="form-control" style="height:150px;resize: none;" id="msg" onkeyup="SMSCounter(this.value.length)" onkeypress="SMSCounter(this.value.length)"><?php echo $row['sms_template'];?></textarea>
                                <p>
                                    <span id="remaining" style="font-weight: bold;color: deepskyblue" >160 characters remaining</span>
                                    <span id="" style="font-weight: bold;color:#000" > || </span>
                                    <span id="messages" style="font-weight: bold;color: deepskyblue" >1 message(s)</span>
                                    <span id="" style="font-weight: bold;color: deepskyblue;float: right" >160 / per sms</span>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <button type="submit" class="btn green">
                                    Save <i class="icon-ok"></i>
                                </button>            
                            </td>
                        </tr>
                    </tbody>
                </table>    
<?php }

if($action == 'add_sms_template'){ ?>
    <table class="table table-striped table-bordered table-hover table-responsive">
                    <tbody>
                        <tr>
                            <td style="width:15%">Title / Name</td>
                            <td>
                                <input class="form-control" type="text" name="title" id="title">
                            </td>
                        </tr>
                        <tr>
                            <td style="width:15%">Delivery Method</td>
                            <td>
                                <select onchange="if(this.value == 'Automated'){$('#repeat_wrapper').show();}else{$('#repeat_wrapper').hide();}" class="form-control" name="delivery_method" id="delivery_method">
                                    <option value="">Please Select</option>
                                    <option <?php if($row['delivery_method'] == 'Manual'){echo 'selected=""';} ?> value="Manual">Manual</option>
                                    <option <?php if($row['delivery_method'] == 'Automated'){echo 'selected=""';} ?> value="Automated">Automated</option>
                                </select>
                            </td>
                        </tr>
                        <tr id="repeat_wrapper" style="display: none;">
                            <td>Date</td>
                            <td>
                                <input type="text" name="date_time" id="date_time" class="form-control" readonly placeholder="Click Me">
                                <select class="form-control" name="repeat_method" id="repeat_method">
                                    <option <?php if($row['repeat_duration'] == 'No'){echo 'selected=""';} ?> value="No">No Repeat</option>
                                    <option <?php if($row['repeat_duration'] == 'Year'){echo 'selected=""';} ?> value="Year">Every Year</option>
                                    <option <?php if($row['repeat_duration'] == 'Month'){echo 'selected=""';} ?> value="Month">Every Month</option>
                                    <option <?php if($row['repeat_duration'] == 'Week'){echo 'selected=""';} ?> value="Week">Every Week</option>
                                    <option <?php if($row['repeat_duration'] == 'Day'){echo 'selected=""';} ?> value="Day">Every Day</option>
                                </select>
                            </td>
                        </tr> 
                        <tr>
                            <td>Message</td>
                            <td>
                                <b onclick="addtosms_replace('[First name]')"> [First name] </b> --
                                <b onclick="addtosms_replace('[Last name]')"> [Last name] </b> --
                                <b onclick="addtosms_replace('[Full name]')"> [Full name] </b> -- 
                                <b onclick="addtosms_replace('[Patient No]')"> [Patient No] </b> -- 
                                <b onclick="addtosms_replace('[Patient Visit No]')"> [Patient Visit No] </b>
                                <?php if($row['title']=='BIRTHDAY WISHES'){ ?>
                                -- <b onclick="addtosms_replace('[Age]')"> [Age] </b>
                                <?php } ?>
                                <br><br>
                                <textarea required name="msg" class="form-control" style="height:150px;resize: none;" id="msg" onkeyup="SMSCounter(this.value.length)" onkeypress="SMSCounter(this.value.length)"><?php echo $row['sms_template'];?></textarea>
                                <p>
                                    <span id="remaining" style="font-weight: bold;color: deepskyblue" >160 characters remaining</span>
                                    <span id="" style="font-weight: bold;color:#000" > || </span>
                                    <span id="messages" style="font-weight: bold;color: deepskyblue" >1 message(s)</span>
                                    <span id="" style="font-weight: bold;color: deepskyblue;float: right" >160 / per sms</span>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <button type="submit" class="btn green">
                                    Save <i class="icon-ok"></i>
                                </button>            
                            </td>
                        </tr>
                    </tbody>
                </table>  

<?php }

 if ($action == 'save_sms_template') {
    $template_id = $_POST['template_id'];
    $title = $_POST['title'];
    $delivery_method = $_POST['delivery_method'];
    $date_time = $_POST['date_time'];
    $repeat_method = $_POST['repeat_method'];
    $msg = $_POST['msg'];
    if ($repeat_method != 'No') {
         $date = $date_time;
//       $date = date('Y-m-d', strtotime('+ 1 ' . $repeat_method, strtotime($date_time)));
    } else {
        $date = $date_time;
    }
    $check = mysql_num_rows(mysql_query("SELECT * FROM sms_templates WHERE id = '$template_id'", $courier));
    if ($check > 0) {
        mysql_query("UPDATE sms_templates SET title='$title',sms_template='$msg',delivery_method='$delivery_method',repeat_duration='$repeat_method',date='$date' WHERE id  = '$template_id'", $courier);
    } else {
        mysql_query("INSERT INTO sms_templates (title,sms_template,delivery_method,repeat_duration,date) VALUES('$title','$msg','$delivery_method','$repeat_method','$date')", $courier);
    }
}

if($action == 'delete_sms_template'){
    mysql_query("DELETE FROM sms_templates WHERE id = '$id'", $courier);
}

if ($action == 'addtosmstel') {
        $info = explode(",", str_replace(" ", "", $_POST['tel']));
        foreach ($info as $key) {
            if (!empty($key)) {
                    mysql_query("INSERT INTO sms_temporal (tel,type) VALUES('".substr($key, 0,10)."','individual')", $courier);
            }
        }
}

if($action == 'allpatients'){
    mysql_query("INSERT INTO sms_temporal(tel,type) VALUES('All Patients','all_patients')", $courier);
}

if($action == 'birthdaypatients'){
    mysql_query("INSERT INTO sms_temporal(tel,type) VALUES('All Birthday Patients','all_birthday_patients')", $courier);
}

if($action == 'deletesmsreceiver'){
mysql_query("DELETE FROM sms_temporal WHERE id = '".$_POST['id']."'");
}

if($action == 'deletesmsall'){
mysql_query("DELETE FROM sms_temporal");
}

if ($action == 'addtosms') {
	//echo $_POST['id'];
    $info = explode("@@@", $_POST['id']);
    $fullname = $info[0] .' '.$info[1] ;
    $check = mysql_num_rows(mysql_query("SELECT * FROM sms_temporal WHERE type='all_patients'", $courier));
    if($check < 1){
    if (!empty($info[1])) {
        mysql_query("INSERT INTO sms_temporal (patient_no,surname,othernames,patient_name,tel,type,dateofbirth) VALUES('" . $info[3] . "','" . $info[0] . "','" . $info[1] . "','$fullname','".substr($info[2], 0,10)."','individual','".$info[4]."')");
		//echo "3333";
    }
  }
}

if ($action == 'addtosmstel' || $action == 'deletesmsreceiver' || $action == 'deletesmsall' || $action == 'allpatients' || $action=='addtosms' || $action=='birthdaypatients') {
    $query = mysql_query("SELECT * FROM sms_temporal ORDER BY id DESC", $courier);
    while ($row = mysql_fetch_assoc($query)) { ?> 
        <span  id="sms_<?php echo $row['id']; ?>"  class="badge badge-roundless badge-success"><?php if ($row['patient_name'] != "") {
            echo strtoupper($row['patient_name']);
        } else {
            echo $row['tel'];
        } ?>  <b id="sms<?php echo $row['id']; ?>" style="" onclick="process_information_sms('sms/sms_step.php', 'deletesmsreceiver', 'receiverswrapper','<?php echo $row['id']; ?>');">&times;</b></span> 
    <?php }
    if (mysql_num_rows($query) > 1) { ?> 
        <span onclick="process_information_sms('sms/sms_step.php', 'deletesmsall', 'receiverswrapper');"  class="badge badge-roundless badge-danger">Delete All  <b >&times;</b></span> 
    <?php } ?> 
<?php
}
