@extends('layouts.main')

@section('title', 'e-TIMS Configuration - PosAI')

@section('content')
<div class="page-header">
    <div>
        <h1 class="page-title">e-TIMS Configuration</h1>
        <p class="text-muted">Configure electronic Tax Invoice Management System integration</p>
    </div>
    <div>
        <a href="{{ route('tax.index') }}" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left me-2"></i> Back to Tax
        </a>
    </div>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0"><i class="bi bi-building me-2"></i>Company Information</h5>
            </div>
            <div class="card-body">
                <form action="{{ route('tax.etims.store') }}" method="POST">
                    @csrf
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Company Name</label>
                            <input type="text" name="company_name" class="form-control" value="{{ $config->company_name }}" required>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">NPWP</label>
                            <input type="text" name="company_npwp" class="form-control" value="{{ $config->company_npwp }}" placeholder="XX.XXX.XXX.X-XXX.XXX">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">NIK (KTP)</label>
                            <input type="text" name="company_nik" class="form-control" value="{{ $config->company_nik }}">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Phone</label>
                            <input type="text" name="company_phone" class="form-control" value="{{ $config->company_phone }}">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Email</label>
                            <input type="email" name="company_email" class="form-control" value="{{ $config->company_email }}">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Branches ID</label>
                            <input type="text" name="branches_id" class="form-control" value="{{ $config->branches_id }}">
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Address</label>
                        <textarea name="company_address" class="form-control" rows="2">{{ $config->company_address }}</textarea>
                    </div>

                    <hr class="my-4">

                    <h5 class="mb-3"><i class="bi bi-key me-2"></i>API Configuration</h5>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">API URL</label>
                            <input type="url" name="api_url" class="form-control" value="{{ $config->api_url }}" placeholder="https://etims-api-sit.kemenkeu.go.id">
                            <small class="text-muted">Leave empty for default</small>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Client ID</label>
                            <input type="text" name="client_id" class="form-control" value="{{ $config->client_id }}">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Client Secret</label>
                            <input type="password" name="client_secret" class="form-control" value="{{ $config->client_secret }}" placeholder="Leave empty to keep current">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">API Key</label>
                            <input type="text" name="api_key" class="form-control" value="{{ $config->api_key }}">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">API Secret</label>
                            <input type="password" name="api_secret" class="form-control" value="{{ $config->api_secret }}" placeholder="Leave empty to keep current">
                        </div>
                    </div>

                    <hr class="my-4">

                    <h5 class="mb-3"><i class="bi bi-hdd me-2"></i>Device Configuration</h5>
                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <label class="form-label">Machine ID</label>
                            <input type="text" name="machine_id" class="form-control" value="{{ $config->machine_id ?? 'POS-001' }}" required>
                        </div>
                        <div class="col-md-4 mb-3">
                            <label class="form-label">Register ID</label>
                            <input type="text" name="register_id" class="form-control" value="{{ $config->register_id ?? 'REG-001' }}" required>
                        </div>
                        <div class="col-md-4 mb-3">
                            <label class="form-label">Serial Number</label>
                            <input type="text" name="serial_number" class="form-control" value="{{ $config->serial_number }}">
                        </div>
                    </div>

                    <hr class="my-4">

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" name="is_production" id="is_production" {{ $config->is_production ? 'checked' : '' }}>
                                <label class="form-check-label" for="is_production">
                                    Production Mode
                                </label>
                            </div>
                            <small class="text-muted">Enable for live API calls</small>
                        </div>
                        <div class="col-md-6 mb-3">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" name="is_active" id="is_active" {{ $config->is_active ? 'checked' : '' }}>
                                <label class="form-check-label" for="is_active">
                                    Enable e-TIMS Integration
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Notes</label>
                        <textarea name="notes" class="form-control" rows="2">{{ $config->notes }}</textarea>
                    </div>

                    <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                        <a href="{{ route('tax.etims.invoices') }}" class="btn btn-outline-primary">
                            <i class="bi bi-file-earmark-text me-2"></i>View Invoices
                        </a>
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-save me-2"></i>Save Configuration
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="col-md-4">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0"><i class="bi bi-info-circle me-2"></i>Status</h5>
            </div>
            <div class="card-body">
                <div class="d-flex align-items-center mb-3">
                    <div class="me-3">
                        <span class="badge bg-{{ $config->is_active ? 'success' : 'secondary' }} p-2">
                            <i class="bi bi-{{ $config->is_active ? 'check-circle' : 'x-circle' }}"></i>
                        </span>
                    </div>
                    <div>
                        <strong>e-TIMS Integration</strong>
                        <p class="text-muted mb-0 small">{{ $config->is_active ? 'Active' : 'Inactive' }}</p>
                    </div>
                </div>
                <hr>
                <div class="d-flex align-items-center mb-3">
                    <div class="me-3">
                        <span class="badge bg-{{ $config->is_production ? 'danger' : 'warning' }} p-2">
                            <i class="bi bi-{{ $config->is_production ? 'lightning' : 'clock' }}"></i>
                        </span>
                    </div>
                    <div>
                        <strong>Environment</strong>
                        <p class="text-muted mb-0 small">{{ $config->is_production ? 'Production' : 'Sandbox ( SIT )' }}</p>
                    </div>
                </div>
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <h5 class="mb-0"><i class="bi bi-question-circle me-2"></i>Help</h5>
            </div>
            <div class="card-body">
                <h6>What is e-TIMS?</h6>
                <p class="small text-muted">Electronic Tax Invoice Management System (e-TIMS) is a government-mandated system for digital tax reporting in Indonesia.</p>
                
                <h6 class="mt-3">Required Information</h6>
                <ul class="small text-muted">
                    <li>NPWP (Tax ID)</li>
                    <li>Company Registration</li>
                    <li>API Credentials from DJP</li>
                </ul>

                <h6 class="mt-3">API Endpoints</h6>
                <ul class="small text-muted">
                    <li><strong>Sandbox:</strong> etims-api-sit.kemenkeu.go.id</li>
                    <li><strong>Production:</strong> etims-api.kemenkeu.go.id</li>
                </ul>
            </div>
        </div>
    </div>
</div>
@endsection
