/**
 * Purchase Invoice Form and List Configuration
 * Optimized version using ETaxes common module patterns
 */

frappe.ui.form.on('Purchase Invoice', {
    refresh: function(frm) {
        // Check if is_taxes_doc flag is set
        if (frm.doc.is_taxes_doc) {
            // Make all fields read-only
            frm.set_read_only(true);
            
            // Add visual indicator
            frm.page.set_indicator(__('Tax Document'), 'blue');
        }
    },
    
    // Make is_taxes_doc field read-only when document loads
    onload: function(frm) {
        frm.set_df_property('is_taxes_doc', 'read_only', 1);
    }
});

frappe.listview_settings['Purchase Invoice'] = {
    onload: function(listview) {
        // Add field as column (proper way)
        listview.columns.push({
            type: 'Check',
            df: {
                label: __('Tax Doc'),
                fieldname: 'is_taxes_doc'
            },
            width: 120
        });
        
        // Force refresh list with new column
        listview.refresh();
    },
    
    // Format is_taxes_doc field display
    formatters: {
        is_taxes_doc: function(value) {
            return value ? 
                `<span class="indicator-pill green" title="${__('Tax Document')}"></span>` : 
                `<span class="indicator-pill gray" title="${__('Regular Document')}"></span>`;
        }
    }
};