<?php
/*
0 - не показвать
1 - в активном поиске
2 - не женат
3 - встречаюсь
4 - помолвлен
5 - женат
6 - влюблён
7 - все сложно
*/
	$thisLink .= "?act=anketa/edit";

	if(isset($_GET['select_partner'])) {
		require ("sp.php");
	}

	if(isset($_GET['status'])) {
		$rs_status = intval($_GET['status']);
	}

	$act = "";
	if(isset($_GET['sel'])) {
		if($_GET['sel'] == 'personal'){$act = 'personal';}
		else if($_GET['sel'] == 'interests'){$act = 'interests';}
	}
	if(isset($_GET['partner'])) {
		$rs_partner = intval($_GET['partner']);
		$partner = profile :: getUser($rs_partner, NULL, false);
	} else if($user['info_partner'] != 0) { $partner = profile :: getUser($user['info_partner'], NULL, false); }

	$bday_m = array(
		1=>	"Января",
		"Февраля",
		"Марта",
		"Апреля",
		"Мая",
		"Июня",
		"Июля",
		"Августа",
		"Сентября",
		"Октября",
		"Ноября",
		"Декабря"
	);
	$y = (date("Y") + 1); 


	if(isset($_POST['cfms'])) {
		if($act == 'personal') { 
			$politikal = intval($_POST['politikal']);
			$politikal = (isset($info_politikal[$politikal]) ? $politikal : 0);

			$religion = intval($_POST['religion']);
			$religion = (isset($info_religion[$religion]) ? $religion : 0);

			$smoking = intval($_POST['smoking']);
			$smoking = (isset($info_smoking_attitude[$smoking]) ? $smoking : 0);

			$drinking = intval($_POST['drinking']);
			$drinking = (isset($info_drinking_attitude[$drinking]) ? $drinking : 0);

			$upd = "`info_politikal`=?,`info_religion`=?,`info_smoking_attitude`=?,`info_drinking_attitude`=?";
			$upd_arr = [$politikal,$religion,$smoking,$drinking];
			DB :: $pdo -> query("UPDATE `users` SET {$upd} WHERE `id` = '{$user['id']}' LIMIT 1;", $upd_arr);
			system :: redirect("Изменения успешно сохранены.",$thisPage."?act=anketa/edit&sel=personal");
		} else if($act == 'interests') { 
			$interes = system :: check(mb_substr(system :: text($_POST['interes']), 0, 500,'UTF-8'));
			$music = system :: check(mb_substr(system :: text($_POST['music']), 0, 500,'UTF-8'));
			$kino = system :: check(mb_substr(system :: text($_POST['kino']), 0, 500,'UTF-8'));
			$books = system :: check(mb_substr(system :: text($_POST['books']), 0, 500,'UTF-8'));
			$quotes = system :: check(mb_substr(system :: text($_POST['quotes']), 0, 500,'UTF-8'));

			$upd = "`info_interes`=?,`info_love_music`=?,`info_love_kino`=?,`info_love_books`=?,`info_love_quotes`=?";
			$upd_arr = [$interes,$music,$kino,$books,$quotes];
			DB :: $pdo -> query("UPDATE `users` SET {$upd} WHERE `id` = '{$user['id']}' LIMIT 1;", $upd_arr);
			system :: redirect("Изменения успешно сохранены.",$thisPage."?act=anketa/edit&sel=interests");
		} else { 
			$sex = ( in_array(intval($_POST['sex']), array(0,1)) ? intval($_POST['sex']) : 0 );
			$first_name = system :: check(system :: text($_POST['first_name']));
			$last_name = system :: check(system :: text($_POST['last_name']));
			$str_last = system :: utf_strlen($last_name);
			$str_first = system :: utf_strlen($first_name);
			$hometown = system :: check(system :: text($_POST['hometown']));
			$aboutme = system :: check(system :: text(substr($_POST['aboutme'], 0, 200)));
			$rs = intval($_POST['rs']);
			if($rs < 0 && $rs > 7){ $rs = 0; }
			
			$info_partner = ( isset($partner['id']) && $rs >= 3 && $rs <= 7 && $partner['id'] != $user['id'] ? $partner['id'] : 0 );


			$bday = intval($_POST['bday']);
			$bday_mm = intval($_POST['bday-m']);
			$bday_yy = intval($_POST['bday-y']);
			$bday_h = (isset($_POST['bday_h']) ? 1 : 0);
			if($str_first >= 3 && $str_first <= 25) {
            if($str_last >= 3 && $str_last <= 25) {
            if (preg_match('|^[а-яёa-z]+$|iu', $first_name)) {  
            if (preg_match('|^[а-яёa-z]+$|iu', $last_name)) {       
			$upd = "`info_sex` = ?, `info_hometown` = ?, `info_aboutme` = ?, `info_b_hide` = ?, `info_first_name` = ?, `info_last_name` = ?, `info_rs` = ?, `info_partner` = ?";
			$upd_arr = array($sex,$hometown,$aboutme,$bday_h,$first_name,$last_name,$rs,$info_partner);

			if(checkdate($bday_mm,$bday,$bday_yy)) {
				$yt = false;
				if($bday_yy >= ($y-75) && $bday_yy < ($y-7)){ $yt = true; }

				$upd .= ", `info_b_d` = ?, `info_b_m` = ?".($yt ? ", `info_b_y` = ?" : '' );
				$upd_arr[] = $bday;
				$upd_arr[] = $bday_mm;
				if($yt){ $upd_arr[] = $bday_yy; }

			}
			
			//if($user['sex'] != $sex || $user['info_hometown'] != $hometown) {$dbgo = true;}
			DB :: $pdo -> query("UPDATE `users` SET {$upd} WHERE `id` = '{$user['id']}' LIMIT 1;", $upd_arr);
			system :: redirect("Изменения успешно сохранены.",$thisPage."?act=anketa/edit");
            }else { $e = "В Фамилии разрешены только латинцкие буквы и русские ."; }
            }else { $e = "В имени разрешины только латинцкие буквы и русские ."; }
            }else { $e = "Фамилия должна состоять от 3-х до 25-ти символов."; }
			}else { $e = "Имя должно состоять от 3-х до 25-ти символов."; }}
		}

        $title = $l = 'Анкета - Редактирование';
	$lBack = "/anketa?id={$user['id']}";
	
	require (CORE."head.php");
if(isset($e)){ system :: show($e); } #Вывод
	?>
	<div class="app-block">
	<form method="post" action="">
				<div class="list tab">
		        <a class="<?=(!in_array($act, ['personal','interests']) ? "tab_active" : '' )?>" href="<?=$thisLink?>">Основное</a>
		        <a class="<?=($act == 'interests' ? "tab_active" : '' )?>" href="<?=$thisLink?>&amp;sel=interests">Интересы</a>
		        <a class="<?=($act == 'personal' ? "tab_active" : '' )?>" href="<?=$thisLink?>&amp;sel=personal">Жизненная позиция</a>
		    </div>
	<div class="list">
		<div class="app-block-w">

			<?php if($act == 'personal') { ?>
			<div class="_m-top">
			<label class="_gray _dbl">Политические взгляды:</label>
			<select name="politikal" class="_dbl _w-100" style="padding: 0 5px;">
			<?php foreach($info_politikal as $k => $v) { ?>
				<option value="<?=$k;?>" <?=($k == $user['info_politikal'] ? "selected" : null);?>>&nbsp;&nbsp;<?=$v;?>&nbsp;&nbsp;</option> 
			<?php } ?>
			</select>
			</div>
			<div class="_m-top">
			<label class="_gray _dbl">Религиозные взгляды:</label>
			<select name="religion" class="_dbl _w-100" style="padding: 0 5px;">
			<?php foreach($info_religion as $k => $v) { ?>
				<option value="<?=$k;?>" <?=($k == $user['info_religion'] ? "selected" : null);?>>&nbsp;&nbsp;<?=$v;?>&nbsp;&nbsp;</option>
			<?php } ?>
			</select>
			</div>
			<div class="_m-top">
			<label class="_gray _dbl">Отношение к курению:</label>
			<select name="smoking" class="_dbl _w-100" style="padding: 0 5px;">
			<?php foreach($info_smoking_attitude as $k => $v) { ?>
				<option value="<?=$k;?>" <?=($k == $user['info_smoking_attitude'] ? "selected" : null);?>>&nbsp;&nbsp;<?=$v;?>&nbsp;&nbsp;</option>
			<?php } ?>
			</select>
			</div>
			<div class="_m-top">
			<label class="_gray _dbl">Отношение к алкоголю:</label>
			<select name="drinking" class="_dbl _w-100" style="padding: 0 5px;">
			<?php foreach($info_drinking_attitude as $k => $v) { ?>
				<option value="<?=$k;?>" <?=($k == $user['info_drinking_attitude'] ? "selected" : null);?>>&nbsp;&nbsp;<?=$v;?>&nbsp;&nbsp;</option>
			<?php } ?>
			</select>
			</div>
			<?php } else if($act == 'interests') { ?>
			<div class="_m-top">
			<label class="_gray _dbl" for="index-interes">Интересы:</label>
			<textarea class="main-input _w-100" name="interes" id="index-interes"><?=$user['info_interes']?></textarea>
			</div>
			<div class="_m-top">
			<label class="_gray _dbl" for="index-music">Любимая музыка:</label>
			<textarea class="main-input _w-100" name="music" id="index-music"><?=$user['info_love_music']?></textarea>
			</div>
			<div class="_m-top">
			<label class="_gray _dbl" for="index-kino">Любимые фильмы:</label>
			<textarea class="main-input _w-100" name="kino" id="index-kino"><?=$user['info_love_kino']?></textarea>
			</div>
			<div class="_m-top">
			<label class="_gray _dbl" for="index-books">Любимые книги:</label>
			<textarea class="main-input _w-100" name="books" id="index-books"><?=$user['info_love_books']?></textarea>
			</div>
			<div class="_m-top">
			<label class="_gray _dbl" for="index-quotes">Любимые цитаты:</label>
			<textarea class="main-input _w-100" name="quotes" id="index-quotes"><?=$user['info_love_quotes']?></textarea>
			</div>
			<?php } else { ?>

			<div>
			<label class="_gray _dbl" for="first_name">Имя:</label>
			<input type="text" class="main-input _w-100" name="first_name" value="<?=@$user['info_first_name']?>" id="first_name">
			</div>
			<div class="_m-top">
			<label class="_gray _dbl" for="last_name">Фамилия:</label>
			<input type="text" class="main-input _w-100" name="last_name" value="<?=@$user['info_last_name']?>" id="last_name">
			</div>
			<div class="_m-top">
			<label class="_gray _dbl">Пол:</label>
			<input name="sex" id="sex_1" class="radio" type="radio"<?=($user['info_sex'] == 1 ? " checked=\"checked\"" : '' )?> value="1"> <label for="sex_1" class="_gray">Мужской</label> &nbsp;&nbsp; <input name="sex" id="sex_0" class="radio" type="radio"<?=($user['info_sex'] == 0 ? " checked=\"checked\"" : '' )?> value="0"> <label for="sex_0" class="_gray">Женский</label>
			</div>
			<div class="_m-top">
			<label class="_gray _dbl" for="index-hometown">Родной город:</label>
			<input type="text" class="main-input _w-100" name="hometown" value="<?=@$user['info_hometown']?>" id="index-hometown">
			</div>
		

		<?php
			if(isset($rs_status) && $rs_status >= 0 && $rs_status <= 7){ $user['info_rs'] = $rs_status; }
		?>
			<div class="_m-top">
			<label class="_gray _dbl">Семейное положение:</label>
			<select name="rs" class="_dbl _w-100" onchange="Linesoc.StatusSelect(this);">
			<option value="0" <?=(0 == $user['info_rs'] ? "selected" : null)?>>&nbsp;&nbsp;Не показывать&nbsp;&nbsp;</option>
			<option value="1" <?=(1 == $user['info_rs'] ? "selected" : null)?>>&nbsp;&nbsp;В активном поиске&nbsp;&nbsp;</option>
			<option value="2" <?=(2 == $user['info_rs'] ? "selected" : null)?>>&nbsp;&nbsp;Не <?=($user['info_sex'] == 1 ? "женат" : "замужем")?>&nbsp;&nbsp;</option>
			<option value="3" <?=(3 == $user['info_rs'] ? "selected" : null)?>>&nbsp;&nbsp;Встречаюсь&nbsp;&nbsp;</option>
			<option value="4" <?=(4 == $user['info_rs'] ? "selected" : null)?>>&nbsp;&nbsp;Помолвлен<?=($user['info_sex'] == 0 ? "а" : "")?>&nbsp;&nbsp;</option>
			<option value="5" <?=(5 == $user['info_rs'] ? "selected" : null)?>>&nbsp;&nbsp;<?=($user['info_sex'] == 1 ? "Женат" : "Замужем")?>&nbsp;&nbsp;</option>
			<option value="6" <?=(6 == $user['info_rs'] ? "selected" : null)?>>&nbsp;&nbsp;Влюблен<?=($user['info_sex'] == 0 ? "а" : "")?>&nbsp;&nbsp;</option>
			<option value="7" <?=(7 == $user['info_rs'] ? "selected" : null)?>>&nbsp;&nbsp;Всё сложно&nbsp;&nbsp;</option>
			</select>
			<div id="current_partner">
				<input type="hidden" id="curr_par" name="partner" value="<?=(isset($partner['id']) ? $partner['id'] : 0)?>">
				<span class="_gray"><?=(isset($partner['id']) ? ( $partner['info_partner'] == $user['id'] ? "В отношениях с " : "Ожидайте подтверждения от " )." ". profile :: goPage($partner, false) : '' )?></span>
			</div>
			<div id="select_partner" style="<?=($user['info_rs'] < 3 || $user['info_rs'] > 7 ? "display: none;" : '' )?>">
				<a href="/anketa?act=anketa/edit&amp;select_partner=1&amp;status=<?=$user['info_rs']?>" style="padding: 7px;" class="_action-link">Изменить партнёра</a>
			</div>
			</div>
			<div class="_m-top">
			<label class="_gray _dbl">Дата рождения:</label>
			<select name="bday" style="padding: 0 5px;">
			<?php for ($i = 1; $i < 32; ++$i) { ?><option value="<?=$i;?>" <?=($i == $user['info_b_d'] ? "selected" : null);?>>&nbsp;&nbsp;<?=$i;?>&nbsp;&nbsp;</option> <?php } ?>
			</select>
			
			<select name="bday-m" style="padding: 0 5px;">
			<?php for ($i = 1; $i < 13; ++$i) { ?><option value="<?=$i;?>" <?=($i == $user['info_b_m'] ? "selected" : null);?>><?=$bday_m[$i]?></option><?php } ?>
			</select>
			
			<select name="bday-y" style="padding: 0 5px;">
	<?php for ($i = ($y-75); $i < ($y-7); ++$i) { ?><option value="<?=($i)?>" <?=($i == $user['info_b_y'] ? "selected" : null);?>>&nbsp;<?=($i)?>&nbsp;</option><?php } ?>
			</select>
			
			
			<label for="date_hide" class="_gray _dbl"><input id="date_hide" class="checkbo" type="checkbox" name="bday_h" <?=($user['info_b_hide'] == 1 ? "checked=\"checked\"" : '' )?> value="1" /> Показывать дату на личной странице</label>
			</div>
			<div class="_m-top">
			<label class="_gray _dbl" for="index-aboutme">О себе:</label>
			<textarea class="main-input _w-100" name="aboutme" id="index-aboutme"><?=@$user['info_aboutme']?></textarea>
			</div>
			<?php } ?>
		<div class="_center _m-top">
			<input type="submit" class="main-btn _w-100" name="cfms" value="Сохранить">
		</div>
		</div></div>
	</form>
	<?=system :: back($thisPage."?id={$user['id']}", "Анкета", true)?>
	</div>
	<?php

	require (CORE."foot.php");