<?php require_once('function.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	$csrf_token = csrf_token($_POST['csrf_token']);
	$is_default = lang_is_default('lang_code');
	if ($csrf_token[0]) {
		$lang_name   = trim(m_u_p('lang_name'));
		$lang_code   = strtolower(trim(m_u_p('lang_code')));
		$lang_locale = trim(m_u_p('lang_locale'));
		$lang_flag   = trim(m_u_p('lang_flag'));
		$lang_order  = is_numeric(m_u_p('lang_order')) ? m_u_p('lang_order') : 0;
		$lang_is_rtl = m_u_p('lang_is_rtl') === '1' ? 1 : 0;
		$lang_is_default = isset($_POST['lang_is_default']) && m_clean_input($_POST['lang_is_default'], 'string') === '1' ? 1 : 0;

		// Varsayılan dil seçildiyse mevcutları sıfırla
		if ($lang_is_default === 1){
			$db->table('languages')->update(['lang_is_default' => '0']);
		}

		// Basit doğrulama
		$errors = [];
		if ($lang_name === '') $errors[] = lang('lang_error_name_required', [], 'Dil adı gereklidir.');
		if ($lang_code === '' || !preg_match('/^[a-z]{2}(-[A-Z]{2})?$/', $lang_code)) $errors[] = lang('lang_error_code_invalid', [], 'Dil kodu geçersiz (ör. tr veya en-US).');
		if ($lang_locale === '') $errors[] = lang('lang_error_locale_required', [], 'Locale gereklidir (ör. tr_TR).');
		if ($lang_flag === '0' || $lang_flag === '') $errors[] = lang('lang_error_flag_required', [], 'Bayrak seçiniz.');

		$data =[
			'lang_name'       => $lang_name,
			'lang_locale'     => $lang_locale,
			'lang_code'       => $lang_code,
			'lang_flag'       => $lang_flag,
			'lang_is_default' => $lang_is_default,
			'lang_order'      => $lang_order,
			'lang_is_rtl'     => $lang_is_rtl,
		];
		$result  = $db->table('languages')->where('lang_code', '=', $lang_code)->get();


	  if (empty($errors)){
		  if ($result['total_count'] != 0) {
			  $_SESSION['formSuccess'] = false;
			  $_SESSION['error_message'] = lang('form_error_language_repeat', [], 'Bu dil zaten ekli.');
		  } else {
			  $query = $db->table('languages')->insert($data);
			  if ($query) {
				  $posts = posts('data','all', $is_default);
				  foreach ($posts as $post){
					  $data=[
						  'lang'=> $lang_code,
						  'lang_ref_id'=> $post['lang_ref_id'],
						  'module_type'=> $post['module_type'],
						  'title'=> $post['title'],
						  'description'=> $post['description'],
						  'content'=> $post['content'],
						  'image'=> $post['image'],
						  'images'=> $post['images'],
						  'sef'=> $post['sef'].'-'.$lang_code,
						  'seo_title'=> $post['seo_title'],
						  'seo_description'=> $post['seo_description'],
						  'seo_keywords'=> $post['seo_keywords'],
						  'status'=> $post['status'],
						  'sort'=> $post['sort']
					  ];
					  $postsquery = $db->table('posts')->insert($data);

				  }
				  $_SESSION['formSuccess'] = true;
				  $_SESSION['success_message'] = lang('form_success_message');
				  m_redirect(ADMIN_URL.'/languages/lists');
			  } else {
				  $_SESSION['formSuccess'] = false;
				  $_SESSION['error_message'] = lang('form_error_message');
			  }
		  }
	  }else{
		  $_SESSION['formSuccess'] = false;
		  $_SESSION['error_message'] = implode(' ', $errors);
	  }
	} else {
		$_SESSION['formSuccess'] = false;
		$_SESSION['error_message'] = lang('form_token_error');
	}
}
?>

<form action="" class="needs-validation" novalidate method="POST">
	<div class="row">
		<div class="col-xxl-10">
			<div class="card">
				<div class="card-header align-items-center d-flex">
					<h4 class="card-title mb-0 flex-grow-1 text-muted"><?= lang('form_detail') ?></h4>
				</div><!-- end card header -->

				<div class="card-body">
					<div class="live-preview">
						<div class="row">
							<div class="col-md-12">
								<div class="mb-3 input-group-v">
									<label class="form-label" for="lang_select"><?= lang('form_input_lang_select', [], 'Dil Seç') ?></label>
									<select id="lang_select" class="form-select input-group-flat" > </select>
								</div>
							</div>
							<!--end col-->
							<div class="col-md-12">
								<div class="mb-3">
									<div class="mb-3 input-group-v ">
										<label for="lang_name" class="form-label"><?= lang('form_input_lang_name', [], 'Dil Adı') ?></label>
										<input type="text" name="lang_name" class="form-control input-group-flat"
											   id="lang_name" required>
										<div class="invalid-feedback">
											<?= lang('form_required', [], 'Zorunlu alan') ?>
										</div>
									</div>
								</div>
							</div>
							<div class="col-md-12">
								<div class="mb-3">
									<div class="mb-3 input-group-v ">
										<label for="lang_code" class="form-label"><?= lang('form_input_lang_code', [], 'Dil Kodu') ?></label>
										<input type="text" name="lang_code" class="form-control input-group-flat"
											   id="lang_code" required>
										<div class="invalid-feedback">
											<?= lang('form_required', [], 'Zorunlu alan') ?>
										</div>

									</div>
								</div>
							</div>
							<div class="col-md-12">
								<div class="mb-3">
									<div class="mb-3 input-group-v ">
										<label for="lang_locale" class="form-label"><?= lang('form_input_lang_locale', [], 'Yerel (Locale)') ?></label>
										<input type="text" name="lang_locale" class="form-control input-group-flat"
											   id="lang_locale" required>
										<div class="invalid-feedback">
											<?= lang('form_required', [], 'Zorunlu alan') ?>
										</div>

									</div>
								</div>
							</div>
							<div class="col-md-12">
								<div class="mb-3 input-group-v">

									<label class="form-label"><?= lang('form_input_lang_flags', [], 'Bayrak') ?></label>
									<select id="imageSelect" class="form-control input-group-flat" name="lang_flag">
										<option value="0"><?= lang('form_input_lang_flags_select', [], 'Bayrak seçiniz') ?></option>
										<?php
										foreach (languages as $key => $language) {
											?>
											<option value="<?= $language[4] ?>"
													data-image="<?= ADMIN_FOLDER ?>/model/languages/flags/<?= $language[4] ?>.svg"> <?= $language[2] ?> </option>
										<?php } ?>
									</select>
								</div>
							</div>

							<div class="col-md-12">
								<div class="mb-3">
									<div class="mb-3 input-group-v ">
										<label for="lang_order" class="form-label"><?= lang('table_order', [], 'Sıra') ?></label>
										<input type="text" name="lang_order" class="form-control input-group-flat"
											   value="0" id="lang_order">
									</div>
								</div>
							</div>
						</div>
						<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']['value']; ?>">


						<!--end row-->

					</div>
				</div>
			</div>
		</div>

		<div class="col-xxl-2">
			<div class="card card-body">
				<h4 class="card-title  flex-grow-1 text-muted"><?= lang('published') ?></h4>
				<hr>
				<div class="saved">
					<button class="btn btn-light" type="submit" name="submitter" value="save">
				<span class="icon-tabler-wrapper icon-left">
					<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-transfer-in" width="24"
						 height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
						 stroke-linecap="round" stroke-linejoin="round">
					  <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
					  <path d="M4 18v3h16v-14l-8 -4l-8 4v3"></path>
					  <path d="M4 14h9"></path>
					  <path d="M10 11l3 3l-3 3"></path>
					</svg>
				</span>
					   <?= lang('save') ?>
					</button>
				</div>
			</div>
			<div class="card card-body">
				<h4 class="card-title  flex-grow-1 text-muted"><?= lang('menu_settings', [], 'Menü Ayarları') ?></h4>
				<hr>
				<div class="col-md-12">
					<div class="mb-3">
						<div class="mb-3 input-group-v ">
							<p class="fw-medium"><?= lang('form_input_lang_text_aling', [], 'Metin Hizalama') ?></p>

							<div style="display: flex">
								<div class="form-check mb-2" style="padding-right: 15px">
									<input class="form-check-input" type="radio" name="lang_is_rtl" id="lang_ltr"
										   checked="" value="0">
									<label class="form-check-label" for="lang_ltr">
										<?= lang('form_input_lang_aling_left', [], 'Sola Hizalı') ?>
									</label>
								</div>
								<div class="form-check">
									<input class="form-check-input" type="radio" name="lang_is_rtl" id="lang_rtl"
										   value="1">
									<label class="form-check-label" for="lang_rtl">
										<?= lang('form_input_lang_aling_right', [], 'Sağa Hizalı') ?>
									</label>
								</div>
							</div>
						</div>
					</div>
				</div>

				<div class="col-md-12">
					<div class="mb-3">
						<div class="form-check form-switch form-switch-md mb-3" dir="ltr">
							<input type="checkbox" class="form-check-input" value="1" id="lang_is_default" name="lang_is_default">
							<label class="form-check-label"
								   for="lang_is_default"><?= lang('form_input_lang_is_default', [], 'Varsayılan dil olsun') ?></label>
						</div>

					</div>
				</div>

			</div>
		</div> <!-- end col -->
	</div>


</form>

