<% content_for :page_specific_css do %>
  <%= stylesheet_link_tag "client_management/backend" %>
<% end %>

<div class="contract-wrapper">
	<div class="page-contract page-client contract-section">
		<div class="info-contract">
			<h3 class="sub-title">Contract Info</h3>
			<div class="line-content">
				<span class="header-line">Contract Id :</span>
				<span class="content-line"><%= @contract.uid %></span>
			</div>
			<div class="line-content">
				<span class="header-line">Contract Date :</span>
				<span class="content-line"><%= @contract.created_at.strftime("%Y-%m-%d") %></span>
			</div>
			<div class="line-content">
				<span class="header-line">Confirmed :</span>
				<span class="content-line">
					<%= (@contract.is_confirmed? ? "<span class='label label-success'>Yes</span>" : "<span class='label label-important'>No</span>").html_safe %>
				</span>
			</div>
			<% if !@contract.contract_purchase.nil? %>
				<div class="line-content">
					<span class="header-line">Payment Status :</span>
					<span class="content-line">
						<% if @contract.contract_purchase.cleared? %>
							<span class="label label-success">Cleared</span>
						<% else %>
							<span class="label label-important">Pending</span>
						<% end %>
					</span>
				</div>
			<% end %>
		</div>
		<div class="info-contract-request">
			<h3 class="sub-title">Request</h3>
			<div class="line-content">
				<span class="header-line">Type :</span>
				<span class="content-line"><%= @site_request.get_request_type %></span>
			</div>
		</div>


		<% if @site_request.type == "existing" %>
			<div class="info-contract-detail">
				<h3 class="sub-title">Site Details</h3>
				<div class="line-content">
					<span class="header-line">Site ID :</span>
					<span class="content-line"><%= @site_request.get_site.uid %></span>
				</div>
				<div class="line-content">
					<span class="header-line">Site Name  : </span>
					<span class="content-line"><%= @site_request.get_site.title %></span>
				</div>
				<div class="line-content">
					<span class="header-line">Site Domain  : </span>
					<span class="content-line"><%= @site_request.get_site.site_domain %></span>
				</div>
			</div>
		<% end %>

		<div class="info-contract-file">
			<h3 class="sub-title">Contract Files</h3>
			<% if !@contract.contract_file.url.nil? %>
				<div class="line-content">
					<span class="header-line">Contract :</span>
					<span class="content-line"><a href="<%= @contract.contract_file.url %>" target="_blank">Download</a></span>
				</div>
			<% end %>
			<% if !@contract.signed_contract_file.url.nil? %>
				<div class="line-content">
					<span class="header-line">Signed Contract :</span>
					<span class="content-line"><a href="<%= @contract.signed_contract_file.url %>" target="_blank">Download</a></span>
				</div>
			<% end %>
		</div>

		<div class="info-contract-quatation">
			<h3 class="sub-title">Quotaion</h3>
		    <div class="line-content qt">
		      <span class="header-line">Item</span>
		      <span class="content-line">Cost in NTD.</span>
		    </div>
			<% total_amount = 0 %>
			<% ["new_site_cost","hosting_with_rulingdigital_cost","service_cost","option_module_cost","customized_module_cost","template_cost","customized_template_cost","rwd_cost"].each do |attrib| %>
				<% amount = @contract.send(attrib.to_sym) %>
				<% if amount > 0 %>
					<div class="line-content qt">
						<span class="header-line">
							<%= t("client_management.#{attrib}") %>
							<% if attrib == "option_module_cost" %>
							<ol class="custom-module">
								<% rsm = RegisteredModule.find(@site_request.optional_modules) %>
								<% rsm.each do |rs|%>
									<li><%= rs.name %></li>
								<% end %>
							</ol>
							<% end %>	
							<% if attrib == "service_cost" %>
							 for <%= @site_request.maintenance_years rescue 1 %> year(s)
							<% end %>
						</span>
						<span class="content-line"><%= amount %></span>
					</div>
					<% total_amount = total_amount + amount %>
				<% end %>
		 	<% end %>
		 	<% if !@contract.other.nil? || @contract.other_cost > 0 %>
		 		<div class="line-content qt">
						<span class="header-line">
							Other Requirements
							<div class="requirement-text">
								<%= nl2br(@contract.other) %>
							</div>
						</span>
						<span class="content-line"><%= @contract.other_cost %></span>
				</div>
				<% total_amount = total_amount + @contract.other_cost %>
		 	<% end %>
		 	<div class="line-content qt hr">
		 		<span class="header-line">Total :</span>
		 		<span class="content-line"><%= total_amount.to_s %></span>
		 	</div>
		</div>

		<div class="info-contract-file-upload">
			<div class="line-content file">
				<%= form_for @contract, :url => {:action => "update_contract"}, :html => {:class => "form-horizontal"} do |f| %>
					<span class="header-line">Signed Contract File :</span>
					<span class="content-line">
						<%= f.file_field :signed_contract_file, :class => "file-box" %>	
						<%= f.submit "Upload", :class => "btn btn-primary" %>
					</span>
				<% end %>
			</div>
		</div>
		<div class="line-content buttons">
 			<a class="btn btn-info pull-right" href="<%= completed_requests_admin_client_managements_path(:page => params[:page]) %>">Back</a>
 		</div>
	</div>
	<div class="contract-actions contract-section">
    <h3 class="action-heading">Actions</h3>
		<ul>
			<% if !@contract.is_confirmed? %>
				<li>
					<a href="<%= edit_contract_admin_client_management_path(@contract.id) %>"><i class="icons-pencil"></i> Edit Contract</a>
				</li>
				<li>
					<a href="<%= confirm_contract_admin_client_management_path(@contract.id, :confirm => "true") %>" ><i class="icons-checkmark"></i> Confirm Contract</a>
				</li>
			<% else %>
					<% if @contract.contract_purchase.nil? %>
						<li>
							<a href="<%= make_purchase_admin_client_management_path(@contract.id) %>" id="make-purchase-btn"><i class="icons-cart"></i> Make Purchase</a>
						</li>
						<li>
							<a href="<%= confirm_contract_admin_client_management_path(@contract.id, :confirm => "false") %>"><i class="icons-cross"></i> Un-Confirm Contract</a>
						</li>
					<% else %>
					<li>
						<a href="<%= receipts_admin_client_management_path(@contract.contract_purchase.id) %>"><i class="icons-credit-card" aria-hidden="true"></i> Receipts</a>
					</li>
					<% end %>
					<li>
						<a href="<%= project_dashboard_admin_client_management_path(@site_request.id) %>"><i class="icons-gauge"></i> Dashboard</a>
					</li>
				<% end %>
				<% if !@contract.site_request.site_timeline.nil? %>
					<li>
						<a href="<%= edit_timeline_admin_client_management_path(@contract.site_request.site_timeline.id) %>"><i class="icons-flow-line"></i> Timeline</a>
					</li>
				<% end %>
				<li>
					<a href="#"><i class="icons-export"></i> Export Contract</a>
				</li>
		</ul>
	</div>
</div>

<script type="text/javascript">
	$("#make-purchase-btn").on("click",function(){
		if(!confirm("Once you create a purchase, you cannot un-confirm or edit this contract. Continue to create a purchase for this contract?")){
			return false;
		}
	})
</script>
