<?php

/**
 * Created by mr2app.
 * User: hani
 * Date: 12/18/17
 * Time: 9:44 AM
 */

if (!defined( 'ABSPATH' )) exit;
$sms = get_option('mr2app_sms');
if(!is_array($sms)){
	$sms = array(
		'enable' => 0,
		'one-time' => 0,
		'field' => '',
		'panel' =>'',
		'number' => '',
		'username'=> '',
		'password' => '',
		'pattern_code' => '',
		'pattern_password' => '',
	);
}

$current_url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if(isset($_REQUEST["submit_edit"]) || isset($_REQUEST["submit_test"])){
	$pattern_code = $_POST['pattern_code_sms_ir'];
	$pattern_password =  $_POST['pattern_password_sms_ir'];

	$sms = array(
		'enable' => (isset($_POST['enable']) && $_POST['enable'] == 'on') ? 1 : 0,
		'one-time' => (isset($_POST['one-time']) && $_POST['one-time'] == 'on') ? 1 : 0,
		'field' => $_POST['field'],
		'panel' => $_POST['panel'],
		'number' => $_POST['number'],
		'username'=> $_POST['username'],
		'password' => $_POST['password'],
		'pattern_code' => $pattern_code,
		'pattern_password' => $pattern_password,
	);
	if(update_option( 'mr2app_sms',$sms )){
		?>
        <div class="notice notice-success is-dismissible">
            <p> <?= _e('Settings saved successfully.' , 'woo2app'); ?></p>
        </div>
		<?php
	}
	else {
		if(isset($_REQUEST["submit_edit"])) {
			?>
            <div class="notice notice-danger is-dismissible">
                <p> <?= _e('Unfortunately, we encountered a problem.' , 'woo2app'); ?></p>
            </div>
			<?php
		}
	}
	if(isset($_REQUEST["submit_test"])){
		global $class_custom_register_woo2app;
		//echo $_POST['mobile'];
		$test = $class_custom_register_woo2app->test_sms($_POST['mobile']);
		if(isset($test['status']) && $test['status'] == true){
			?>
            <div class="notice notice-success is-dismissible">
                <p> <?= _e('SMS Sent successfully.' , 'woo2app'); ?></p>
            </div>
			<?php
		}
		else{
			?>
            <div class="notice notice-danger is-dismissible">
                <p> <?= _e('Error sending SMS.' , 'woo2app'); ?></p>
            </div>
			<?php
		}
	}
}
if(!isset($sms['pattern_password'])) $sms['pattern_password'] = '117';
if(!isset($sms['pattern_code'])) $sms['pattern_code'] = '681';
?>
<div class="wrap" >
    <div id="col-container" >
        <div class="col-wrap" >
            <div class="form-wrap">
                <form id="addtag" method="post" action="" class="validate">
                    <table class="form-table ">
                        <tr>
                            <th> <?= _e('Enable System' , 'woo2app');?> </th>
                            <td><input type="checkbox" <?= $sms['enable'] ? 'checked' : '';?>  name="enable"  /></td>
                        </tr>
                        <tr>
                            <th> <?= _e('Temporary Password' , 'woo2app');?> </th>
                            <td><input type="checkbox" <?= $sms['one-time'] ? 'checked' : '';?>  name="one-time"  /></td>
                        </tr>
                        <tr>
                            <th> <?= _e('Select Mobile Field' , 'woo2app');?> </th>
                            <td>
								<?php
								$args      = array(
									'post_type'   => 'woo2app_register',
									'post_status' => 'draft',
									'posts_per_page' => -1,
									'orderby' => 'menu_order',
									'order' => 'ASC'
								);
								$the_query = get_posts( $args );
								?>
                                <select name="field" class="regular-text">
									<?php
									foreach ($the_query as $f){
										?>
                                        <option <?= ($sms['field'] == $f->post_content)? 'selected' : '' ;?> value="<?= $f->post_content; ?>">
											<?= $f->post_title;?>
                                        </option>
										<?php
									}
									?>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <th> <?= _e('Select SMS Panel' , 'woo2app');?></th>
                            <td>
                                <select name="panel" class="regular-text" onchange="change_panel(this)">
                                    <option <?= ($sms['panel'] == 'payamresan')? 'selected' : '' ;?> value="payamresan">  پیام رسان - payamresan </option>
                                    <option <?= ($sms['panel'] == 'rangine')? 'selected' : '' ;?> value="rangine">  rangine </option>
                                    <option <?= ($sms['panel'] == 'melipayamak')? 'selected' : '' ;?> value="melipayamak">  melipayamak </option>
                                    <option <?= ($sms['panel'] == 'fast_sms_ir')? 'selected' : '' ;?> value="fast_sms_ir">  sms.ir </option>
                                    <option <?= ($sms['panel'] == 'farazsms')? 'selected' : '' ;?> value="farazsms">  farazsms </option>
                                    <option <?= ($sms['panel'] == 'parsgreen')? 'selected' : '' ;?> value="parsgreen">  Parsgreen.com </option>
                                    <option <?= ($sms['panel'] == 'dlesms_ir')? 'selected' : '' ;?> value="dlesms_ir">  dlesms.ir </option>
                                    <option <?= ($sms['panel'] == 'kavenegar')? 'selected' : '' ;?> value="kavenegar">  kavenegar.com </option>
                                    <option <?= ($sms['panel'] == 'aryan_sms_ir')? 'selected' : '' ;?> value="aryan_sms_ir">  aryan_sms_ir </option>
                                    <option <?= ($sms['panel'] == 'ippanel')? 'selected' : '' ;?> value="ippanel">  ippanel </option>
                                    <option <?= ($sms['panel'] == 'parsniaz')? 'selected' : '' ;?> value="parsniaz">  parsniaz </option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <th><?= _e('SMS Number' , 'woo2app');?> </th>
                            <td><input name="number" class="regular-text" dir="ltr" type="text" value="<?= $sms['number'];?>" /></td>
                        </tr>
                        <tr>
                            <th><?= _e('Username Or ApiKey' , 'woo2app');?>   </th>
                            <td>
                                <input name="username" class="regular-text" dir="ltr"  type="text" value="<?= $sms['username'];?>" />
                                <p>
                                    <small> <?= _e('SmsPanel Username' , 'woo2app');?>  </small>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <th> <?= _e('Password Or SecurityKey' , 'woo2app');?>  </th>
                            <td>
                                <input name="password" class="regular-text" dir="ltr" type="password" value="<?= $sms['password'];?>" />
                                <p>
                                    <small> <?= _e('SmsPanel Password' , 'woo2app');?>  </small>
                                </p>
                            </td>
                        </tr>
	                    <?php
	                    $array = array(
		                    "rangine", "dlesms_ir" , "ippanel" , "parsniaz" , 'farazsms' , 'fast_sms_ir' , 'kavenegar' , 'melipayamak'
	                    );
	                    ?>
                        <tr class="pattern_div_sms_ir <?= (in_array($sms['panel'],$array)  ) ?: 'hide-all' ;?> ">
                            <th> <?= _e('VerifyCode pattern' , 'woo2app');?> </th>
                            <td>
                                <input type="text" class="regular-text" name="pattern_code_sms_ir"  value="<?= $sms['pattern_code']?>"/>
                                <p id="msg_passwordk">
                                </p>
                            </td>
                        </tr>
                        <tr class="pattern_div_sms_ir <?= (in_array($sms['panel'],$array)  ) ?: 'hide-all' ;?> ">
                            <th><?= _e('Forget Password pattern' , 'woo2app');?>  </th>
                            <td>
                                <input type="text" class="regular-text" name="pattern_password_sms_ir"  value="<?= $sms['pattern_password']?>"/>
                                <p id="msg_passwordk">
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td class="submit" colspan="2">
                                <input type="submit" name="submit_edit"  class="button button-primary" value="<?= _e('Save Changes' , 'woo2app');?>"  />
                                <button type="submit" onclick="send_test()" name="submit_test" class="button"> <?= _e('Sms Test' , 'woo2app');?> </button>
                                <input type="hidden" id="mobile" name="mobile" value="" />
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    function change_panel(e) {
        var myarr = ["rangine", "dlesms_ir" , "ippanel" , "parsniaz" , 'farazsms' , 'fast_sms_ir' , 'kavenegar' , 'melipayamak'];
        if(myarr.indexOf(e.value) > -1){
            //jQuery(".pattern_div").removeClass('hide-all')
            jQuery(".pattern_div_sms_ir").removeClass('hide-all')
            jQuery(".pattern_div").addClass('hide-all')
        }
        else if(e.value == 'kavenegar'){
            jQuery(".pattern_div_kavenegar").removeClass('hide-all')
            jQuery(".pattern_div_sms_ir").addClass('hide-all')
        }
        else{
            jQuery(".pattern_div_sms_ir").addClass('hide-all')
            jQuery(".pattern_div_kavenegar").addClass('hide-all')
        }

    }
    function send_test() {
        let mobile = prompt("لطفا شماره همراه را وارد کنید.");
        if(mobile != null){
            jQuery("#mobile").val(mobile);
            jQuery("form").submit();
        }
    }
</script>