frappe.pages['intake'].on_page_load = function(wrapper) {
    try {
    var page = frappe.ui.make_app_page({
        parent: wrapper,
        title: 'Customer Management',
        single_column: true
    });

    page.set_primary_action('New Customer', function() {
        show_intake_form();
    }, 'add');

    page.add_inner_button('Refresh', function() {
        load_customer_list();
    });

    $(wrapper).find('.layout-main-section').html(`
        <div class="intake-station" style="padding: 20px;">
            <div id="intake-form-container">
                <div class="card" style="margin-bottom: 20px;">
                    <div class="card-header" style="background: linear-gradient(135deg, #6f42c1, #28a745); color: white; padding: 15px;">
                        <h4 style="margin:0; color: white;">Customer Management</h4>
                    </div>
                    <div class="card-body" style="padding: 20px;">
                        <form id="intake-form">
                            <div class="row">
                                <div class="col-md-4">
                                    <h5 style="color:#6f42c1;">Customer</h5>
                                    <div class="form-group">
                                        <label>Customer <span class="text-danger">*</span></label>
                                        <div id="customer-number-control"></div>
                                    </div>
                                    <div class="form-group">
                                        <label>Company Name</label>
                                        <input type="text" id="company_name" class="form-control" readonly style="background:#f8f9fa;">
                                    </div>
                                    <div class="form-group">
                                        <label>Driver</label>
                                        <div id="driver-control"></div>
                                    </div>
                                    <div class="form-group">
                                        <label>Contact Name</label>
                                        <input type="text" id="contact_name" class="form-control">
                                    </div>
                                    <div class="form-group">
                                        <label>Contact #</label>
                                        <input type="tel" id="contact_number" class="form-control">
                                    </div>
                                    <div class="form-group">
                                        <label>Contact Email</label>
                                        <input type="email" id="contact_email" class="form-control">
                                    </div>
                                    <div class="form-group">
                                        <label>Address</label>
                                        <input type="text" id="address_line" class="form-control">
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <h5 style="color:#6f42c1;">Dates & Source</h5>
                                    <div class="form-group">
                                        <label>Received Date <span class="text-danger">*</span></label>
                                        <input type="date" id="received_date" class="form-control" required>
                                    </div>
                                    <div class="form-group">
                                        <label>Weekday</label>
                                        <input type="text" id="weekday" class="form-control" readonly style="background:#f8f9fa;">
                                    </div>
                                    <div class="form-group">
                                        <label>Pickup / Drop-off</label>
                                        <select id="pickup" class="form-control">
                                            <option value="">—</option>
                                            <option value="Pickup">Pickup</option>
                                            <option value="Drop-off">Drop-off</option>
                                        </select>
                                    </div>
                                    <div class="form-group">
                                        <label>Hours of Operation</label>
                                        <input type="text" id="hours_of_operation" class="form-control" readonly style="background:#f8f9fa;" placeholder="Auto-filled from Customer">
                                    </div>
                                    <div class="form-group">
                                        <label>Data Status</label>
                                        <select id="data_status" class="form-control">
                                            <option value="">—</option>
                                            <option value="D0">D0 (Unknown)</option>
                                            <option value="D1">D1 (Contains Data)</option>
                                        </select>
                                    </div>
                                    <div class="form-group">
                                        <label>RED / R2</label>
                                        <select id="red_r2" class="form-control">
                                            <option value="">—</option>
                                            <option value="RED">RED</option>
                                            <option value="R2">R2</option>
                                            <option value="Both">Both</option>
                                            <option value="Clear">Clear</option>
                                        </select>
                                    </div>
                                    <div class="form-group">
                                        <label>Notes</label>
                                        <textarea id="notes" class="form-control" rows="3"></textarea>
                                    </div>
                                    <div class="form-group">
                                        <label>Legacy Notes</label>
                                        <textarea id="legacy_notes" class="form-control" rows="2" readonly style="background:#fafafa;" title="Auto-filled from Customer record"></textarea>
                                    </div>
                                </div>
                                <div class="col-md-4" id="load-info-section" style="display:none;">
                                    <h5 style="color:#6f42c1;">Items & Weight</h5>
                                    <div class="form-group">
                                        <label>Barcode</label>
                                        <input type="text" id="barcode" class="form-control" placeholder="Scan barcode...">
                                    </div>
                                    <div class="form-group">
                                        <label>Total Items</label>
                                        <input type="number" id="total_items" class="form-control" value="0" min="0">
                                    </div>
                                    <div class="form-group">
                                        <label>Number of Labels</label>
                                        <input type="number" id="num_labels" class="form-control" value="1" min="1" max="20">
                                    </div>
                                    <hr>
                                    <div class="form-group">
                                        <label>Weight <span class="text-danger">*</span></label>
                                        <input type="text" id="weights" class="form-control" placeholder="e.g. 340 lbs" required>
                                    </div>
                                    <div class="form-group">
                                        <label>Invoice / Check Request</label>
                                        <input type="text" id="invoice_check_request" class="form-control">
                                    </div>
                                    <div class="form-group">
                                        <label>Amount</label>
                                        <input type="number" id="amount" class="form-control" step="0.01" value="0">
                                    </div>
                                    <div class="form-group">
                                        <label>Paid / Received</label>
                                        <select id="paid_received" class="form-control">
                                            <option value="">—</option>
                                            <option value="Paid">Paid</option>
                                            <option value="Received">Received</option>
                                            <option value="Pending">Pending</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="row" style="margin-top: 20px;">
                                <div class="col-md-12">
                                    <button type="submit" class="btn btn-primary btn-lg" style="background: linear-gradient(135deg, #6f42c1, #28a745); border: none;">
                                        Save Contact Info
                                    </button>
                                    <button type="button" class="btn btn-default btn-lg" id="btn-print-labels" disabled>
                                        Print Labels
                                    </button>
                                    <button type="button" class="btn btn-default btn-lg" id="btn-generate-cor" disabled>
                                        CoR/AoR
                                    </button>
                                    <button type="button" class="btn btn-default btn-lg" id="btn-cancel">
                                        Cancel
                                    </button>
                                    <span id="save-status" class="ml-3" style="font-size: 1.2em;"></span>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

            <div id="recent-pallets" style="display:none;">
                <div class="card">
                    <div class="card-header" style="background: #f8f9fa; display: flex; justify-content: space-between; align-items: center;">
                        <h5 style="margin:0;">Customers</h5>
                        <input type="text" id="customer-search" class="form-control" placeholder="Search..." style="width: 300px;">
                    </div>
                    <div class="card-body" style="padding: 0;">
                        <table class="table table-striped table-hover" id="customer-table">
                            <thead>
                                <tr>
                                    <th>Company</th>
                                    <th>Contact</th>
                                    <th>Phone</th>
                                    <th>Address</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody id="customer-tbody">
                                <tr><td colspan="5" class="text-center">Loading...</td></tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    `);

    setup_link_controls();
    set_today_date();

    // Trigger weekday calculation after date field is set
    setTimeout(function() {
        var dateVal = $('#received_date').val();
        if (dateVal) {
            var d = new Date(dateVal + 'T12:00:00');
            var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
            $('#weekday').val(days[d.getDay()] || '');
        }
    }, 200);

    
    // Show/hide load info based on pickup dropdown
    .on('change', function() {
        var val = .val();
        if (val) {
            .show();
        } else {
            .hide();
        }
    });
    // Initial state - hide if blank
    .trigger('change');
    
load_customer_list();

    $('#received_date').on('change', function() {
        var d = new Date($(this).val() + 'T12:00:00');
        var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
        $('#weekday').val(days[d.getDay()] || '');
    });

    $('#intake-form').on('submit', function(e) {
        e.preventDefault();
        save_customer();
    });

    $('#btn-cancel').on('click', function() {
        show_customer_list();
    });

    $('#btn-print-labels').on('click', function() {
        frappe.msgprint('Label printing coming soon.');
    });

    $('#btn-generate-cor').on('click', function() {
        generate_cor_report();
    });

    $('#customer-search').on('input', function() {
        var q = $(this).val().toLowerCase();
        $('#customer-tbody tr').each(function() {
            var text = $(this).text().toLowerCase();
            $(this).toggle(text.indexOf(q) > -1);
        });
    });

    } catch(e) {
        console.error('[INTAKE] FATAL:', e.message, e.stack);
        $(wrapper).find('.layout-main-section').html(
            '<div style="padding:20px;"><h3>Error Loading Page</h3><pre>' + e.message + '</pre></div>'
        );
    }
};

// ── Link Controls ──────────────────────────────────────────────

var customer_number_control = null;
var driver_control = null;
var selected_customer_name = null;

function setup_link_controls() {
    customer_number_control = frappe.ui.form.make_control({
        parent: $('#customer-number-control'),
        df: {
            fieldtype: 'Link',
            fieldname: 'customer_number',
            options: 'Customer',
            label: 'Customer',
            reqd: 1,
            placeholder: 'Search customer...',
            onchange: function() {
                var val = customer_number_control.get_value();
                if (val) {
                    selected_customer_name = val;
                    fetch_customer_details(val);
                } else {
                    selected_customer_name = null;
                    clear_customer_fields();
                }
            }
        },
        only_input: true,
    });
    customer_number_control.refresh();
    $('#customer-number-control .control-input').css('margin', '0');
    $('#customer-number-control .help-box').remove();

    driver_control = frappe.ui.form.make_control({
        parent: $('#driver-control'),
        df: {
            fieldtype: 'Link',
            fieldname: 'driver',
            options: 'Employee',
            label: 'Driver',
            placeholder: 'Search driver...',
            onchange: function() {}
        },
        only_input: true,
    });
    driver_control.refresh();
    $('#driver-control .control-input').css('margin', '0');
    $('#driver-control .help-box').remove();
}

function fetch_customer_details(customer_name) {
    frappe.call({
        method: 'frappe.client.get',
        args: {doctype: 'Customer', name: customer_name},
        callback: function(r) {
            if (r.message) {
                var c = r.message;
                $('#company_name').val(c.customer_name || '');
                $('#contact_name').val(c.contact_persons || '');
                $('#hours_of_operation').val(c.hours_of_operation || '');
                $('#legacy_notes').val(c.legacy_notes || '');

                // Parse phone and email from contact_persons if not in dedicated fields
                var phone = c.mobile_no || '';
                var email = c.email_id || '';
                var addressLine = '';

                // Extract phone from contact_persons text blob
                if (!phone && c.contact_persons) {
                    var phoneMatch = c.contact_persons.match(/\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}/);
                    if (phoneMatch) phone = phoneMatch[0];
                }
                // Extract email from contact_persons text blob
                if (!email && c.contact_persons) {
                    var emailMatch = c.contact_persons.match(/[\w.+-]+@[\w.-]+\.\w+/);
                    if (emailMatch) email = emailMatch[0];
                }

                $('#contact_number').val(phone);
                $('#contact_email').val(email);

                // Enable CoR button once we have a customer with data
                $('#btn-generate-cor').prop('disabled', false);

                // Get address — always fetch from Address record for full street+city+state+zip
                if (c.customer_primary_address) {
                    frappe.call({
                        method: 'frappe.client.get',
                        args: {doctype: 'Address', name: c.customer_primary_address},
                        callback: function(r3) {
                            if (r3.message) {
                                var addr = r3.message;
                                var line = (addr.address_line1 || '') + (addr.address_line2 ? ', ' + addr.address_line2 : '');
                                var full = line + (addr.city ? ', ' + addr.city : '') + (addr.state ? ', ' + addr.state : '') + (addr.pincode ? ' ' + addr.pincode : '');
                                $('#address_line').val(full);
                            }
                        }
                    });
                } else if (c.primary_address) {
                    // Fallback: parse text blob (less reliable)
                    var lines = c.primary_address.split('\n');
                    addressLine = lines.join(', ');
                    $('#address_line').val(addressLine);
                }

                // If still missing phone/email, try linked Contact
                if ((!phone || !email) && c.customer_primary_contact) {
                    frappe.call({
                        method: 'frappe.client.get',
                        args: {doctype: 'Contact', name: c.customer_primary_contact},
                        callback: function(r2) {
                            if (r2.message) {
                                var ct = r2.message;
                                if (!phone) $('#contact_number').val(ct.phone || ct.mobile_no || '');
                                if (!email) $('#contact_email').val(ct.email_id || '');
                            }
                        }
                    });
                }
            }
        }
    });
}

function clear_customer_fields() {
    $('#company_name').val('');
    $('#contact_name').val('');
    $('#contact_number').val('');
    $('#contact_email').val('');
    $('#address_line').val('');
    $('#hours_of_operation').val('');
}

function clear_form() {
    if (customer_number_control) customer_number_control.set_value('');
    if (driver_control) driver_control.set_value('');
    $('#company_name').val('');
    $('#received_date').val('');
    $('#weekday').val('');
    $('#pickup').val('');
    $('#data_status').val('');
    $('#red_r2').val('');
    $('#barcode').val('');
    $('#total_items').val(0);
    $('#num_labels').val(1);
    $('#contact_name').val('');
    $('#contact_number').val('');
    $('#contact_email').val('');
    $('#address_line').val('');
    $('#hours_of_operation').val('');
    $('#legacy_notes').val('');
    $('#weights').val('');
    $('#invoice_check_request').val('');
    $('#amount').val(0);
    $('#paid_received').val('');
    $('#notes').val('');
    $('#btn-print-labels').prop('disabled', true);
    $('#btn-generate-cor').prop('disabled', true);
    $('#save-status').html('');
}

function set_today_date() {
    var today = new Date().toISOString().split('T')[0];
    $('#received_date').val(today);
    $('#received_date').trigger('change');
}

function show_intake_form() {
    $('#intake-form-container').show();
    $('#recent-pallets').hide();
}

function show_customer_list() {
    $('#intake-form-container').hide();
    $('#recent-pallets').show();
    clear_form();
    load_customer_list();
}

function load_customer_list() {
    frappe.call({
        method: 'frappe.client.get_list',
        args: {
            doctype: 'Customer',
            fields: ['name', 'customer_name', 'customer_number', 'mobile_no', 'email_id', 'contact_persons', 'primary_address', 'customer_primary_address'],
            limit_page_length: 50,
            order_by: 'customer_name asc'
        },
        callback: function(r) {
            var tbody = $('#customer-tbody');
            tbody.empty();
            if (!r.message || r.message.length === 0) {
                tbody.append('<tr><td colspan="5" class="text-center">No customers found.</td></tr>');
                return;
            }
            r.message.forEach(function(c) {
                var contactName = '';
                if (c.contact_persons) {
                    var parts = c.contact_persons.split('|');
                    if (parts.length > 0) contactName = parts[0].trim();
                }

                tbody.append(
                    '<tr style="cursor:pointer;" onclick="select_customer_from_list(\'' + c.name.replace(/'/g, "\\'") + '\')">' +
                    '<td><strong>' + (c.customer_name || c.name) + '</strong>' + (c.customer_number ? ' <span class="text-muted">(#' + c.customer_number + ')</span>' : '') + '</td>' +
                    '<td>' + contactName + '</td>' +
                    '<td>' + (c.mobile_no || '') + '</td>' +
                    '<td>' + (c.primary_address ? c.primary_address.replace(/\n/g, ', ') : '') + '</td>' +
                    '<td><button class="btn btn-xs btn-default"><i class="fa fa-arrow-right"></i></button></td>' +
                    '</tr>'
                );
            });
        }
    });
}

function select_customer_from_list(customer_name) {
    if (customer_number_control) customer_number_control.set_value(customer_name);
    fetch_customer_details(customer_name);
}

function edit_pallet(name) {
    frappe.call({
        method: 'frappe.client.get',
        args: {doctype: 'Pallet', name: name},
        callback: function(r) {
            if (r.message) {
                show_intake_form();
                var d = r.message;
                $('#intake-form-container').data('pallet-name', name);
                $('#received_date').val(d.received_date || '');
                if (customer_number_control) customer_number_control.set_value(d.customer_number || '');
                if (driver_control) driver_control.set_value(d.driver || '');
                $('#company_name').val(d.company_name || '');
                $('#pickup').val(d.pickup || '');
                $('#data_status').val(d.data_status || '');
                $('#red_r2').val(d.red_r2 || '');
                $('#barcode').val(d.barcode || '');
                $('#total_items').val(d.total_items || 0);
                $('#num_labels').val(d.num_labels || 1);
                $('#contact_name').val(d.contact_name || '');
                $('#contact_number').val(d.contact_number || '');
                $('#contact_email').val(d.contact_email || '');
                $('#address_line').val(d.address_line || '');
                $('#hours_of_operation').val(d.hours_of_operation || '');
                $('#legacy_notes').val(d.legacy_notes || '');
                $('#weights').val(d.weights || '');
                $('#invoice_check_request').val(d.invoice_check_request || '');
                $('#amount').val(d.amount || 0);
                $('#paid_received').val(d.paid_received || '');
                $('#notes').val(d.notes || '');
                $('#received_date').trigger('change');
                $('#btn-print-labels').prop('disabled', false);
                $('#btn-generate-cor').prop('disabled', false);
            }
        }
    });
}

function save_customer() {
    var customer_name = customer_number_control ? customer_number_control.get_value() : null;
    if (!customer_name) {
        frappe.msgprint("Please select a customer first.");
        return;
    }
    frappe.call({
        method: "frappe.client.get",
        args: { doctype: "Customer", name: customer_name },
        callback: function(r) {
            if (r.message) {
                var doc = r.message;
                doc.contact_persons = .val();
                doc.mobile_no = .val();
                doc.email_id = .val();
                doc.hours_of_operation = .val();
                doc.legacy_notes = .val();
                frappe.call({
                    method: "frappe.client.save",
                    args: { doc: doc },
                    callback: function(r2) {
                        if (r2.message) {
                            frappe.msgprint("Customer updated!");
                            .html("<span style=\"color:green;\">Saved!</span>");
                        }
                    }
                });
            }
        }
    });
}

function generate_cor_report() {
    var companyName = $('#company_name').val();
    if (!companyName) {
        frappe.msgprint('Please select a customer first.');
        return;
    }
    var args = {
        company_name: companyName,
        weights: $('#weights').val() || '',
        received_date: $('#received_date').val() || '',
        red_r2: $('#red_r2').val() || '',
        contact_name: $('#contact_name').val() || '',
        contact_number: $('#contact_number').val() || '',
        address_line: $('#address_line').val() || '',
        pallet_name: $('#intake-form-container').data('pallet-name') || ''
    };
    window.open('/api/method/westech_r2.api.cor_generator.generate_cor?'
        + '&company_name=' + encodeURIComponent(args.company_name)
        + '&weights=' + encodeURIComponent(args.weights)
        + '&received_date=' + encodeURIComponent(args.received_date)
        + '&red_r2=' + encodeURIComponent(args.red_r2)
        + '&contact_name=' + encodeURIComponent(args.contact_name)
        + '&contact_number=' + encodeURIComponent(args.contact_number)
        + '&address_line=' + encodeURIComponent(args.address_line)
        + '&pallet_name=' + encodeURIComponent(args.pallet_name)
    );
}

window.edit_pallet = edit_pallet;