# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.


from odoo.exceptions import UserError, ValidationError
from odoo import api, fields, models, _

class EtourismBookingCheckout(models.TransientModel):
    _name = 'etourism.booking.checkout.wizard'
    _description = 'Open Booking Checkout Wizard'

    @api.model
    def default_get(self, fields):
        context = self.env.context
        res = super(EtourismBookingCheckout, self).default_get(fields)
        booking_group = self.env['etourism.booking.group'].browse(context.get('active_ids'))
        lines = []
        temp = [lines.append(line.id) for line in booking_group.booking_line if line.state in ('hosted')]

        res.update(booking_ids=[[6,0, lines]])
        return res

    booking_group_id = fields.Many2one('etourism.booking.group', string='Booking Group')
    booking_ids = fields.Many2many('etourism.booking', 'booking_group_bookings2')

    @api.multi
    def action_checkout(self):
        print "LET'S CHECKOUT::::::::::::"
        for booking in self:
            booking_group_id=booking.booking_group_id
            booking_lines = self.env['etourism.booking']
            cancelIds =[]
            if booking.booking_ids:
                for bk in booking.booking_ids:
                    print "BOOKING::::::::::::: ",bk.id
                    cancelIds.append(bk.id)
                    bk.write({'state': 'checkout'})
            bk_not_selected = booking_lines.sudo().search([('id', 'not in', cancelIds), ('state', '!=', 'checkout'), ('booking_group_id', '=', booking_group_id.id)])
            for a in booking_group_id.booking_line:
                print "STATES:::::::::::::", a.state
            print "NOT SELECTED:::::::::::", len(bk_not_selected)
            if len(bk_not_selected) == 0:
                booking_group_id.write({'state': 'checkout'})
            #raise ValidationError(_('Test'))
