class Admin::ClientManagementsController < OrbitAdminController
  
  def index
    @site_requests = SiteRequest.where(:completed => false).order_by(sort).page(params[:page]).per(10)
  end

  def show
    @site_request = SiteRequest.find(params[:id])
    @user = @site_request.c_panel_user
  end

  def make_contract
    @site_request = SiteRequest.find(params[:id])
    @user = @site_request.c_panel_user
    @contract = RequestContract.new
  end

  def make_timeline
    @timeline = SiteTimeline.new
    @site_request = SiteRequest.find(params[:id])
    @user = @site_request.c_panel_user
  end

  def edit_timeline
    @timeline = SiteTimeline.find(params[:id])
    @site_request = @timeline.site_request
    @user = @site_request.c_panel_user
    
  end

  def create_timeline
    timeline = SiteTimeline.new(timeline_params)
    timeline.save
    redirect_to admin_client_management_path(timeline.site_request.id)
  end

  def update_timeline
    timeline = SiteTimeline.find(params[:id])
    timeline.update_attributes(timeline_params)
    timeline.save
    redirect_to admin_client_management_path(timeline.site_request.id)
  end

  def project_dashboard
    @site_request = SiteRequest.find(params[:id])
    @members = []
    @project_post = ProjectPost.new
    MemberProfile.all.each do |mp|
      user = mp.user rescue nil
      if !user.nil? && user.id.to_s != current_user.id.to_s
        avatar = (mp.avatar.thumb.url == "thumb_person.png" ? "/assets/thumb_person.png" : mp.avatar.thumb.url rescue "/assets/thumb_person.png")
        @members << {
          "id" => user.id.to_s,
          "user_name" => (user.user_name rescue ""),
          "avatar" => avatar,
          "name" => (mp.name_translations rescue {"en" => "","zh_tw" => ""})
        }
      end
    end
    @timeline = []
    timeline = @site_request.site_timeline
    @timeline << [{"test_site_creation_start" => timeline.test_site_creation_start.strftime("%Y-%m-%d"), "test_site_creation_end" => timeline.test_site_creation_end.strftime("%Y-%m-%d")}] if !timeline.test_site_creation_start.nil?
    @timeline << [{"template_design_images_start" => timeline.template_design_images_start.strftime("%Y-%m-%d"), "template_design_images_end" => timeline.template_design_images_end.strftime("%Y-%m-%d")}] if !timeline.template_design_images_start.nil?
    @timeline << [{"template_html_css_start" => timeline.template_html_css_start.strftime("%Y-%m-%d"), "template_html_css_end" => timeline.template_html_css_end.strftime("%Y-%m-%d")}] if !timeline.template_html_css_start.nil?
    @timeline << [{"customized_module_start" => timeline.customized_module_start.strftime("%Y-%m-%d"), "customized_module_end" => timeline.customized_module_end.strftime("%Y-%m-%d")}] if !timeline.customized_module_start.nil?
    @timeline << [{"data_migration_start" => timeline.data_migration_start.strftime("%Y-%m-%d"), "data_migration_end" => timeline.data_migration_end.strftime("%Y-%m-%d")}] if !timeline.data_migration_start.nil?
    @timeline << [{"site_structure_start" => timeline.site_structure_start.strftime("%Y-%m-%d"), "site_structure_end" => timeline.site_structure_end.strftime("%Y-%m-%d")}] if !timeline.site_structure_start.nil?
    @timeline << [{"site_migration_start" => timeline.site_migration_start.strftime("%Y-%m-%d"), "site_migration_end" => timeline.site_migration_end.strftime("%Y-%m-%d")}] if !timeline.site_migration_start.nil?
    
  end

  def complete_request
    site_request = SiteRequest.find(params[:id])
    site_request.completed = true
    site_request.save
    redirect_to admin_client_management_path(site_request.id)
  end

  def create_contract
    rc = RequestContract.new(contract_params)
    rc.save
    sr = SiteRequest.find(contract_params[:site_request])
    sr.request_contract = rc
    sr.completed = true
    sr.save
    redirect_to see_contract_admin_client_management_path(rc.id)
  end

  def update_contract
    contract = RequestContract.find(params[:id])
    contract.update_attributes(contract_params)
    contract.save
    redirect_to see_contract_admin_client_management_path(contract.id)
  end

  def edit_contract
    @contract = RequestContract.find(params[:id])
    @site_request = @contract.site_request
    @user = @site_request.c_panel_user
  end

  def see_contract
    @contract = RequestContract.find(params[:id])
    @site_request = @contract.site_request
  end

  def completed_requests
    @site_requests = SiteRequest.where(:completed => true).order_by(sort).page(params[:page]).per(10)
  end

  def confirm_contract
    contract = RequestContract.find(params[:id])
    if params["confirm"] == "true"
      contract.confirmed = true
    elsif params["confirm"] == "false"
      contract.confirmed = false
    end
    contract.save
    redirect_to see_contract_admin_client_management_path(contract.id)
  end

  def make_purchase
    contract = RequestContract.find(params[:id]) rescue nil
    if !contract.nil?
      purchase = ContractPurchase.new
      purchase.request_contract = contract
      purchase.total_amount = contract.total_amount
      purchase.save
    end
    redirect_to see_contract_admin_client_management_path(contract.id)
  end

  def receipts
    @purchase = ContractPurchase.find(params[:id]) rescue nil
    @contract = @purchase.request_contract
    @total_received_amount = @purchase.total_amount_recieved
    @total_adjustable_amount = @purchase.total_adjusted_amount
  end

  def add_receipt
    @receipt = PurchaseReceipt.new
    @purchase = ContractPurchase.find(params[:id]) rescue nil
  end

  def create_receipt
    receipt = PurchaseReceipt.create(receipt_params)
    redirect_to receipts_admin_client_management_path(receipt.contract_purchase.id)
  end

  def edit_receipt
    @receipt = PurchaseReceipt.find(params[:id])
    @purchase = @receipt.contract_purchase rescue nil
  end

  def update_receipt
    receipt = PurchaseReceipt.find(params[:id])
    receipt.update_attributes(receipt_params)
    receipt.save
    redirect_to receipts_admin_client_management_path(receipt.contract_purchase.id)
  end

  def update_site_request
    sr = SiteRequest.find(params[:id])
    sr.update_attributes(site_request_params)
    render :json => {"success" => true, "url" => sr.new_site_url}.to_json
  end

  def add_sites
    sr = SiteRequest.find(params[:id])
    sr.update_attributes(site_request_params)
    user = CPanelUser.find(sr.c_panel_user.id)
    site_request_params[:sites_added].each do |sid|
      site = RegisteredSite.find(sid)
      sr.sites_to_add.delete(site.site_domain)
      user.registered_site_ids << site.id
    end
    sr.completed = true
    sr.save
    user.save
    redirect_to admin_client_management_path(sr.id)
  end

  def contracts
    @contracts = RequestContract.all.order_by(:created_at => "desc", :confirmed => 'desc').page(params[:page]).per(10)
  end

  def create_project_post
    ppost = ProjectPost.new(project_post_params)
    ppost.save
    render :partial => "conversation", :object => ppost
  end

  def timeline_update
    timeline = SiteTimeline.find(params[:id])
    if params[:checked] == "true"
      case params[:field]
      when "test_site_creation"
        timeline.test_site_creation_completed = Time.now
        timeline.test_site_creation_by = current_user.id.to_s
        time = timeline.test_site_creation_completed.strftime("%Y/%m/%d")
      when "template_design_images"
        timeline.template_design_images_completed = Time.now
        timeline.template_design_images_by = current_user.id.to_s
        time = timeline.template_design_images_completed.strftime("%Y/%m/%d")
      when "template_html_css"
        timeline.template_html_css_completed = Time.now
        timeline.template_html_css_by = current_user.id.to_s
        time = timeline.template_html_css_completed.strftime("%Y/%m/%d")
      when "customized_module"
        timeline.customized_module_completed = Time.now
        timeline.customized_module_by = current_user.id.to_s
        time = timeline.customized_module_completed.strftime("%Y/%m/%d")
      when "data_migration"
        timeline.data_migration_completed = Time.now
        timeline.data_migration_by = current_user.id.to_s
        time = timeline.data_migration_completed.strftime("%Y/%m/%d")
      when "site_structure"
        timeline.site_structure_completed = Time.now
        timeline.site_structure_by = current_user.id.to_s
        time = timeline.site_structure_completed.strftime("%Y/%m/%d")
      when "site_migration"
        timeline.site_migration_completed = Time.now
        timeline.site_migration_by = current_user.id.to_s
        time = timeline.site_migration_completed.strftime("%Y/%m/%d")
      when "psd_approval"
        timeline.psd_approval_completed = Time.now
        time = timeline.psd_approval_completed.strftime("%Y/%m/%d")
      when "cutomer_approval"
        timeline.cutomer_approval_completed = Time.now
        time = timeline.cutomer_approval_completed.strftime("%Y/%m/%d")
      when "module_approval"
        timeline.module_approval_completed = Time.now
        time = timeline.module_approval_completed.strftime("%Y/%m/%d")
      end
    else
      case params[:field]
      when "test_site_creation"
        timeline.test_site_creation_completed = nil
        timeline.test_site_creation_by = nil
        time = timeline.test_site_creation_end.strftime("%Y/%m/%d")
      when "template_design_images"
        timeline.template_design_images_completed = nil
        timeline.template_design_images_by = nil
        time = timeline.template_design_images_end.strftime("%Y/%m/%d")
      when "template_html_css"
        timeline.template_html_css_completed = nil
        timeline.template_html_css_by = nil
        time = timeline.template_html_css_end.strftime("%Y/%m/%d")
      when "customized_module"
        timeline.customized_module_completed = nil
        timeline.customized_module_by = nil
        time = timeline.customized_module_end.strftime("%Y/%m/%d")
      when "data_migration"
        timeline.data_migration_completed = nil
        timeline.data_migration_by = nil
        time = timeline.data_migration_end.strftime("%Y/%m/%d")
      when "site_structure"
        timeline.site_structure_completed = nil
        timeline.site_structure_by = nil
        time = timeline.site_structure_end.strftime("%Y/%m/%d")
      when "site_migration"
        timeline.site_migration_completed = nil
        timeline.site_migration_by = nil
        time = timeline.site_migration_end.strftime("%Y/%m/%d")
      when "psd_approval"
        timeline.psd_approval_completed = nil
        time = ""
      when "cutomer_approval"
        timeline.cutomer_approval_completed = nil
        time = ""
      when "module_approval"
        timeline.module_approval_completed = nil
        time = ""
      end
    end
    timeline.save
    render :json => {"success" => true, "time" => time, "name" => current_user.name}.to_json
  end

  private 

  def project_post_params
    params.require(:project_post).permit!
  end

  def receipt_params
    params.require(:purchase_receipt).permit!
  end

  def contract_params
    params.require(:request_contract).permit!
  end

  def site_request_params
    params.require(:site_request).permit!
  end

  def timeline_params
    params.require(:site_timeline).permit!
  end
  
end