<?php
session_start();
include_once('../../config/#Config.php');
if(!$_SESSION['UserName']){
die('<script>window.location.href=\''.$config['site']['url'].'index.php\';</script>');
}
$get_AD = mysqli_query($conn, "Select * From config_admin WHERE Account = '".$_SESSION['UserName']."'");
if(!mysqli_num_rows($get_AD)){
	die('Không đủ quyền.');
}
?>
<style>
.discount {
    position: absolute;
    margin-left: 30px;
    margin-top: -25px;
    border: 2px solid #f15a21;
    border-radius: 100px 0 0 100px;
    padding: 2px;
    width: 50px;
    height: 23.5px;
}
.discount p {
    background: #f15a21;
    border-radius: 100px 0 0 100px;
    text-align: center;
    line-height: 0!important;
    font-size: .9em!important;
    padding: 7px;
}
.discount span {
    color: #fff;
    font-size: 11px;
    text-align: center;
    display: inline-block;
    font-weight: 700;
}
</style>
   
        <!-- Modal body -->
        <div class="modal-body">
            <div class="form-group pass_show"> 
                <input type="text" value=""  id="messager_mail" class="form-control" placeholder="Nội dung thư" > 
            </div>			
<center>
          <button type="button" class="btn btn-danger" onClick="if ($('#id1').css('display') == 'none') { $('#id1').show() } else { $('#id1').hide() }">Gửi Dạng List</button>
          <button type="button" class="btn btn-success" onClick="if ($('#id2').css('display') == 'none') { $('#id2').show() } else { $('#id2').hide() }">Gửi Dạng Đơn</button>
		  </center>
			<div id="id1" style="display:none">
			<font color="red">========> Gửi vật phẩm</font> <br />
 <div class="row align-items-start">
    <div class="col">
               <select id="loaivatpham" onChange="LoadItem();" class="form-control">
			   <?php
			   foreach (ItemList() as $key => $element) {

echo '<option value="'.$key.'">'.$element[0].'</option>';
}
?>			   
</select>
    </div>
    <div class="col">
                <input type="text" value=""  id="itemid_info" class="form-control" placeholder="ID vật phẩm hoặc tên vật phẩm"> 
    </div>
	<div class="col">
			<span id="searchresultdata">
			<select class="form-control" id="list_itemseacher">
			<?php 
			$_get_item = mysqli_query($conn, "Select * From item_goods");
			if(mysqli_num_rows($_get_item)){
				while($row = mysqli_fetch_array($_get_item, MYSQLI_ASSOC)){
					echo '<option value="'.$row['EventID'].'">'.$row['Name'].'</option>';
				}
			}
			?>
			</select>
			</span>
    </div>
	    <div class="col">
				<input type="submit" class="btn btn-success btn-block" value="Thêm" onClick="addItem();"/>
    </div>
  </div>
			<br />
<center>
<table border="1px">
        <tr>
			<td style="width:80px;height:80px;text-align: center;">
						<div class="discount" id="btn-xoa-0" onClick="xoaitem('0');" style="display:none">
                <p><span>Xóa</span></p>
            </div>
			<div id="item_0"><font color="blue">Chưa có</font></span></div>
			</td>
            <td style="width:80px;height:80px;text-align: center;">
			<div class="discount"  id="btn-xoa-1" onClick="xoaitem('1');" style="display:none">
                <p><span>Xóa</span></p>
            </div>
			<div id="item_1"><font color="blue">Chưa có</font></span></div>
			</td>
            <td style="width:80px;height:80px;text-align: center;">
						<div class="discount"  id="btn-xoa-2" onClick="xoaitem('2');" style="display:none">
                <p><span>Xóa</span></p>
            </div>
			<div id="item_2"><font color="blue">Chưa có</font></span></div>
			</td>
            <td style="width:80px;height:80px;text-align: center;">
						<div class="discount" id="btn-xoa-3" onClick="xoaitem('3');" style="display:none">
                <p><span>Xóa</span></p>
            </div>
			<div id="item_3"><font color="blue">Chưa có</font></span></div>
			</td>
            <td style="width:80px;height:80px;text-align: center;">
						<div class="discount" id="btn-xoa-4" onClick="xoaitem('4');" style="display:none">
                <p><span>Xóa</span></p>
            </div>
			<div id="item_4"><font color="blue">Chưa có</font></span></div>
			</td>			
        </tr>
    </table>
</center>			
			<br />
		       <label>Tùy chọn Gửi thêm</label>			
            <div class="form-group pass_show"> 
			<select class="form-control" id="guikhac">
            <option value=''>Lựa Chọn</option>
            <option value='Gold'>Vàng</option>
            <option value='Coin'>Bạc</option>
            <option value='VipExp'>Vip EXP</option>
			<option value='Exp'>EXP</option>
			<option value='Honor'>Vinh Dự</option>
			<option value='Renown'>Danh Vọng</option>
			<option value='Contrib'>Cống Hiến</option>
			<option value='SilverBoard'>Ngân sức</option>
			<option value='DarkSteel'>Huynh Đệ Thần Thạch</option>
			</select>
            </div>			
		       <label>Số lượng</label>			
            <div class="form-group pass_show"> 
                <input type="text" value=""  id="count_sendkhac" class="form-control" placeholder="Nhập số lượng"> 
            </div>					
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" onClick="saveInfo();">Gửi ngay</button>
        </div>
<input type="hidden" id="o_0" value="" style="width:0px;height:0px"/>
<input type="hidden" id="o_1" value="" style="width:0px;height:0px" />
<input type="hidden" id="o_2" value="" style="width:0px;height:0px" />
<input type="hidden" id="o_3" value="" style="width:0px;height:0px" />
<input type="hidden" id="o_4" value="" style="width:0px;height:0px" />		
		</div>
			<div id="id2" style="display:none">
			<font color="red">Gửi Dạng List</font> <br />
            <div class="form-group pass_show"> 
                <input type="text" value=""  id="item_list" class="form-control" placeholder="item,2804,1|Gold,50000"> 
            </div>			
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" onClick="saveInfoList();">Gửi ngay</button>
        </div>	
		</div>		
		</div>
		        </div>
				<script>
function LoadItem(){
	$.post('ajax/loaditem_setting.php', {list:$('#loaivatpham').val()}, function(data){
		$('#searchresultdata').html(data);
	});
}
 jQuery.fn.filterByText = function(itemid_info, selectSingleMatch) {
        return this.each(function() {
            var select = this;
            var options = [];
            $(select).find('option').each(function() {
                options.push({value: $(this).val(), text: $(this).text()});
            });
            $(select).data('options', options);
            $(itemid_info).bind('change keyup', function() {
                var options = $(select).empty().data('options');
                var search = $.trim($(this).val());
                var regex = new RegExp(search,"gi");

                $.each(options, function(i) {
                    var option = options[i];
                    if(option.text.match(regex) !== null) {
                        $(select).append(
                           $('<option>').text(option.text).val(option.value)
                        );
                    }
                });
                if (selectSingleMatch === true && $(select).children().length === 1) {
                    $(select).children().get(0).selected = true;
                }
            });            
        });
    };

    $(function() {
        $('#list_itemseacher').filterByText($('#itemid_info'), true);
    });  
</script>
<script type="text/javascript">
function xoaitem(o){
	$('#btn-xoa-'+o).hide();
	$('#o_'+o).val("");
	$('#item_'+o).html('<font color="blue">Chưa thêm</font>');
}
function addItem(){
	if($('#searchresultdata').find(":selected").val() == "" || $('#searchresultdata').find(":selected").val() == null || $('#searchresultdata').find(":selected").val() <= 0){
	alert('Vui lòng chọn vật phẩm để thêm vào.');
	} else {
	if($('#o_0').val() == ""){
		$('#item_0').html('<font color="red"><img src="assets/images/e8df9049a53edc450eacd12adfa54194_icon.png" style="width:30px" title="'+$('#searchresultdata').find(":selected").text()+'"/><Br/><center><input type="text" id="sl_0" value="1" class="form-control" style=" width: 100px; height: 20px; " maxlength="10"></center></font>');
		$('#o_0').val($('#searchresultdata').find(":selected").val());
		$('#btn-xoa-0').show();
		return false;
	} else if($('#o_1').val() == ""){
		$('#item_1').html('<font color="red"><img src="assets/images/e8df9049a53edc450eacd12adfa54194_icon.png" style="width:30px" title="'+$('#searchresultdata').find(":selected").text()+'"/><center><input type="text" id="sl_1" value="1" class="form-control" style=" width: 100px; height: 20px; " maxlength="10"></center></font>');
		$('#o_1').val($('#searchresultdata').find(":selected").val());
		$('#btn-xoa-1').show();		
		return false;		
	}else if($('#o_2').val() == ""){
		$('#item_2').html('<font color="red"><img src="assets/images/e8df9049a53edc450eacd12adfa54194_icon.png" style="width:30px" title="'+$('#searchresultdata').find(":selected").text()+'"/><center><input type="text" id="sl_2" value="1" class="form-control" style=" width: 100px; height: 20px; " maxlength="10"></center></font>');
		$('#o_2').val($('#searchresultdata').find(":selected").val());
		$('#btn-xoa-2').show();		
		return false;		
	}else if($('#o_3').val() == ""){
		$('#item_3').html('<font color="red"><img src="assets/images/e8df9049a53edc450eacd12adfa54194_icon.png" style="width:30px" title="'+$('#searchresultdata').find(":selected").text()+'"/><center><input type="text" id="sl_3" value="1" class="form-control" style=" width: 100px; height: 20px; " maxlength="10"></center></font>');
		$('#o_3').val($('#searchresultdata').find(":selected").val());
		$('#btn-xoa-3').show();
		return false;		
	}else if($('#o_4').val() == ""){
		$('#item_4').html('<font color="red"><img src="assets/images/e8df9049a53edc450eacd12adfa54194_icon.png" style="width:30px" title="'+$('#searchresultdata').find(":selected").text()+'"/><center><input type="text" id="sl_4" value="1" class="form-control" style=" width: 100px; height: 20px; " maxlength="10"></center></font>');
		$('#o_4').val($('#searchresultdata').find(":selected").val());
		$('#btn-xoa-4').show();
		return false;		
	} else {
	toastr.info('Không còn chỗ trống gửi thư', 'Chú ý');

	}
	}
}
function saveInfoList(){
			if($('#item_list').val() == ""){
				toastr.info('Vui lòng nhập list cần gửi đi', 'Chú ý');
			return false;				
			}
					$.post('ajax/ajax_sendmailist.php', 
			{   user:'<?=$_POST['user']?>',
				sv:'<?=$_POST['sv']?>',
				namenew:$('#nhanvat').val(),
				messager_mail:$('#messager_mail').val(),
				list:$('#item_list').val() }, function(data){
		if(data['type'] == 0){
			toastr.success(data['text'], 'Thông báo');
			
		} else {
			toastr.info(data['text'], 'Chú ý');
		
		}
	},'json');
}
function saveInfo(){
		 if($('#o_0').val() == "" && $('#o_1').val() == "" && $('#o_2').val() == "" && $('#o_3').val() == "" && $('#o_4').val() == "" && $('#guikhac').val() == ""){
			toastr.info('Vui lòng chọn thứ cần gửi đi', 'Chú ý');
			return false;
			} else {
			if($('#guikhac').val()){
				if($('#count_sendkhac').val() <= 0){
					toastr.info('Số lượng nhập không được nhỏ hơn 0', 'Chú ý');
			return false;				
				}	
			}
			if($('#sl_0').val() == 0 || $('#sl_1').val() == 0 || $('#sl_2').val() == 0 || $('#sl_3').val() == 0 || $('#sl_4').val() == 0){
				toastr.info('Số lượng không phù hợp', 'Chú ý');
				return false;
			}
					$.post('ajax/ajax_sendmail.php', 
			{   user:'<?=$_POST['user']?>',
				sv:'<?=$_POST['sv']?>',
				namenew:$('#nhanvat').val(),
				sl_0:$('#sl_0').val(),
				sl_1:$('#sl_1').val(),
				sl_2:$('#sl_2').val(),
				sl_3:$('#sl_3').val(),
				sl_4:$('#sl_4').val(),
				item_1:$('#o_0').val(),
				item_2:$('#o_1').val(),
				item_3:$('#o_2').val(),
				item_4:$('#o_3').val(),
				item_5:$('#o_4').val(),
				number:$('#guikhac').val(),
				messager_mail:$('#messager_mail').val(),
				count:$('#count_sendkhac').val() }, function(data){
		if(data['type'] == 0){
			toastr.success(data['text'], 'Thông Báo');
		} else {
			toastr.info(data['text'], 'Chú ý');
			
		}
	},'json');
}
}
function reloadItem(){
		$.post('ajax/ajax-search.php', {keyword:$('#itemid_info').val()}, function(data){
$('#searchresultdata').html(data);
});
	return false;		// Không chuyển trang
}
</script>
		   