<?php
include_once ("./static_data/continent.php");

$menu_code = '65';
require_once (APPPATH.'views/skin/admin/_content_comm.php');



$arr_weather_rank = array();
foreach ($arr_weather as $val) {
    $arr_weather_rank[$val['rank']] = array('continent'=> $val['continent'], 'country'=> $val['country'],'weather_icon'=>$val['weather_icon'], 'weather_content'=>$val['weather_content'], 'display_date'=>$val['display_date'], 'display_status'=>$val['display_status'], 'is_auto_setting'=>$val['is_auto_setting'], 'tag'=>$val['tag']);
}

$arr_relation_article_rank = array();
foreach ($arr_relation_article as $val) {
    $arr_relation_article_rank[$val['rank']][] = array('title'=> $val['title'], 'relation_article_idx'=> $val['idx']);
}


?>


<form name="input_form" id="input_form" action="/admin/etc_section/weather_list_reg_proc" method="post" enctype="multipart/form-data">
<input type="hidden" name="idx" id="idx" value="<?=$idx?>">
<input type="hidden" id="search_type" name="search_type" value="<?=$search_type?>">
<input type="hidden" id="search_keyword" name="search_keyword" value="<?=$search_keyword?>">
<input type="hidden" id="search_display_status" name="search_display_status" value="<?=$search_display_status?>">
<input type="hidden" id="page" name="page" value="<?=$page?>">
<input type="hidden" id="rank" name="rank">


<span class="text_blue"><b>* 모바일과 공통 송출관리</b></span>
<div class="common_table news">
<table>


<?
   for($z=1;$z<=10;$z++) {

       $continent          = '';
       $country            = '';
       $weather_icon       = '';
       $weather_content    = '';
       $tag                = '';
       $is_auto_setting    = 'Y';
       $continent_key      = '';
       if(isset($arr_weather_rank[$z])== true) {
           $continent       = $arr_weather_rank[$z]['continent'];
           $country         = $arr_weather_rank[$z]['country'];
           $weather_icon    = $arr_weather_rank[$z]['weather_icon'];
           $weather_content = $arr_weather_rank[$z]['weather_content'];
           $weather_content = htmlspecialchars($weather_content);
           $tag             = $arr_weather_rank[$z]['tag'];
           $tag             = str_replace(" ", ",", $tag);
           $is_auto_setting = $arr_weather_rank[$z]['is_auto_setting'];

           $continent_key_tmp = array_keys($arr_continent1, $continent);
           $continent_key     = $continent_key_tmp[0];

       }


?>
        <tr>
            <th rowspan="6"><?=$z?>위</th>
            <td class="tl">
                <select name="continent<?=$z?>" id="continent<?=$z?>" onchange="change_continent(this.value, '<?=$z?>')">
                    <option value="">대륙선택</option>
        <?  foreach ($arr_continent1 as $key=>$val) {?>
                    <option value="<?=$key?>" <? if($continent==$val) { echo 'selected';}?>><?=$val?></option>

        <?  }?>
                </select>
            </td>
        </tr>

        <tr>

            <td class="tl">
                <select name="country<?=$z?>" id="country<?=$z?>">
                    <option value="">나라선택</option>
    <?  if(isset($arr_continent2[$continent_key])==true) {
            foreach ($arr_continent2[$continent_key] as $val) {
    ?>
                <option value="<?=$val?>" <? if($country==$val) { echo 'selected';}?>><?=$val?></option>
    <?      }

        }?>

            </select>
        </td>
    </tr>
    <tr>
        <td class="tl">
            날씨아이콘
            <button type="button" class="btn_black" style="width:100px" onclick="view_icon('<?=$z?>');" style="cursor: pointer;">선택하기</button>
            <input type="hidden" name="weather_icon<?=$z?>" id="weather_icon<?=$z?>" value="<?=$weather_icon?>">
            <div id="icon_img<?=$z?>">
        <? 	if($weather_icon!='') { ?>
            <img src="<?=$weather_icon?>" width="50px">
        <?	} ?>
            </div>
        </td>
    </tr>
    <tr>
        <td class="tl">
            <input type="text" name="weather_content<?=$z?>" id="weather_content<?=$z?>" value="<?=$weather_content?>" placeholder="날씨코멘트">
        </td>
    </tr>
    <tr>
        <td class="tl">
            <input type="text" name="tag<?=$z?>" id="tag<?=$z?>" class="type01" value="<?=$tag?>"> 최대 5개까지 입력 가능(,로 구분해주세요.)
        </td>
    </tr>
    <tr>
        <td class="tl">관련뉴스
            <input type="radio" name="is_auto_setting<?=$z?>" id="is_auto_setting<?=$z?>" onclick="click_relation_news_auto_setting('Y', '<?=$z?>');" value="Y" <? if($is_auto_setting=='Y') { echo 'checked';}?>> 자동설정&nbsp;&nbsp;
            <input type="radio" name="is_auto_setting<?=$z?>" id="is_auto_setting<?=$z?>" onclick="click_relation_news_auto_setting('N', '<?=$z?>');" value="N" <? if($is_auto_setting=='N') { echo 'checked';}?>> 수정설정
            <div class="two_box_type01 clearfix" id="no_auto_setting_content<?=$z?>" style="display:none; margin-top:10px;">
                <div class="con">
                    <div class="searh_area fn mb10">
                        <input type="text" name="article_keyword<?=$z?>" id="article_keyword<?=$z?>" class="input00" size="20" placeholder="검색어 입력"  onkeypress="if( event.keyCode==13 ){search_article('<?=$z?>');}">
                        <a href="javascript:search_article('<?=$z?>');" class="btn"><i class="fa moving_all fa-search" style="margin-left:8px;"></i></a>
                    </div>
                     <div class="mb10">
                         <input type="radio" name="article_sort<?=$z?>" id="article_sort<?=$z?>" value="1"  onclick="search_article('<?=$z?>');" checked>&nbsp;최신순&nbsp;
                         <input type="radio" name="article_sort<?=$z?>" id="article_sort<?=$z?>" value="2" onclick="search_article('<?=$z?>');">&nbsp;오래된순&nbsp;

                        &nbsp;&nbsp;&nbsp;검색결과&nbsp;  <span class="mark" id="result_count<?=$z?>">0</span>건
                    </div>
                    <div class="list scs">
                        <ul id="result<?=$z?>">
                        </ul>
                    </div>
                </div>
                <div class="con">
                    <h3 class="mb10">선택된 뉴스 항목</h3>
                    <div class="list type02 scs">
                        <ul id="selected_news<?=$z?>">
                            <?
                            if(isset($arr_relation_article_rank[$z])==true) {
                                $i=1;
                                foreach ($arr_relation_article_rank[$z] as $val) {
                                    ?>
                                    <li><input type="checkbox" name="relation_idxs<?=$z?>[]" id="relation_idx<?=$z?>_<?=$i?>" value="<?=$val['relation_article_idx']?>"  onclick="delete_article('<?=$i?>', '<?=$z?>');" checked><a href="/web/news/news_view/<?=$val['relation_article_idx']?>" target="_blank"><?=$val['title']?></a></li>
                            <?
                                    $i++;
                                }
                            }

                            ?>
                        </ul>
                    </div>
                </div>
            </div>
        </td>

    </tr>
<?

      if($is_auto_setting=='N') {
?>
        <script type="text/javascript">
            $("#no_auto_setting_content<?=$z?>").show();
        </script>


<?
      }

   }
?>

</table>
</div>


<div class="write_btn tc">
    <a href="javascript:weather_list_reg_proc()">저장하기</a>
    <a href="/admin/etc_section/weather_list/6/<?=$page?>?search_type=<?=$search_type?>&search_keyword=<?=$search_keyword?>&search_display_status=<?=$search_display_status?>">목록으로</a>

</div>






<div id="icon_reg_layer" style="display:none">
<div class="black_board" style="display:block"></div>
<div class="pop_con common_table moving_all" style="display:block;top:100px;">
   <a href="javascript:commonLayerClose('icon_reg_layer')" class="pop_close">x</a>
	<h2>채널아이콘</h2>
	<table>
		<tr>
			<th style="width:150px">아이콘선택</th>
			<td class="tl">
				<div class="icon_list" style="overflow-y: scroll; height:150px;">
			<?	foreach ($weather_icon_list as $val) {?>
					<a href="#;" class="por">
						 <input type="radio" name="sel_weather_icon" id="sel_weather_icon" class="stebox" value="<?=$val['weather_icon']?>">
						  <img src="<?=$val['weather_icon']?>" width="50px" />
				  	</a>
			<?	}?>
				</div>
			</td>
		</tr>

	</table>
	<div class="write_btn tc">
		<a href="javascript:select_weather_icon('add')">적용하기</a>
		<a href="javascript:commonLayerClose('icon_reg_layer')">취소하기</a>
	</div>
</div>
</div>







</form>


<script type="text/javascript">

// 대륙변경
function change_continent(val, rank){
    var json_category = '<?=json_encode($arr_continent2)?>';
    json_category = eval(json_category);

    $('#country'+rank).empty();
    var option = "<option value=''>선택</option>";
    $('#country'+rank).append(option);
    for(key in json_category[val]) {
        option = $("<option value='"+json_category[val][key]+"'>"+json_category[val][key]+"</option>");
            $('#country'+rank).append(option);

    }
}



// 아이콘 보기
function view_icon(rank){
    $("#rank").val(rank);
	commonLayerOpen('icon_reg_layer')

}

// 아이콘 적용
function select_weather_icon(){
	var weather_icon = $("input[type=radio][id=sel_weather_icon]:checked").val();
    var rank = $("#rank").val();

    if(weather_icon == '' || weather_icon == undefined) {
        alert("적용할 아이콘이 없습니다.");
    } else {
        $("#weather_icon"+rank).val(weather_icon);
        $("#icon_img"+rank).empty();
        $("#icon_img"+rank).html('<img src="'+weather_icon+'" width="50px">');
    }

	commonLayerClose('icon_reg_layer')

}


// 관련뉴스검색
function click_relation_news_auto_setting(type, rank){
    $("#rank").val(rank);
	if(type == 'Y') {
		$("#no_auto_setting_content"+rank).hide();
	} else {
		$("#no_auto_setting_content"+rank).show();
        $("#article_keyword"+rank).focus();
	}
}

function search_article(rank){
    $("#rank").val(rank);

	var article_keyword = $("#article_keyword"+rank).val();
	var article_sort = $(":input:radio[name=article_sort"+rank+"]:checked").val();

	if(article_keyword == '') {
		alert("검색어를 입력해주세요");
		return false;
	}
	var form_data = {
		"content_type"			: "1",
		"article_keyword"		: article_keyword,
		"sort"					: article_sort,
		"type"					: 'JSON'
	};
	$.ajax({
		url: '/admin/search/search_article_list',
		async : false,
		type : 'POST',
		dataType : 'JSON',
		data : form_data,
		success: function (rtnJson) {
			if(rtnJson.result_code == '0000') {
				if(article_sort==1) {
					data = sortJSON(rtnJson.result_data.list,'reg_date', '321'); // 123 or 321
				} else {
					data = sortJSON(rtnJson.result_data.list,'reg_date', '123'); // 123 or 321
				}
				$('#result_count'+rank).html(rtnJson.result_data.count);
				$('#result'+rank).empty();



				if(rtnJson.result_data.count == 0) {
					$('#result'+rank).html("<li>검색된 내용이 없습니다.</li>");
				} else {
					for(key in data) {
						var title = data[key]['title'];
						title = title.replace(/'/g, "&#039;");
						title = title.replace(/"/g, '&quot;');

						$('#result'+rank).append('<li class="title"><a href="javascript:;" onclick="select_article(\''+data[key]['idx']+'\', \''+title+'\');">'+title+'</a></li>');
					}
				}

			} else {
				alert("뉴스검색에 실패했습니다.");
				return false;
			}

		},
		error: function(jqXHR, exception) {
			console.log(jqXHR);
		}
	});
}


function select_article(idx, title){
    var rank = $("#rank").val();
    var index = ($("input[name=\"relation_idxs"+rank+"[]\"]").length)+1;
	$('#selected_news'+rank).append('<li><input type="checkbox" name="relation_idxs'+rank+'[]" id="relation_idx'+rank+'_'+index+'" value="'+idx+'" onclick="delete_article('+index+', '+rank+');"> <a href="/web/news/news_view/'+idx+'" target="_blank">'+title+'</a></li>');
	$("input[name='relation_idxs"+rank+"[]'][value=" + idx + "]").attr("checked", true);
}

function delete_article(idx, rank) {
    $("#rank").val(rank);
	var checkbox_id = 'relation_idx'+rank+'_'+idx;
	if($('input:checkbox[id=checkbox_id]').is(":checked") == false){
		$("#"+checkbox_id).parent().remove();
	}
}


function weather_list_reg_proc(){
    for(i=1;i<=10;i++) {
        if($("#continent"+i).val()!='') {
            if($("#country"+i).val()==''){
                alert("나라를 선택해주세요.");
                return false;
            }
            if($("#weather_icon"+i).val()=='') {
                alert("날씨아이콘을 입력해주세요.");
                return false;
            }

            if($("#weather_content"+i).val()=='') {
                alert("날씨코멘트를 입력해주세요.");
                return false;
            }

            var is_auto_setting = $("input[type=radio][id=is_auto_setting"+i+"]:checked").val();
            if(is_auto_setting == 'Y' && $("#tag"+i).val()=='') {
                alert("관련뉴스를 자동설정 하시려면 tag가 필요합니다.");
                return false;
            }

         }
    }

	$("#input_form").submit();

}




// 아이콘 이미지 선택
$(".icon_list a").click(function(){
   $(".icon_list a").removeClass("on");
	$(this).addClass("on");
});


</script>