<?php
    // Parse ZATCA configuration
    $zatcaConfig = [];
    if (isset($company->zatca_configuration) && $company->zatca_configuration != '') {
        $zatcaConfig = json_decode($company->zatca_configuration, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            $zatcaConfig = [];
        }
    }
    
    // Set default values
    $defaults = [
        'zatca_phase' => '0', // 0 = None, 1 = Phase 1, 2 = Phase 2
        'zatca_status' => 'Disable',
        'vat_registration_number' => '',
        'legal_business_name_arabic' => '',
        'legal_business_name_english' => '',
        'zatca_address' => '',
        'compliance_csid' => '',
        'production_csid' => '',
        'zatca_secret_key' => '',
    ];
    $zatcaConfig = array_merge($defaults, $zatcaConfig);
    
    // Get current selected phase (for backward compatibility, check old zatca_1 and zatca_2)
    $selectedPhase = $zatcaConfig['zatca_phase'] ?? '0';
    if ($selectedPhase == '0') {
        // Check old format for backward compatibility
        if (isset($zatcaConfig['zatca_1']) && $zatcaConfig['zatca_1'] == '1') {
            $selectedPhase = '1';
        } elseif (isset($zatcaConfig['zatca_2']) && $zatcaConfig['zatca_2'] == '1') {
            $selectedPhase = '2';
        }
    }
    
    // Check if CSR files exist (using direct path to storage/app/zatca)
    $csrExists = file_exists(storage_path('app/zatca/csr.pem'));
    $privateKeyExists = file_exists(storage_path('app/zatca/private_key.pem'));
?>

<div class="tab-pane fade <?php echo e(request()->route('tab') == 'zatca_setting' ? 'active show' : ''); ?>" id="zatca_setting" role="tabpanel">
    <form id="zatca_setting_form" enctype="multipart/form-data">
        <?php echo csrf_field(); ?>

        <div class="card mb-6">
            <div class="card-header">
                <h5 class="card-title m-0"><?php echo e(__('ZATCA')); ?></h5>
            </div>
            <div class="card-body">
                <div class="row g-6">
                    <!-- Radio buttons for ZATCA Phase selection -->
                    <div class="col-12 validate_wrapper">
                        <label class="form-label mb-3"><?php echo e(__('Select ZATCA Phase')); ?> <?php echo requiredField(); ?></label>
                        <div class="d-flex flex-column gap-3">
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="zatca_phase" id="zatca_phase_0" value="0" <?php echo e($selectedPhase == '0' ? 'checked' : ''); ?>>
                                <label class="form-check-label" for="zatca_phase_0">
                                    <?php echo e(__('None (Disable ZATCA)')); ?>

                                </label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="zatca_phase" id="zatca_phase_1" value="1" <?php echo e($selectedPhase == '1' ? 'checked' : ''); ?>>
                                <label class="form-check-label" for="zatca_phase_1">
                                    <strong><?php echo e(__('ZATCA Phase 1')); ?></strong> - <?php echo e(__('Basic QR Code Generation (No API Submission)')); ?>

                                </label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="zatca_phase" id="zatca_phase_2" value="2" <?php echo e($selectedPhase == '2' ? 'checked' : ''); ?>>
                                <label class="form-check-label" for="zatca_phase_2">
                                    <strong><?php echo e(__('ZATCA Phase 2')); ?></strong> - <?php echo e(__('Full Compliance (API Submission, Digital Signatures, Hash Chain)')); ?>

                                </label>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- ZATCA Phase 2 Status (Only shown when Phase 2 is selected) -->
        <div class="card mb-6" id="zatca_phase2_status_card" style="display: <?php echo e($selectedPhase == '2' ? 'block' : 'none'); ?>;">
            <div class="card-header">
                <h5 class="card-title m-0"><?php echo e(__('ZATCA Phase 2 Status')); ?></h5>
            </div>
            <div class="card-body">
                <div class="row g-6">
                    <div class="col-12 col-md-6 validate_wrapper">
                        <label class="form-label mb-1" for="zatca_status"><?php echo e(__('ZATCA Phase 2 Status')); ?> <?php echo requiredField(); ?></label>
                        <select class="select2 form-select" id="zatca_status" name="zatca_status" data-placeholder="<?php echo e(__('Select')); ?> <?php echo e(__('Status')); ?>">
                            <option value=""></option>
                            <option value="Enable" <?php echo e(($zatcaConfig['zatca_status'] ?? 'Disable') == 'Enable' ? 'selected' : ''); ?>><?php echo e(__('Enable')); ?></option>
                            <option value="Disable" <?php echo e(($zatcaConfig['zatca_status'] ?? 'Disable') == 'Disable' ? 'selected' : ''); ?>><?php echo e(__('Disable')); ?></option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <!-- Environment Information (Hidden when Phase is None) -->
        <div class="card mb-6" id="zatca_environment_info_card" style="display: <?php echo e($selectedPhase != '0' ? 'block' : 'none'); ?>;">
            <div class="card-header">
                <h5 class="card-title m-0"><?php echo e(__('Environment Information')); ?></h5>
            </div>
            <div class="card-body">
                <div class="row">

                    <div class="col-12">
                        <div class="alert alert-info" role="alert">
                            <h6 class="alert-heading"><?php echo e(__('Instructions')); ?></h6>
                            <p class="mb-0"><?php echo e(__('Fillup the below required fields and Click the "Generate CSR" button to create Certificate Signing Request (CSR) and Private Key files. After generation, download the CSR file and upload it to your ZATCA account.')); ?></p>
                        </div>
                    </div>

                    <div class="col-12 col-md-6 mb-3 validate_wrapper">
                        <label class="form-label mb-1" for="legal_business_name_english"><?php echo e(__('Legal Business Name (English)')); ?> <?php echo requiredField(); ?></label>
                        <input type="text" class="form-control" id="legal_business_name_english" name="legal_business_name_english" placeholder="<?php echo e(__('Enter')); ?> <?php echo e(__('Legal Business Name (English)')); ?>" value="<?php echo e($zatcaConfig['legal_business_name_english'] ?? ''); ?>">
                    </div>

                    <div class="col-12 col-md-6 mb-3 validate_wrapper">
                        <label class="form-label mb-1" for="legal_business_name_arabic"><?php echo e(__('Legal Business Name (Arabic)')); ?> <?php echo requiredField(); ?></label>
                        <input type="text" class="form-control" id="legal_business_name_arabic" name="legal_business_name_arabic" placeholder="<?php echo e(__('Enter')); ?> <?php echo e(__('Legal Business Name (Arabic)')); ?>" value="<?php echo e($zatcaConfig['legal_business_name_arabic'] ?? ''); ?>" dir="rtl">
                    </div>

                    <div class="col-12 col-md-6 mb-3 validate_wrapper">
                        <label class="form-label mb-1" for="vat_registration_number"><?php echo e(__('VAT Registration Number')); ?> <?php echo requiredField(); ?></label>
                        <input type="text" class="form-control" id="vat_registration_number" name="vat_registration_number" placeholder="<?php echo e(__('Enter')); ?> <?php echo e(__('VAT Registration Number')); ?>" value="<?php echo e($zatcaConfig['vat_registration_number'] ?? ''); ?>">
                    </div>

                    <div class="col-12 col-md-6 mb-3 validate_wrapper">
                        <label class="form-label mb-1" for="zatca_address"><?php echo e(__('Address')); ?> <?php echo requiredField(); ?></label>
                        <textarea class="form-control" id="zatca_address" name="zatca_address" rows="3" placeholder="<?php echo e(__('Enter')); ?> <?php echo e(__('Address')); ?>"><?php echo e($zatcaConfig['zatca_address'] ?? ''); ?></textarea>
                    </div>

                    <?php if(isset($zatcaConfig) && $zatcaConfig && $zatcaConfig['vat_registration_number'] != '' && $zatcaConfig['legal_business_name_arabic'] != '' && $zatcaConfig['legal_business_name_english'] != '' && $zatcaConfig['zatca_address'] != ''): ?>
                        <div class="col-12 cs-generate-btn" id="cs_generate_btn_wrapper" style="display: <?php echo e($selectedPhase == '2' ? 'block' : 'none'); ?>;">
                            <button type="button" class="btn btn-primary waves-effect waves-light" id="generate_csr_btn">
                                <i class="ti tabler-key me-1"></i>
                                <?php echo e(__('Generate CSR')); ?>

                            </button>
                        </div>
                        
                        <?php if($csrExists || $privateKeyExists): ?>
                        <div class="col-12 cs-generated-files" id="cs_generated_files_wrapper" style="display: <?php echo e($selectedPhase == '2' ? 'block' : 'none'); ?>;">
                            <hr>
                            <div class="alert alert-success" role="alert">
                                <h6 class="alert-heading"><?php echo e(__('Files Generated Successfully')); ?></h6>
                                <p class="mb-2"><?php echo e(__('Your CSR and Private Key files have been generated. Please download the CSR file and upload it to ZATCA.')); ?></p>
                                <div class="d-flex gap-2 flex-wrap">
                                    <?php if($csrExists): ?>
                                        <a href="<?php echo e(route('zatca.download', ['file' => 'csr'])); ?>" class="btn btn-sm btn-success">
                                            <i class="ti tabler-download me-1"></i>
                                            <?php echo e(__('Download CSR')); ?>

                                        </a>
                                    <?php endif; ?>
                                    <?php if($privateKeyExists): ?>
                                        <a href="<?php echo e(route('zatca.download', ['file' => 'private_key'])); ?>" class="btn btn-sm btn-warning">
                                            <i class="ti tabler-download me-1"></i>
                                            <?php echo e(__('Download Private Key')); ?>

                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                        <?php endif; ?>
                    <?php endif; ?>

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


        <!-- ZATCA Credentials (After Upload) - Only shown when Phase 2 is selected -->
        <div class="card mb-6 zatca_credentials_card" id="zatca_credentials_card" style="display: <?php echo e($selectedPhase == '2' ? 'block' : 'none'); ?>;">
            <div class="card-header">
                <h5 class="card-title m-0"><?php echo e(__('ZATCA Credentials')); ?></h5>
            </div>
            <div class="card-body">
                <div class="row g-6">
                    <div class="col-12">
                        <div class="alert alert-warning" role="alert">
                            <h6 class="alert-heading"><?php echo e(__('Important')); ?></h6>
                            <p class="mb-0"><?php echo e(__('After uploading the CSR file to your ZATCA account, you will receive three credentials. Please enter them below:')); ?></p>
                        </div>
                    </div>

                    <div class="col-12 col-md-4 validate_wrapper">
                        <label class="form-label mb-1" for="compliance_csid"><?php echo e(__('Compliance CSID')); ?></label>
                        <input type="text" class="form-control" id="compliance_csid" name="compliance_csid" placeholder="<?php echo e(__('Enter')); ?> <?php echo e(__('Compliance CSID')); ?>" value="<?php echo e($zatcaConfig['compliance_csid'] ?? ''); ?>">
                    </div>

                    <div class="col-12 col-md-4 validate_wrapper">
                        <label class="form-label mb-1" for="production_csid"><?php echo e(__('Production CSID')); ?></label>
                        <input type="text" class="form-control" id="production_csid" name="production_csid" placeholder="<?php echo e(__('Enter')); ?> <?php echo e(__('Production CSID')); ?>" value="<?php echo e($zatcaConfig['production_csid'] ?? ''); ?>">
                    </div>

                    <div class="col-12 col-md-4 validate_wrapper">
                        <label class="form-label mb-1" for="zatca_secret_key"><?php echo e(__('ZATCA Secret Key')); ?></label>
                        <input type="text" class="form-control" id="zatca_secret_key" name="zatca_secret_key" placeholder="<?php echo e(__('Enter')); ?> <?php echo e(__('ZATCA Secret Key')); ?>" value="<?php echo e($zatcaConfig['zatca_secret_key'] ?? ''); ?>">
                    </div>
                </div>
            </div>
        </div>


        <div class="d-flex justify-content-end gap-4">
            <button type="submit" class="btn btn-primary waves-effect waves-light zatca_setting_submit">
                <i class="ti tabler-checkbox me-1"></i>
                <?php echo e(__('Save Changes')); ?>

            </button>
        </div>
    </form>
</div>

<script>
    // Show/hide sections based on selected ZATCA phase
    (function() {
        function toggleZatcaSections(selectedPhase) {
            // Phase 0 (None): Hide Phase 2 Status, Environment Info, Credentials
            // Phase 1: Hide Phase 2 Status, CSR button, CSR files, Credentials (but show Environment Info)
            // Phase 2: Show everything
            
            // Helper function to safely show/hide elements
            function safeToggle(selector, show) {
                const element = document.querySelector(selector);
                if (element) {
                    if (show) {
                        element.style.display = 'block';
                    } else {
                        element.style.display = 'none';
                    }
                }
            }
            
            // Helper function for elements that might not exist (CSR button/files)
            function safeToggleOptional(selector, show) {
                const element = document.querySelector(selector);
                if (element) {
                    if (show) {
                        element.style.display = 'block';
                    } else {
                        element.style.display = 'none';
                    }
                }
            }
            
            if (selectedPhase == '0') {
                // None - Hide everything except the phase selection
                safeToggle('#zatca_phase2_status_card', false);
                safeToggle('#zatca_environment_info_card', false);
                safeToggle('#zatca_credentials_card', false);
            } else if (selectedPhase == '1') {
                // Phase 1 - Show Environment Info, but hide Phase 2 specific items
                safeToggle('#zatca_phase2_status_card', false);
                safeToggle('#zatca_environment_info_card', true);
                safeToggleOptional('#cs_generate_btn_wrapper', false);
                safeToggleOptional('#cs_generated_files_wrapper', false);
                safeToggle('#zatca_credentials_card', false);
            } else if (selectedPhase == '2') {
                // Phase 2 - Show everything
                safeToggle('#zatca_phase2_status_card', true);
                safeToggle('#zatca_environment_info_card', true);
                safeToggleOptional('#cs_generate_btn_wrapper', true);
                safeToggleOptional('#cs_generated_files_wrapper', true);
                safeToggle('#zatca_credentials_card', true);
            }
        }
        
        // Wait for DOM to be ready
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', init);
        } else {
            init();
        }
        
        function init() {
            // Handle phase change using both jQuery (if available) and vanilla JS
            const radioButtons = document.querySelectorAll('input[name="zatca_phase"]');
            radioButtons.forEach(function(radio) {
                radio.addEventListener('change', function() {
                    const selectedPhase = this.value;
                    toggleZatcaSections(selectedPhase);
                });
            });
            
            // Also use jQuery if available (for compatibility)
            if (typeof jQuery !== 'undefined') {
                jQuery('input[name="zatca_phase"]').on('change', function() {
                    const selectedPhase = jQuery(this).val();
                    toggleZatcaSections(selectedPhase);
                });
            }
            
            // Initialize on page load
            const checkedRadio = document.querySelector('input[name="zatca_phase"]:checked');
            if (checkedRadio) {
                toggleZatcaSections(checkedRadio.value);
            }
        }
    })();
</script>
<?php /**PATH /home/uzbeacom/pos.uzbea.com/off_pos/Modules/Configuration/resources/views/backend/settings/setting-component/zatca-setting.blade.php ENDPATH**/ ?>