<?php
require_once('function.php');
$url = isset($_GET['url']) ? $_GET['url'] : '';
$segments = explode('/', $url);
$id = get_id($segments[3]);
$query =  $db->table('languages')->where('lang_id', '=', $id)->get();
$language = $query['data'][0];

if ($_POST) {
	$csrf_token = csrf_token($_POST['csrf_token']);
	if ($csrf_token[0]) {
		$lang_id = m_u_p('lang_id');
		$lang_name = m_u_p('lang_name');
		$lang_code = m_u_p('lang_code');
		$lang_locale = m_u_p('lang_locale');
		$lang_flag = m_u_p('lang_flag');
		$lang_order = m_u_p('lang_order');
		$lang_is_rtl = m_u_p('lang_is_rtl');
		$lang_is_default = m_u_p('lang_is_default');
		if (isset($_POST['lang_is_default']) && m_clean_input($_POST['lang_is_default'], 'string') === '1'){
			$db->table('languages')->update(['lang_is_default' => '0']);
		}
		$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,
		];
		$query = $db->table('languages')->where('lang_id','=',$lang_id)->update($data);
		if($query):
			$_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');
		endif;
	} else {
		$_SESSION['formSuccess'] = $csrf_token[0];
		$_SESSION['error_message'] = $csrf_token[1];
	}
}

?>
<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-control input-group-flat">
										<option value="<?= $language['lang_code'] ?>"><?=
											$language['lang_name']
										?></option>
									</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" value="<?=$language['lang_name']?>">

									</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" value="<?=$language['lang_code']?>">

									</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" value="<?=$language['lang_locale']?>">

									</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>
									<div id="edit_flag" data-langcode="<?= $language['lang_code'] ?>" data-langname="<?= $language['lang_name'] ?>"></div>
									<select id="imageSelect" class="form-control input-group-flat" name="lang_flag">

										<?php

										foreach (languages as $key => $languagelist) {
											?>
											<option value="<?= $languagelist[4] ?>"  data-image="<?= ADMIN_THEME_URL ?>/model/languages/flags/<?= $languagelist[4] ?>.svg"> <?= $languagelist[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('form_input_lang_order', [], 'Sıra') ?></label>
										<input type="text" name="lang_order" class="form-control input-group-flat"
											   value="<?=$language['lang_order']?>" id="lang_order">
									</div>
								</div>
							</div>
						</div>

						<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']['value']; ?>">
						<input type="hidden" name="lang_id" value="<?php echo $language['lang_id'] ?>">


						<!--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') ?>  <?=$language['lang_name']?></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"
										   value="0" <?php if ($language['lang_is_rtl'] == 0):?> checked="checked" <?php endif;?> >
									<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"  <?php if ($language['lang_is_rtl'] == 1):?> checked="checked" <?php endif;?> >
									<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" <?php if ($language['lang_is_default'] == 1):?> checked="checked" <?php endif;?>>
							<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>

