# -*- coding: utf-8 -*-
class TrelloController < ApplicationController
  $department = 'construction'
  $department_id = '676'
  layout "constr_works"
  skip_before_filter :login_required, :only =>:create
  skip_before_filter :check_roles, :only =>:create
  before_filter :isAuthorization, :except => :create
  before_filter :set_var, :except => :create


  def edit
  end

  def create
    @mess = ""
    @status = ""
    @report_id = nil
    if params[:sk]="5D<mys+sYT5D<mys+sYTXsrCTEXsrCTE"
     # @report = Report.find_by_trello_id params[:id]
      @status = "Отчёт обновлён"
    #  unless @report
        @report = Report.create(:trello_id => params[:id], :date => Date.today, :department =>"construction")
        @status = "Отчёт создан"
    #  end
        category = Category.find_by_name(params[:category])
      if category
        category.update_attributes(:description => params[:board_id]) if category.description.to_s.blank?
      else
        category = Category.create(:name => params[:category], :department => "construction", :description => params[:board_id])
      end
      @report.categor = category
      @report.name = params[:name]
      if @report.save
        @mess = "https://sdelka.allnets.pro/constr_works/#{@report.id.to_s}/edit"
        @report_id = @report.id
      else
        @mess = @report.errors.each_full { |msg| puts msg }
        @status = "Error"
      end
    else
      @mess = "Проблемы с доступом!"
    end
    render(:json => {:status => @status, :mess => @mess, :report_id => @report_id})

  end
  
  def new
  end

  def script
    pl = 0
    all_rows = 0
    bad = 0
    bad_desc = []
    tracker = {}
    periods = []
    require 'fastercsv'
    require 'iconv'
    #@converter = Iconv.new('UTF-8', 'Windows-1251')
    @text = params[:file].read  
    str = "CASE tracker_id"
    str2 = "CASE tracker_id"
    @tr_ids = []
    date = Date.today
    period = Period.for_month(date).for_department("construction")
    if period.to_a.size == 0
      Period.create(:month => date.beginning_of_month, :department => "construction", :closed => false)
    end
    FasterCSV.parse(@text, :col_sep => ',') do |row|
      if row[0] == "Card ID"
        @desc_ind = row.find_index("Card Description")
        @name_ind = row.find_index("Card Name")
        @cat_ind = row.find_index("Board Name")
        
      else
        all_rows += 1
        category = Category.find_by_name(row[@cat_ind])
        unless category
          category = Category.create(:name => row[@cat_ind], :department => "construction")
        end
        name = row[@name_ind].to_s
        desc = row[@desc_ind].to_s
      report = Report.find_by_trello_id row[0]
      unless report
        report = Report.new(:department =>"construction", :date => date)
      end
      report.category = category.id
      report.name = name
      report.trello_id = row[0]
      report.com_stay_work = desc
      if report.save
        @tr_ids << report.name
      else
        bad+=1
        bad_desc << report.name
      end
      end
    end

    flash[:notice] = "Всего: " + (all_rows).to_s + " Хорошие: " + @tr_ids.join(", ") + " Плохие: " + bad.to_s + ".  " + bad_desc.join(", ")
    redirect_to home_path
  end

  
  private

  def set_var
    @date = Date.today
    @employee = Employee.find(:all, :conditions => "division_id = 676")
    Employee.current_user = Employee.find(session[:user_id]) unless session[:user_id].nil?
    $department = 'construction'
    $department_id = '676'
    cookies[:sys_dep] = { :value => $department }
    @current_user = Employee.current_user
    @boss = (@current_user.accesses.for_department('construction').size > 0 && @current_user.accesses.for_department('construction').first.access_superuser == 1)
     if (params[:date_from] && params[:date_to])
       @date_from = params[:date_from].to_date
       @date_to = params[:date_to].to_date
     else
       @date_from = Date.today.beginning_of_month
       @date_to = Date.today.end_of_month
     end

  end

end
