odoo.define('lefilament_tdb.dashboard_year', function (require) {
	"use strict";

	var core = require('web.core');
	var formats = require('web.formats');
	var Model = require('web.Model');
	var session = require('web.session');
	var Widget = require('web.Widget');

	var QWeb = core.qweb;

	var _t = core._t;
	var _lt = core._lt;

	var YearDashboardView = Widget.extend({
	    template: 'YearDashboard',

	    events: {
	        'click .card': function() { 
	        	this.mychart.reflow();
	         },
	        'click #facture_non_encaisse': function() {
	        	this.facture();
	        },
	        'click #commandes': function() {
	        	this.commandes();
	        },
	    },

	    init: function() {
	      var result = this._super.apply(this, arguments);
	      return result;
	    },

	    willStart: function() {
	        var deferred = new jQuery.Deferred();
	        var self = this;
	        this.values = {};
	        this.progess = 0;
	        
	        var dash_model = new Model('lefilament.dashboard');
	        dash_model.call('retrieve_datas_dashboard')
                .then(function(results) {
                    self.values = results;
                    self.progress = results.facture / results.target;
                    deferred.resolve();
                });
	          return jQuery.when(this._super.apply(this, arguments),deferred);
	    },

	    start: function() {
	    	return this.render_chart();
	    },

	    render_chart: function(chart) {
	    	self = this;

	    	var pfact = (this.values.facture / this.values.target * 100).toFixed(0);
			var pcomm = (this.values.commandes / this.values.target * 100).toFixed(0);
			var ppipe_win = (this.values.pipe_win / this.values.target * 100).toFixed(0);
			var ppipe_to_win = (this.values.pipe_to_win / this.values.target * 100).toFixed(0);
			var ptarg = 100-pfact-pcomm-ppipe_win-ppipe_to_win;
			var pfact2 = (this.values.facture);
			var pcomm2 = (this.values.commandes);
			var ppipe2_win = (this.values.pipe_win);
			var ppipe2_to_win = (this.values.pipe_to_win);
			var ptarg2 = this.values.target -pfact2-pcomm2-ppipe2_to_win-ppipe2_win;

   			var hchart = this.$el.find('#hchart')[0];

        	Highcharts.setOptions({ colors: ['#8ED8A2', '#F6DCA2', '#F6CCA2', '#F6ACA2', '#eee'], });

			this.mychart = new Highcharts.chart({
				chart: {
			        renderTo: hchart,
			        type: 'pie',
			        margin: 0
			    },
			    title: {
			        text: '<span class="ca_target">'+
				    	self.values.target.toLocaleString('fr', { maximumFractionDigits: 0 }) + ' € </span><br />'
				    	+'facturé '+ pfact + ' %<br />'
				    	+'commandes '+ pcomm + ' %<br />'
				    	+'gagné '+ ppipe_win + ' %<br />'
				    	+'pipe '+ ppipe_to_win + ' %',
			        align: 'center',
			        verticalAlign: 'middle',
			        style: { 'color': '#73879C', 'font-size': '11px'  },
			        y: -20,
			    },
			    plotOptions: {
			        pie: {
			        	slicedOffset: 0,
			            size: '100%',
			            dataLabels: {
			                enabled: false
			            }
			        },
			        series: {
			            states: {
			                hover: {
			                    enabled: false
			                }
			            }
			        }
			    },
			    tooltip: { 
			    	pointFormat: '<p class="point"><span class="point_percent">{point.percentage:.0f}% </span><br/> {point.y} €</p>',
			    	backgroundColor: "rgba(255,255,255,0.85)",
					borderColor: null,
					borderRadius: 0,
					borderWidth: 0,
					useHTML: true,
					shadow: false,
				},
			    series: [{
			        type: 'pie',
			        innerSize: '85%',
			        data: [  ['Facturé',   pfact2], ['Commandes', pcomm2], ['Gagné', ppipe2_win], ['Pipe', ppipe2_to_win], ['To Do', ptarg2], ],
			        slicedOffset: 0,

			    }],
			});

			this.mychart.reflow();
	    },

	    render_monetary: function(value) {
	        value = value.toLocaleString('fr', { maximumFractionDigits: 0 }) + ' €';
	        return value;
	    },
	    render_date: function(value) {
	    	var dateFormat = new Date(value);
            var new_value = moment(dateFormat).format('Do MMM YYYY');
            return new_value;
	    },
	    render_monetary_color: function(value) {
	        if (value >= 0)
	          value = '<span class="positive">'+value.toLocaleString('fr', { maximumFractionDigits: 0 }) + ' €</span>';
	        else
	          value = '<span class="negative">'+value.toLocaleString('fr', { maximumFractionDigits: 0 }) + ' €</span>';
	        
	        return value;
	    },

	    facture: function() {
	    	var self = this;
	    	var context = { 'user_id': this.session.uid, }

        	var action = ({
            	type: 'ir.actions.act_window',
             	res_model: 'account.invoice',
            	view_type: 'form',
            	view_mode: 'tree,form',
            	views: [[false, 'list'], [false, 'form']],
            	domain: [['state','=','open'],['type','=','out_invoice']],
            	target:'current',
            	name: 'Factures en cours',
            	context: context
        	})

        	this.do_action(action);
	    },
	    commandes: function() {
	    	var self = this;
	    	var context = { 'user_id': this.session.uid, }

        	var action = ({
            	type: 'ir.actions.act_window',
             	res_model: 'sale.order',
            	view_type: 'form',
            	view_mode: 'tree,form',
            	views: [[false, 'list'], [false, 'form']],
            	domain: [['invoice_status','=','to invoice']],
            	target:'current',
            	name: 'Commandes en cours',
            	context: context
        	})

        	this.do_action(action);
	    },
	});

	core.action_registry.add('lefilament_tdb.dashboard_year', YearDashboardView);


});