<?php
$shop = $shop ?? null;
$currentLg = $currentLg ?? null;
if ($shop === null) {
    return;
}
$v = fn ($key, $default = '') => old($key) !== null && old($key) !== '' ? old($key) : ($shop->{$key} ?? $default);
$vaAccountDefault = (isset($shop->ds_va_account) && (string) $shop->ds_va_account !== '')
    ? (string) $shop->ds_va_account
    : (string) ($shop->ds_va_number ?? '');
$dateField = static function (string $key) use ($shop, $v): string {
    $s = (string) $v($key);
    if ($s === '' || str_starts_with($s, '0000')) {
        return '';
    }

    return $s;
};
?>
<section class="border-b border-gray-300 p-2 shrink-0 bg-control-panel">
  <span class="text-sm font-bold text-gray-700">지정판매소 수정</span>
</section>
<div class="border border-gray-300 rounded-lg p-4 mt-2 bg-white max-w-3xl">
  <form id="designated-shop-edit-form" action="<?= mgmt_url('designated-shops/update/' . (int) $shop->ds_idx) ?>" method="POST" class="space-y-4">
    <?= csrf_field() ?>

    <input type="hidden" name="addr_search_sido" id="addr_search_sido" value="<?= esc((string) old('addr_search_sido', $currentLg !== null ? (string) ($currentLg->lg_sido ?? '') : '')) ?>"/>
    <input type="hidden" name="addr_search_sigungu" id="addr_search_sigungu" value="<?= esc((string) old('addr_search_sigungu', $currentLg !== null ? (string) ($currentLg->lg_gugun ?? '') : '')) ?>"/>

    <?php if ($currentLg !== null): ?>
    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">지자체</label>
      <div class="text-sm">
        <span class="font-semibold"><?= esc($currentLg->lg_name) ?></span>
        <span class="text-gray-500 ml-2">(<?= esc($currentLg->lg_code) ?>)</span>
        <span class="text-gray-500 ml-1"><?= esc(trim((string) ($currentLg->lg_sido ?? '') . ' ' . (string) ($currentLg->lg_gugun ?? ''))) ?></span>
      </div>
    </div>
    <?php endif; ?>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">판매소번호</label>
      <div class="text-sm font-mono"><?= esc($shop->ds_shop_no) ?></div>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">구코드</label>
      <div class="text-sm font-mono"><?= esc($shop->ds_gugun_code) ?></div>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">상호명 <span class="text-red-500">*</span></label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-72" name="ds_name" type="text" value="<?= esc($v('ds_name')) ?>" required/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">사업자번호 <span class="text-red-500">*</span></label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-48" name="ds_biz_no" type="text" value="<?= esc($v('ds_biz_no')) ?>" required/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">대표자명 <span class="text-red-500">*</span></label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-48" name="ds_rep_name" type="text" value="<?= esc($v('ds_rep_name')) ?>" required/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">업태</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-48" name="ds_biz_type" type="text" value="<?= esc($v('ds_biz_type')) ?>"/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">업종</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-48" name="ds_biz_kind" type="text" value="<?= esc($v('ds_biz_kind')) ?>"/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">가상계좌(은행)</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-48" name="ds_va_bank" type="text" value="<?= esc($v('ds_va_bank')) ?>"/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">계좌번호</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-60" name="ds_va_account" type="text" value="<?= esc((old('ds_va_account') !== null && old('ds_va_account') !== '') ? old('ds_va_account') : $vaAccountDefault) ?>"/>
    </div>

    <div class="flex flex-wrap items-start gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28 pt-1.5">주소</label>
      <p class="text-xs text-gray-600 max-w-lg leading-relaxed">우편·도로명·지번은 <strong>주소 검색</strong>으로만 바꿀 수 있습니다(직접 입력 불가). <strong>지도</strong>는 카카오맵에서 현재 입력된 주소를 검색해 엽니다. 관할이 아니면 검색 직후 안내 후 반영되지 않습니다. 동·호 등은 <strong>상세주소</strong>에 입력하세요.</p>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">우편번호</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-32 bg-gray-100 text-gray-800 cursor-not-allowed" name="ds_zip" id="ds_zip" type="text" value="<?= esc($v('ds_zip')) ?>" maxlength="10" readonly tabindex="-1"/>
      <button type="button" id="btn-ds-kakao-postcode-edit" class="no-print border border-btn-print-border text-gray-700 px-3 py-1.5 rounded-sm text-sm hover:bg-gray-50 transition shrink-0">주소 검색</button>
      <?= view('components/kakao_map_link_button', ['buttonId' => 'btn-ds-kakao-map-edit']) ?>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">도로명주소</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-96 max-w-full bg-gray-100 text-gray-800 cursor-not-allowed" name="ds_addr" id="ds_addr" type="text" value="<?= esc($v('ds_addr')) ?>" readonly tabindex="-1"/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">지번주소</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-96 max-w-full bg-gray-100 text-gray-800 cursor-not-allowed" name="ds_addr_jibun" id="ds_addr_jibun" type="text" value="<?= esc($v('ds_addr_jibun')) ?>" readonly tabindex="-1"/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">상세주소</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-96 max-w-full" name="ds_addr_detail" id="ds_addr_detail" type="text" value="<?= esc($v('ds_addr_detail')) ?>" maxlength="200" placeholder="동·호수·층 등"/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">일반전화</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-48" name="ds_tel" type="text" value="<?= esc($v('ds_tel')) ?>"/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">대표 휴대전화</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-48" name="ds_rep_phone" type="text" value="<?= esc($v('ds_rep_phone')) ?>"/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">이메일</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-60" name="ds_email" type="email" value="<?= esc($v('ds_email')) ?>"/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">구역</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-72" name="ds_zone_code" type="text" value="<?= esc($v('ds_zone_code')) ?>"/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">종사업장번호</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-48" name="ds_branch_no" type="text" value="<?= esc($v('ds_branch_no')) ?>"/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">지정일자</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-40" name="ds_designated_at" type="date" value="<?= esc($v('ds_designated_at')) ?>"/>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">영업상태</label>
      <select class="border border-gray-300 rounded px-3 py-1.5 text-sm w-40" name="ds_state">
        <option value="1" <?= (string) $v('ds_state', '1') === '1' ? 'selected' : '' ?>>정상</option>
        <option value="2" <?= (string) $v('ds_state', '1') === '2' ? 'selected' : '' ?>>폐업</option>
        <option value="3" <?= (string) $v('ds_state', '1') === '3' ? 'selected' : '' ?>>직권해지</option>
      </select>
    </div>

    <div class="flex flex-wrap items-center gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28">변경일자</label>
      <input class="border border-gray-300 rounded px-3 py-1.5 text-sm w-40" name="ds_state_changed_at" type="date" value="<?= esc($dateField('ds_state_changed_at')) ?>"/>
    </div>

    <div class="flex flex-wrap items-start gap-2">
      <label class="block text-sm font-bold text-gray-700 w-28 pt-1.5">변경사유</label>
      <textarea class="border border-gray-300 rounded px-3 py-1.5 text-sm w-96 min-h-[4rem]" name="ds_change_reason" rows="3"><?= esc($v('ds_change_reason')) ?></textarea>
    </div>

    <div class="flex gap-2 pt-2">
      <button type="submit" class="bg-btn-search text-white px-4 py-1.5 rounded-sm text-sm shadow hover:opacity-90 transition">수정</button>
      <a href="<?= mgmt_url('designated-shops') ?>" class="bg-white text-black border border-btn-print-border px-4 py-1.5 rounded-sm text-sm shadow hover:bg-gray-50 transition">목록</a>
    </div>
  </form>
</div>

<?= view('components/kakao_map_modal', ['kakaoJavascriptKey' => $kakaoJavascriptKey ?? '']) ?>

<?= view('components/kakao_address_search', [
    'buttonId'         => 'btn-ds-kakao-postcode-edit',
    'zipName'          => 'ds_zip',
    'roadName'         => 'ds_addr',
    'jibunName'        => 'ds_addr_jibun',
    'sidoFieldName'    => 'addr_search_sido',
    'sigunguFieldName' => 'addr_search_sigungu',
    'tenantScope'      => $addrTenantScope ?? ['lg_sido' => '', 'lg_gugun' => ''],
    'roadBaseOnly'     => true,
    'detailFieldName'  => 'ds_addr_detail',
]) ?>
