@extends('layouts.master')

@section('content')
<div style="max-width: 850px; margin: 0 auto;">
    <h3 style="font-weight: 800; margin-bottom: 5px;">إعدادات صفحة إعادة البيع</h3>
    <p style="color: #666;">تعديل معلومات وإعدادات صفحة رخصة إعادة البيع والتوزيع</p>
    <hr>

    @include('layouts.errors')
    <div id="flash">
        @if(session('flash_notification'))
            @foreach(session('flash_notification') as $msg)
                <div class="alert alert-{{ $msg->level }}">{{ $msg->message }}</div>
            @endforeach
        @endif
    </div>

    <div style="text-align: center; margin-bottom: 1.5rem; display: flex; gap: 10px; justify-content: center; flex-wrap: wrap;">
        <a href="/admin/product-settings" class="btn btn-primary">إعدادات النسخة العادية</a>
        <a href="/admin/licenses" class="btn btn-primary">إدارة التراخيص</a>
        <a href="/reseller" target="_blank" class="btn btn-success">معاينة صفحة إعادة البيع</a>
    </div>

    <form method="POST" action="/admin/reseller-settings/update">
        @csrf

        {{-- Basic Info --}}
        <div style="background: #bbbbd0; border: 2px solid #9999CC; padding: 1.5rem; margin-bottom: 1.5rem;">
            <h5 style="color: #7777CC; font-weight: 700; margin-bottom: 1rem;">الإعدادات الأساسية</h5>

            <div class="form-group">
                <label>تفعيل صفحة إعادة البيع:</label>
                <select name="reseller_page_enabled" class="form-control" style="max-width: 300px;">
                    <option value="1" {{ ($settings['reseller_page_enabled'] ?? '0') == '1' ? 'selected' : '' }}>مفعّلة (ظاهرة)</option>
                    <option value="0" {{ ($settings['reseller_page_enabled'] ?? '0') == '0' ? 'selected' : '' }}>معطّلة (مخفية)</option>
                </select>
            </div>

            <div class="form-group">
                <label>شارة الصفحة (Badge):</label>
                <input type="text" class="form-control" name="reseller_badge" value="{{ $settings['reseller_badge'] ?? 'RESELLER LICENSE' }}" style="max-width: 300px; direction: ltr;">
            </div>

            <div class="form-group">
                <label>عنوان الصفحة:</label>
                <input type="text" class="form-control" name="reseller_title" value="{{ $settings['reseller_title'] ?? '' }}">
            </div>

            <div class="form-group">
                <label>الوصف المختصر:</label>
                <textarea class="form-control" name="reseller_subtitle" rows="2">{{ $settings['reseller_subtitle'] ?? '' }}</textarea>
            </div>

            <div style="display: flex; gap: 15px;">
                <div class="form-group" style="flex: 1;">
                    <label>السعر:</label>
                    <input type="text" class="form-control" name="reseller_price" value="{{ $settings['reseller_price'] ?? '' }}" placeholder="499$">
                </div>
                <div class="form-group" style="flex: 1;">
                    <label>السعر القديم (مشطوب):</label>
                    <input type="text" class="form-control" name="reseller_old_price" value="{{ $settings['reseller_old_price'] ?? '' }}" placeholder="799$">
                </div>
            </div>

            <div class="form-group">
                <label>ملاحظة السعر:</label>
                <input type="text" class="form-control" name="reseller_price_note" value="{{ $settings['reseller_price_note'] ?? '' }}" placeholder="دفعة واحدة - بدون اشتراكات">
            </div>
        </div>

        {{-- Features --}}
        <div style="background: #bbbbd0; border: 2px solid #9999CC; padding: 1.5rem; margin-bottom: 1.5rem;">
            <h5 style="color: #7777CC; font-weight: 700; margin-bottom: 1rem;">مميزات رخصة إعادة البيع</h5>

            <div class="form-group">
                <label>عنوان القسم:</label>
                <input type="text" class="form-control" name="reseller_why_title" value="{{ $settings['reseller_why_title'] ?? '' }}">
            </div>
            <div class="form-group">
                <label>وصف القسم:</label>
                <input type="text" class="form-control" name="reseller_why_desc" value="{{ $settings['reseller_why_desc'] ?? '' }}">
            </div>

            <div id="features-list">
                @forelse($resellerFeatures as $i => $f)
                <div class="feat-row" style="display: flex; gap: 8px; margin-bottom: 8px; align-items: center;">
                    <input type="text" name="feature_icons[]" class="form-control" value="{{ $f['icon'] ?? '' }}" placeholder="أيقونة" style="width: 70px; text-align: center;">
                    <input type="text" name="feature_titles[]" class="form-control" value="{{ $f['title'] }}" placeholder="عنوان الميزة" style="flex: 1;">
                    <input type="text" name="feature_descs[]" class="form-control" value="{{ $f['desc'] ?? '' }}" placeholder="وصف" style="flex: 2;">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @empty
                <div class="feat-row" style="display: flex; gap: 8px; margin-bottom: 8px; align-items: center;">
                    <input type="text" name="feature_icons[]" class="form-control" placeholder="أيقونة" style="width: 70px; text-align: center;">
                    <input type="text" name="feature_titles[]" class="form-control" placeholder="عنوان الميزة" style="flex: 1;">
                    <input type="text" name="feature_descs[]" class="form-control" placeholder="وصف" style="flex: 2;">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @endforelse
            </div>
            <button type="button" class="btn btn-sm btn-success" onclick="addTriple('features-list','feature_icons[]','أيقونة','feature_titles[]','عنوان','feature_descs[]','وصف')">+ إضافة ميزة</button>
        </div>

        {{-- Includes --}}
        <div style="background: #bbbbd0; border: 2px solid #9999CC; padding: 1.5rem; margin-bottom: 1.5rem;">
            <h5 style="color: #7777CC; font-weight: 700; margin-bottom: 1rem;">ماذا يحصل الموزع؟</h5>
            <div class="form-group">
                <label>عنوان القسم:</label>
                <input type="text" class="form-control" name="reseller_includes_title" value="{{ $settings['reseller_includes_title'] ?? '' }}">
            </div>
            <div id="includes-list">
                @php $rIncludes = json_decode(\App\SiteSetting::get('reseller_includes', '[]'), true) ?: []; @endphp
                @forelse($rIncludes as $item)
                <div style="display: flex; gap: 8px; margin-bottom: 6px; align-items: center;">
                    <input type="text" name="include_items[]" class="form-control" value="{{ $item }}">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @empty
                <div style="display: flex; gap: 8px; margin-bottom: 6px; align-items: center;">
                    <input type="text" name="include_items[]" class="form-control" placeholder="ميزة مشمولة">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @endforelse
            </div>
            <button type="button" class="btn btn-sm btn-success" onclick="addSingle('includes-list','include_items[]','ميزة مشمولة')">+ إضافة</button>
        </div>

        {{-- Comparison Table --}}
        <div style="background: #bbbbd0; border: 2px solid #9999CC; padding: 1.5rem; margin-bottom: 1.5rem;">
            <h5 style="color: #7777CC; font-weight: 700; margin-bottom: 1rem;">جدول المقارنة</h5>
            <div id="comp-list">
                @php $comps = json_decode(\App\SiteSetting::get('reseller_comparison', '[]'), true) ?: []; @endphp
                @forelse($comps as $comp)
                <div style="display: flex; gap: 8px; margin-bottom: 8px; align-items: center;">
                    <input type="text" name="comp_features[]" class="form-control" value="{{ $comp['feature'] }}" placeholder="الميزة" style="flex: 2;">
                    <input type="text" name="comp_normal[]" class="form-control" value="{{ $comp['normal'] }}" placeholder="العادية (✓/✗)" style="flex: 1;">
                    <input type="text" name="comp_reseller[]" class="form-control" value="{{ $comp['reseller'] }}" placeholder="الموزع (✓/✗)" style="flex: 1;">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @empty
                <div style="display: flex; gap: 8px; margin-bottom: 8px; align-items: center;">
                    <input type="text" name="comp_features[]" class="form-control" placeholder="الميزة" style="flex: 2;">
                    <input type="text" name="comp_normal[]" class="form-control" placeholder="العادية" style="flex: 1;">
                    <input type="text" name="comp_reseller[]" class="form-control" placeholder="الموزع" style="flex: 1;">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @endforelse
            </div>
            <button type="button" class="btn btn-sm btn-success" onclick="addComp()">+ إضافة صف</button>
        </div>

        {{-- Steps --}}
        <div style="background: #bbbbd0; border: 2px solid #9999CC; padding: 1.5rem; margin-bottom: 1.5rem;">
            <h5 style="color: #7777CC; font-weight: 700; margin-bottom: 1rem;">خطوات البدء كموزع</h5>
            <div class="form-group">
                <label>عنوان القسم:</label>
                <input type="text" class="form-control" name="reseller_steps_title" value="{{ $settings['reseller_steps_title'] ?? '' }}">
            </div>
            <div id="steps-list">
                @php $rSteps = json_decode(\App\SiteSetting::get('reseller_steps', '[]'), true) ?: []; @endphp
                @forelse($rSteps as $s)
                <div style="display: flex; gap: 8px; margin-bottom: 8px; align-items: center;">
                    <input type="text" name="step_titles[]" class="form-control" value="{{ $s['title'] }}" placeholder="عنوان" style="flex: 1;">
                    <input type="text" name="step_descs[]" class="form-control" value="{{ $s['desc'] }}" placeholder="وصف" style="flex: 2;">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @empty
                <div style="display: flex; gap: 8px; margin-bottom: 8px; align-items: center;">
                    <input type="text" name="step_titles[]" class="form-control" placeholder="عنوان" style="flex: 1;">
                    <input type="text" name="step_descs[]" class="form-control" placeholder="وصف" style="flex: 2;">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @endforelse
            </div>
            <button type="button" class="btn btn-sm btn-success" onclick="addPair('steps-list','step_titles[]','عنوان','step_descs[]','وصف')">+ إضافة خطوة</button>
        </div>

        {{-- Terms --}}
        <div style="background: #bbbbd0; border: 2px solid #9999CC; padding: 1.5rem; margin-bottom: 1.5rem;">
            <h5 style="color: #7777CC; font-weight: 700; margin-bottom: 1rem;">شروط رخصة إعادة البيع</h5>

            <h6 style="color: #2a2; font-weight: 700;">مسموح</h6>
            <div id="allowed-list">
                @php $rAllowed = json_decode(\App\SiteSetting::get('reseller_allowed', '[]'), true) ?: []; @endphp
                @forelse($rAllowed as $a)
                <div style="display: flex; gap: 8px; margin-bottom: 6px; align-items: center;">
                    <input type="text" name="allowed_items[]" class="form-control" value="{{ $a }}">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @empty
                <div style="display: flex; gap: 8px; margin-bottom: 6px; align-items: center;">
                    <input type="text" name="allowed_items[]" class="form-control" placeholder="بند مسموح">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @endforelse
            </div>
            <button type="button" class="btn btn-sm btn-success" onclick="addSingle('allowed-list','allowed_items[]','بند مسموح')" style="margin-bottom: 10px;">+ إضافة</button>

            <h6 style="color: #a00; font-weight: 700;">غير مسموح</h6>
            <div id="notallowed-list">
                @php $rNotAllowed = json_decode(\App\SiteSetting::get('reseller_not_allowed', '[]'), true) ?: []; @endphp
                @forelse($rNotAllowed as $na)
                <div style="display: flex; gap: 8px; margin-bottom: 6px; align-items: center;">
                    <input type="text" name="notallowed_items[]" class="form-control" value="{{ $na }}">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @empty
                <div style="display: flex; gap: 8px; margin-bottom: 6px; align-items: center;">
                    <input type="text" name="notallowed_items[]" class="form-control" placeholder="بند غير مسموح">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @endforelse
            </div>
            <button type="button" class="btn btn-sm btn-success" onclick="addSingle('notallowed-list','notallowed_items[]','بند غير مسموح')" style="margin-bottom: 10px;">+ إضافة</button>

            <div class="form-group">
                <label style="font-weight: 700; color: #664;">نص التحذير:</label>
                <textarea class="form-control" name="reseller_warning" rows="2">{{ $settings['reseller_warning'] ?? '' }}</textarea>
            </div>
        </div>

        {{-- FAQ --}}
        <div style="background: #bbbbd0; border: 2px solid #9999CC; padding: 1.5rem; margin-bottom: 1.5rem;">
            <h5 style="color: #7777CC; font-weight: 700; margin-bottom: 1rem;">الأسئلة الشائعة</h5>
            <div id="faq-list">
                @php $rFaq = json_decode(\App\SiteSetting::get('reseller_faq', '[]'), true) ?: []; @endphp
                @forelse($rFaq as $faq)
                <div style="display: flex; gap: 8px; margin-bottom: 8px; align-items: center;">
                    <input type="text" name="faq_questions[]" class="form-control" value="{{ $faq['q'] }}" placeholder="السؤال" style="flex: 1;">
                    <input type="text" name="faq_answers[]" class="form-control" value="{{ $faq['a'] }}" placeholder="الجواب" style="flex: 2;">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @empty
                <div style="display: flex; gap: 8px; margin-bottom: 8px; align-items: center;">
                    <input type="text" name="faq_questions[]" class="form-control" placeholder="السؤال" style="flex: 1;">
                    <input type="text" name="faq_answers[]" class="form-control" placeholder="الجواب" style="flex: 2;">
                    <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button>
                </div>
                @endforelse
            </div>
            <button type="button" class="btn btn-sm btn-success" onclick="addPair('faq-list','faq_questions[]','السؤال','faq_answers[]','الجواب')">+ إضافة سؤال</button>
        </div>

        {{-- Contact / CTA --}}
        <div style="background: #bbbbd0; border: 2px solid #9999CC; padding: 1.5rem; margin-bottom: 1.5rem;">
            <h5 style="color: #7777CC; font-weight: 700; margin-bottom: 1rem;">التواصل والدعوة للشراء</h5>
            <div class="form-group">
                <label>عنوان زر الدعوة:</label>
                <input type="text" class="form-control" name="reseller_cta_title" value="{{ $settings['reseller_cta_title'] ?? '' }}">
            </div>
            <div class="form-group">
                <label>وصف الدعوة:</label>
                <input type="text" class="form-control" name="reseller_cta_desc" value="{{ $settings['reseller_cta_desc'] ?? '' }}">
            </div>
            <div class="form-group">
                <label>معلومات التواصل:</label>
                <textarea class="form-control" name="reseller_contact" rows="3" placeholder="واتساب: ...&#10;تلجرام: ...">{{ $settings['reseller_contact'] ?? '' }}</textarea>
            </div>
        </div>

        <button type="submit" class="btn btn-primary btn-block" style="padding: 12px; font-size: 1.05em;">حفظ إعدادات إعادة البيع</button>
    </form>

    <hr>

    {{-- Upload Reseller Product File --}}
    <div style="background: #bbbbd0; border: 2px solid #e94560; padding: 1.5rem; margin-bottom: 1.5rem;">
        <h5 style="color: #e94560; font-weight: 700; margin-bottom: 1rem;">رفع ملف نسخة الموزع</h5>
        @php $resellerFile = \App\SiteSetting::get('reseller_product_file', ''); @endphp
        @if($resellerFile)
        <div style="background: #d0e8d0; padding: 8px 12px; margin-bottom: 10px; font-size: 0.9em;">
            الملف الحالي: <strong>{{ $resellerFile }}</strong>
        </div>
        @else
        <div style="background: #e8d0d0; padding: 8px 12px; margin-bottom: 10px; font-size: 0.9em;">
            لم يتم رفع ملف نسخة الموزع بعد
        </div>
        @endif
        <form method="POST" action="/admin/reseller-settings/upload" enctype="multipart/form-data">
            @csrf
            <div style="display: flex; gap: 10px; align-items: center;">
                <input type="file" name="reseller_file" class="form-control" accept=".zip,.rar,.7z,.tar.gz" required style="flex: 1;">
                <button type="submit" class="btn btn-danger">رفع الملف</button>
            </div>
            <small style="color: #666;">صيغ مدعومة: ZIP, RAR, 7Z - هذا الملف سيتمكن الموزعون المرخصون من تحميله</small>
        </form>
    </div>
</div>

<script>
function addSingle(listId, name, ph) {
    document.getElementById(listId).insertAdjacentHTML('beforeend',
        '<div style="display:flex;gap:8px;margin-bottom:6px;align-items:center;">' +
        '<input type="text" name="' + name + '" class="form-control" placeholder="' + ph + '">' +
        '<button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button></div>');
}
function addPair(listId, n1, p1, n2, p2) {
    document.getElementById(listId).insertAdjacentHTML('beforeend',
        '<div style="display:flex;gap:8px;margin-bottom:8px;align-items:center;">' +
        '<input type="text" name="' + n1 + '" class="form-control" placeholder="' + p1 + '" style="flex:1;">' +
        '<input type="text" name="' + n2 + '" class="form-control" placeholder="' + p2 + '" style="flex:2;">' +
        '<button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button></div>');
}
function addTriple(listId, n1, p1, n2, p2, n3, p3) {
    document.getElementById(listId).insertAdjacentHTML('beforeend',
        '<div style="display:flex;gap:8px;margin-bottom:8px;align-items:center;">' +
        '<input type="text" name="' + n1 + '" class="form-control" placeholder="' + p1 + '" style="width:70px;text-align:center;">' +
        '<input type="text" name="' + n2 + '" class="form-control" placeholder="' + p2 + '" style="flex:1;">' +
        '<input type="text" name="' + n3 + '" class="form-control" placeholder="' + p3 + '" style="flex:2;">' +
        '<button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button></div>');
}
function addComp() {
    document.getElementById('comp-list').insertAdjacentHTML('beforeend',
        '<div style="display:flex;gap:8px;margin-bottom:8px;align-items:center;">' +
        '<input type="text" name="comp_features[]" class="form-control" placeholder="الميزة" style="flex:2;">' +
        '<input type="text" name="comp_normal[]" class="form-control" placeholder="العادية" style="flex:1;">' +
        '<input type="text" name="comp_reseller[]" class="form-control" placeholder="الموزع" style="flex:1;">' +
        '<button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()">X</button></div>');
}
</script>
@endsection
