<%
  define_breadcrumbs do
    add_breadcrumb('竞赛列表', cooperative_competitions_path)
    add_breadcrumb(@competition.name)
  end
%>

<div class="card mb-5">
  <div class="card-header d-flex justify-content-between align-items-center">
    <span class="flex-1">基础设置</span>
  </div>
  <div class="card-body row">
    <%= form_tag(basic_setting_cooperative_competition_competition_settings_path(@competition), method: :post, class: 'basic-setting-form flex-1', remote: true) do %>
      <div class="container competition-mode-container">
        <div class="row align-items-center mb-1">
          <div class="col-1 text-right">
            主标题
          </div>
          <div class="col-5 text-left">
            <%= text_field_tag(:name, @competition.name, autocomplete: 'off', class: 'form-control', placeholder: '竞赛标题') %>
          </div>
        </div>

        <div class="row align-items-center mb-1">
          <div class="col-1 text-right">
            副标题
          </div>
          <div class="col-5 text-left">
            <%= text_field_tag(:sub_title, @competition.sub_title, autocomplete: 'off', class: 'form-control', placeholder: '竞赛副标题') %>
          </div>
        </div>

        <div class="row align-items-center mb-1">
          <div class="col-1 text-right">
            起止时间
          </div>
          <div class="col-5 competition-start-end-date d-flex">
            <%= text_field_tag :start_time, @competition.start_time&.strftime('%Y-%m-%d %H:%M'), autocomplete: 'off', class: 'form-control start-date mx-0 mr-2', placeholder: '竞赛开始时间' %>
            <%= text_field_tag :end_time, @competition.end_time&.strftime('%Y-%m-%d %H:%M'), autocomplete: 'off', class: 'form-control end-date mx-0', placeholder: '竞赛截止时间' %>
          </div>
        </div>

        <input type="hidden" name="mode" id="mode_1" value="1" class="form-radio-input" checked="checked">

        <div class="row align-items-center mb-2">
          <div class="col-1 text-right">
            URL
          </div>
          <div class="col-5 text-left mode-input">
            <%= text_field_tag(:identifier, @competition.identifier, autocomplete: 'off', class: 'form-control', placeholder: '请输入url赛事网址') %>
          </div>
        </div>

        <div class="row align-items-center d-flex mb-2">
          <div class="col-1 text-right">
            管理员
          </div>
          <div class="col-5 text-left sponsorPanel">
            <% manager_data = @competition.managers.map { |u| [u.name, u.id] } %>
            <%= select_tag :manager_ids, options_for_select(manager_data, @competition.managers.map(&:id)), class: 'form-control manager-select', multiple: true %>
          </div>
        </div>

        <div class="row align-items-center d-flex mb-2">
          <div class="col-1 text-right">
            主办方
          </div>
          <div class="col-5 text-left sponsorPanel">
            <% sponsor_data = @competition.sponsor_schools.map { |s| [s.school.name, s.school.id] } %>
            <%= select_tag :sponsor_schools, options_for_select(sponsor_data, @competition.sponsor_schools.map(&:school_id)), class: 'form-control sponsor-select', multiple: true %>
          </div>
        </div>

        <div class="row align-items-center d-flex mb-2">
          <div class="col-1 text-right">
            开放范围
          </div>
          <div class="col-5 text-left sponsorPanel">
            <% region_data = @competition.region_schools.map { |s| [s.school.name, s.school.id] } %>
            <%= select_tag :region_schools, options_for_select(region_data, @competition.region_schools.map(&:school_id)), class: 'form-control allow-school-select', multiple: true %>
          </div>
        </div>

        <div class="row align-items-center mb-2">
          <div class="col-1 text-right">
            奖金
          </div>
          <div class="col-5 text-left input-group">
            <div class="input-group-prepend">
              <div class="input-group-text">¥</div>
            </div>
            <%= number_field_tag(:bonus, @competition.bonus, autocomplete: 'off', step: 1, min: 0, class: 'form-control', placeholder: '请输入总奖金额') %>
          </div>
        </div>

        <div class="row align-items-center mb-2">
          <div class="col-1 text-right">
            奖项数
          </div>
          <div class="col-5 text-left">
            <%= number_field_tag(:awards_count, @competition.awards_count, autocomplete: 'off', step: 1, min: 0, class: 'form-control', placeholder: '请输入奖项数') %>
          </div>
        </div>

        <div class="row des-row align-items-center mb-2">
          <div class="col-1 text-right">
            描述
          </div>
          <div class="col-11 text-left">
            <%= text_area_tag(:description, @competition.description, class: 'form-control', placeholder: '请输入赛事简介') %>
          </div>
        </div>

        <div class="error my-2 danger text-danger"></div>

        <div class="row des-row align-items-center mb-2">
          <div class="col-1 text-right">
          </div>
          <div class="col-5 text-left">
            <%= javascript_void_link '保存', class: 'btn btn-primary submit-btn' %>
          </div>
        </div>
      </div>
  <% end %>
  </div>
</div>

<div class="card mb-5">
  <div class="card-header d-flex justify-content-between align-items-center">
    <span class="flex-1">导航设置</span>
  </div>
  <div class="card-body row">
    <%= form_tag(nav_setting_cooperative_competition_competition_settings_path(@competition), method: :post, class: 'nav-setting-form flex-1', remote: true) do %>
      <div class="container competition-mode-container">
        <% @competition.competition_modules.each do |com_module| %>
          <% case com_module.module_type  %>
        <% when 'home' %>
            <div class="mt-3" id="MD_typeFrom">
              <div class="row MD_type">
                <div class="col-1 text-right">
                  <label class="checkbox checkbox-primary mt-1">
                    <input type="checkbox" name="navbar[][hidden]" value="0" hidden class="font-16" checked="checked">
                    <input type="checkbox" disabled="disabled" class="font-16" checked="checked">
                  </label>
                </div>
                <div class="col-md-4">
                  <input type="hidden" value="<%= com_module.id %>" name="navbar[][module_id]">
                  <%= text_field_tag('navbar[][name]', com_module.name, id: nil, class: 'form-control', placeholder: '首页') %>
                  <input type="hidden" value="<%= com_module.module_type %>" name="navbar[][module_type]">
                </div>
                <div class="col-md-1">
                  <%= text_field_tag('navbar[][position]', com_module.position, id: nil, class: 'form-control', placeholder: '位置') %>
                </div>
              </div>
            </div>
          <% when 'enroll' %>
            <div class="row mt-2">
              <div class="col-1 text-right">
                <label class="checkbox checkbox-primary mt-1">
                  <input type="checkbox" name="navbar[][hidden]" value="<%= com_module.hidden ? 1 : 0 %>" hidden class="font-16" checked="checked">
                  <%= check_box_tag('', 0, !com_module.hidden, id: nil, class: 'font-16 module_hidden') %>
                </label>
              </div>
              <div class="col-md-8 color-blue mt-1">
                <input type="hidden" value="<%= com_module.id %>" name="navbar[][module_id]">
                <input type="hidden" value="<%= com_module.module_type %>" name="navbar[][module_type]">
                <input type="hidden" value="报名" name="navbar[][name]">
                报名
              </div>
            </div>
            <div class="row mt-2 align-items-center">
              <div class="col-1 text-right">&nbsp;&nbsp;</div>
              <div class="col-1 text-left" style="max-width: 120px;flex: 0 0 120px;">
                报名截止时间
              </div>
              <div class="col-md-3">
                <%= text_field_tag :enroll_end_time, @competition.enroll_end_time&.strftime('%Y-%m-%d %H:%M'), autocomplete: 'off', class: 'form-control enroll_end_time', placeholder: '报名截止时间' %>
              </div>
            </div>
            <div class="row mt-2">
              <div class="col-1 text-right">&nbsp;&nbsp;</div>
              <div class="col-1 text-left mt-1">
                报名要求
              </div>
              <div class="col-md-3">
                <%= javascript_void_link '+', class: 'btn btn-primary waves-effect waves-light btn-xs setBtn_s addRequireBtn' %>
              </div>
            </div>

            <div id="requireForm" class="competition-staff-settings">
              <% @competition.competition_staffs.each do |staff| %>
                <div class="row mt-2 mb-4 requireForm_item">
                  <div class="col-1 text-right">&nbsp;&nbsp;</div>
                  <div class="col-1 text-left mt-1">
                    <input type="text" class="form-control" name="competition_staffs[][minimum]" value="<%= staff.minimum %>" />
                  </div>
                  <span class="mt-2">~</span>
                  <div class="col-1 mt-1">
                    <input type="text" class="form-control" name="competition_staffs[][maximum]" value="<%= staff.maximum %>" />
                  </div>
                  <span class="mt-2">人</span>
                  <div class="col-2 mt-1">
                    <select class="form-control" name="competition_staffs[][category]">
                      <option value="student" <%= staff.category == "student" ? "selected='selected'" : "" %>>学生</option>
                      <option value="teacher" <%= staff.category == "teacher" ? "selected='selected'" : "" %>>教师</option>
                    </select>
                  </div>
                  <div class="col-2 mt-1">
                    <label class="radio checkbox-primary mt-1" value="require_1_1">
                      <input id="require_1_<%= staff.id %>" <%= staff.mutiple_limited? ? '' : 'checked="checked"' %> class="mutiple-limited-radio" value="false" name="competition_staffs[][mutiple_limited]" type="checkbox">
                      <label for="require_1_<%= staff.id %>">可多次报名</label>
                    </label>
                  </div>
                  <div class="col-2 mt-1">
                    <label class="radio checkbox-primary mt-1" value="require_1_2">
                      <input id="require_2_<%= staff.id %>" <%= staff.mutiple_limited? ? 'checked="checked"' : '' %> class="mutiple-limited-radio"  value="true" name="competition_staffs[][mutiple_limited]" type="checkbox">
                      <label for="require_2_<%= staff.id %>">不可多次报名</label>
                    </label>
                    <a href="javascript:void(0)" class="ml20 delRequrieBtn">
                      <i class="fa fa-times-circle font-20 color-grey-c"></i>
                    </a>
                  </div>
                </div>
              <% end %>
            </div>

          <% when 'inform', 'chart', 'resource', 'certificate' %>
            <div class="row mt-2 new_module_div linkFormItem">
              <div class="col-1 text-right">
                <label class="checkbox checkbox-primary mt-1">
                  <input type="checkbox" name="navbar[][hidden]" value="<%= com_module.hidden ? 1 : 0 %>" hidden class="font-16" checked="checked">
                  <%= check_box_tag('', 0, !com_module.hidden, id: nil, class: 'font-16 module_hidden') %>
                </label>
              </div>
              <div class="col-md-label mt-2">
                <input type="hidden" value="<%= com_module.id %>" name="navbar[][module_id]">
                <input type="hidden" value="<%= com_module.module_type %>" name="navbar[][module_type]">
                <input type="hidden" value="<%= com_module.name %>" name="navbar[][name]">
                <%= com_module.name %>
              </div>
              <div class="col-md-1 mt-1">
                <%= text_field_tag('navbar[][position]', com_module.position, id: nil, class: 'form-control', placeholder: '位置') %>
              </div>
              <% if com_module.module_type == "resource" %>
                <div class="col-md-3 mt-1">
                  <%= text_field_tag('navbar[][url]', com_module.url, id: nil, class: 'form-control', placeholder: '请输入资料下载地址') %>
                </div>
                <%= javascript_void_link '+', class: 'mt-1 btn btn-primary waves-effect waves-light btn-xs setBtn_s add_linkBtn' %>
              <% end %>
            </div>
          <% else %>
            <div class="row mt-2 align-items-center linkFormItem">
              <div class="col-1 text-right">
                <label class="checkbox checkbox-primary mt-1">
                  <input type="checkbox" name="navbar[][hidden]" value="<%= com_module.hidden ? 1 : 0 %>" hidden class="font-16" checked="checked">
                  <%= check_box_tag('', 0, !com_module.hidden, id: nil, class: 'font-16 module_hidden') %>
                </label>
              </div>
              <div class="col-md-label mt-1">
                <input type="hidden" value="<%= com_module.id %>" name="navbar[][module_id]">
                <input type="hidden" value="<%= com_module.module_type %>" name="navbar[][module_type]">
                <%= text_field_tag('navbar[][name]', com_module.name, id: nil, class: 'form-control', placeholder: '请输入模块名称') %>
              </div>
              <div class="col-md-1 mt-1">
                <%= text_field_tag('navbar[][position]', com_module.position, id: nil, class: 'form-control', placeholder: '位置') %>
              </div>
              <div class="col-md-3 mt-1">
                <%= text_field_tag('navbar[][url]', com_module.url, id: nil, class: 'form-control', placeholder: '请输入资料下载地址') %>
              </div>
              <%= javascript_void_link '+', class: 'mt-1 btn btn-primary waves-effect waves-light btn-xs setBtn_s add_linkBtn' %>
              <%= javascript_void_link '×', class: 'mt-1 btn btn-icon waves-effect btn-default waves-light setBtn_s ml10 del_linkBtn' %>
            </div>
          <% end %>
        <% end %>

        <div class="error my-2 danger text-danger"></div>

        <div class="row mt-2 mb-4">
          <div class="col-1 text-right">
          </div>
          <div class="col-md-label mt-2"><%= javascript_void_link '保存', class: 'btn btn-primary submit-btn' %></div>
        </div>
      </div>
    <% end %>
  </div>
</div>


<%# if @competition.mode == 1 %>
  <div class="card mb-5 competition-chart-stages">
    <div class="card-header d-flex justify-content-between align-items-center">
      <span>排行榜设置</span>
      <i class="fa fa-question-circle font-14" data-toggle="tooltip" data-html="true" data-placement="top" title="温馨提示：<br/>
需要参赛选手完成几个实训，请相应的创建几个阶段；<br/>
若多个实训只需要参赛选手选择完成1个，请将其配置在同一个阶段；<br/>
一个阶段多个实训时，请注意实训关卡数及各关卡的经验值设置保持相同；<br/>
同阶段的各个实训，请保持相同的成绩来源；<br/>
每阶段每个实训关卡的得分，将按照组内成员最高得分计算，总分为各阶段得分加和。"></i>
      <a href="javascript:void(0)" class="btn btn-primary btn-custom waves-effect add-new-tab waves-light ml20" data-competition-id="<%= @competition.id %>">+ 新增tab</a>

      <span class="flex-1 text-right color-orange">实训ID填写示例：实训地址为https://www.educoder.net/shixuns/u5plmgka/challenges，则填写“u5plmgka”</span>
    </div>

    <div class="card-body">
      <div id="large_panel" class="large_panel competition-chart-setting">
        <% if @competition.competition_stages.count > 0 %>
          <% @competition.competition_stages.includes(competition_stage_sections: :competition_entries).each_with_index do |stage, index| %>
            <%= form_tag(cooperative_competition_competition_stage_path(competition_id: @competition.id, id: stage.id), method: :put, class: 'stage-update-form flex-1', remote: true) do %>
              <div class="large_panel_part" attr_line="<%= index + 1 %>">
                <div class="row d-flex">
                  <span class="col-1 mt-2">tab标题</span>
                  <div class="col-2 no_padding">
                    <input type="text" class="form-control" name="stage_name" value="<%= stage.name %>" />
                  </div>
                  <span class="col-1 text-right mt-2 no_padding">总排行榜占比:</span>
                  <div class="col-1 no_padding">
                    <input type="number" class="form-control" name="score_rate" value="<%= (stage.score_rate * 100).to_i %>"/>
                  </div><span class=" mt-2">%</span>
                  <div class="flex-1">
                    <a href="javascript:void(0)" class="btn btn-outline-primary export-action ml20 add-task-sub">新增子阶段</a>
                    <% if stage.max_end_time && stage.max_end_time > Time.now %>
                      <%= agree_link '发送短信提醒', send_message_cooperative_competition_competition_stage_path(@competition, stage, element: ".send-message-#{stage.id}"),
                                     class: 'btn btn-outline-primary ml20', 'data-confirm': '确认执行发送短信操作？' %>
                    <% end %>
                    <% if stage.max_end_time && stage.max_end_time < Time.now && Time.now < Time.at(stage.max_end_time.to_i + 30*24*3600) %>
                      <%= agree_link '计算成绩', calculate_stage_score_cooperative_competition_competition_stage_path(@competition, stage, element: ".calculate-score-#{stage.id}"),
                                     class: 'btn btn-outline-primary ml20', 'data-confirm': '确认执行计算成绩操作？' %>
                    <% end %>
                  </div>
                  <%= delete_link '删除', cooperative_competition_competition_stage_path(competition_id: @competition.id, id: stage.id), class: 'btn btn-default delete-stage ml20' %>
                  <a href="javascript:void(0)" class="btn btn-outline-primary export-action update-stage ml20">保存</a>
                </div>

                <div id="small_panel_<%= index + 1 %>" class="small_panel">
                  <% stage.competition_stage_sections.each_with_index do |section, j| %>
                    <div class="row d-flex small_panel_item" attr_line="sub_<%= index %>_<%= j %>" count="<%= j + 1 %>">
                      <span class="mt-2 subName mr10">第<%= j + 1 %>阶段</span>
                      <div class="flex-1">
                        <div class="row">
                          <div class="row col-6">
                            <span class="ml20 mt-2 mr10">有效时间:</span>
                            <div class="col-4 no_padding">
                              <%= text_field_tag 'stage[][start_time]', section.start_time&.strftime('%Y-%m-%d %H:%M'), autocomplete: 'off', class: 'section-start-time form-control', placeholder: '有效开始时间' %>
                            </div>
                            <span class="mt-2">~</span>
                            <div class="col-4 no_padding">
                              <%= text_field_tag 'stage[][end_time]', section.end_time&.strftime('%Y-%m-%d %H:%M'), autocomplete: 'off', class: 'section-end-time form-control', placeholder: '有效结束时间' %>
                            </div>
                          </div>
                          <div class="row col-3">
                            <span class="col-4 text-right mt-2 no_padding mr10">总任务数:</span>
                            <div class="col-6 no_padding">
                              <input type="number" class="form-control task_all" value="<%= section.entry %>" onchange="change_total(this)" name="stage[][entry]"/>
                            </div>
                          </div>
                          <div class="row col-3">
                            <span class="col-4 text-right mt-2 no_padding mr10">成绩来源:</span>
                            <div class="col-6 no_padding">
                              <select class="form-control" name="stage[][score_source]">
                                <option value="0" <%= section.score_source == 0 ? "selected='selected'" : "" %>>经验值</option>
                                <option value="1" <%= section.score_source == 1 ? "selected='selected'" : "" %>>预测准确率</option>
                              </select>
                            </div>
                          </div>
                        </div>
                        <div class="row mt-2" id="task_Input_sub_<%= index %>_<%= j %>">
                          <% section.competition_entries.each_with_index do |entry, z| %>
                            <div class="col-4 row task_Input_div">
                              <span class="col-4 text-right mt-3 no_padding mr10">任务<%= z+1 %></span>
                              <div class="col-6 no_padding">
                                <input type="text" class="form-control mt-2" value="<%= entry.shixun_identifier %>" name="stage[][identifiers][]" placeholder="请填写实训ID">
                              </div>
                            </div>
                          <% end %>
                        </div>
                      </div>
                      <span>
                      <a href="javascript:void(0)" class="btn btn-default ml20 small_panel_item_del">删除</a>
                    </span>
                    </div>
                  <% end %>
                </div>
                <div class="error my-2 danger text-danger"></div>
              </div>
            <% end %>
          <% end %>
        <% else %>
          <%= form_tag(cooperative_competition_competition_stages_path(competition_id: @competition.id), method: :post, class: 'stage-update-form new-stage-form flex-1', remote: true) do %>
            <div class="large_panel_part" attr_line="1">
              <div class="row d-flex">
                <span class="col-1 mt-2">tab标题</span>
                <div class="col-2 no_padding">
                  <input type="text" class="form-control" name="stage_name"/>
                </div>
                <span class="col-1 text-right mt-2 no_padding">总排行榜占比:</span>
                <div class="col-1 no_padding">
                  <input type="number" class="form-control" name="score_rate" value="100"/>
                </div><span class=" mt-2">%</span>
                <div class="flex-1">
                  <a href="javascript:void(0)"class="btn btn-outline-primary export-action ml20 add-task-sub">新增子阶段</a>
                </div>
                <a href="javascript:void(0)" class="btn btn-default ml20" onclick="Del_tab(this)">删除</a>
                <a href="javascript:void(0)" class="btn btn-outline-primary update-stage export-action ml20">保存</a>
              </div>
              <div id="small_panel_1" class="small_panel">
                <div class="row d-flex small_panel_item" attr_line="sub_new_new" count="1">
                  <span class="mt-2 subName mr10">第1阶段</span>
                  <div class="flex-1">
                    <div class="row">
                      <div class="row col-6">
                        <span class="mt-2 ml20 mr10">有效时间:</span>
                        <div class="col-4 no_padding">
                          <%= text_field_tag 'stage[][start_time]', '', autocomplete: 'off', class: 'section-start-time form-control', placeholder: '有效开始时间' %>
                        </div>
                        <span class="mt-2">~</span>
                        <div class="col-4 no_padding">
                          <%= text_field_tag 'stage[][end_time]', '', autocomplete: 'off', class: 'section-end-time form-control', placeholder: '有效结束时间' %>
                        </div>
                      </div>
                      <div class="row col-3">
                        <span class="col-4 text-right mt-2 no_padding mr10">总任务数:</span>
                        <div class="col-6 no_padding">
                          <input type="number" class="form-control task_all" onchange="change_total(this)" value="3" name="stage[][entry]"/>
                        </div>
                      </div>
                      <div class="row col-3">
                        <span class="col-4 text-right mt-2 no_padding mr10">成绩来源:</span>
                        <div class="col-6 no_padding">
                          <select class="form-control" name="stage[][score_source]">
                            <option value="0">经验值</option>
                            <option value="1">预测准确率</option>
                          </select>
                        </div>
                      </div>
                    </div>
                    <div class="row mt-2" id="task_Input_sub_new_new">
                      <div class="col-4 row task_Input_div">
                        <span class="col-4 text-right mt-3 no_padding mr10">任务1</span>
                        <div class="col-6 no_padding">
                          <input type="text" class="form-control mt-2" name="stage[][identifiers][]" placeholder="请填写实训ID">
                        </div>
                      </div>
                      <div class="col-4 row task_Input_div">
                        <span class="col-4 text-right mt-3 no_padding mr10">任务2</span>
                        <div class="col-6 no_padding">
                          <input type="text" class="form-control mt-2" name="stage[][identifiers][]" placeholder="请填写实训ID">
                        </div>
                      </div>
                      <div class="col-4 row task_Input_div">
                        <span class="col-4 text-right mt-3 no_padding mr10">任务3</span>
                        <div class="col-6 no_padding">
                          <input type="text" class="form-control mt-2" name="stage[][identifiers][]" placeholder="请填写实训ID">
                        </div>
                      </div>
                    </div>
                  </div>
                  <span>
              <a href="javascript:void(0)" class="btn btn-default ml20 small_panel_item_del">删除</a>
            </span>
                </div>
              </div>
            </div>
          <% end %>
        <% end %>
        <div class="error my-2 danger text-danger"></div>
      </div>
    </div>
  </div>

  <div class="card mb-5" id="competition-prize-card" data-id="<%= @competition.id %>">
    <div class="card-header d-flex justify-content-between align-items-center">
      <span class="flex-1">奖项配置</span>

      <% unless @competition.competition_prize_users.exists? %>
        <%= link_to '新增奖项', new_cooperative_competition_competition_prize_path(@competition), remote: true, class: 'btn btn-primary btn-sm add-competition-prize-btn' %>
      <% end %>

      <% if @competition.finished? && !@competition.competition_prize_users.exists? %>
        <%= javascript_void_link '生成获奖记录', class: 'btn btn-primary btn-sm ml-2 generate-prize-user-action' %>
      <% end %>
    </div>
    <div class="card-body row pt-0">
      <table class="table text-center competition-prize-table">
        <thead>
          <th width="8%">序号</th>
          <th width="10%">奖项名称</th>
          <th width="10%">数量</th>
          <th width="10%">奖励类型</th>
          <th width="16%">队员个人证书模板</th>
          <th width="16%">团队证书模板</th>
          <th width="16%">指导老师证书模板</th>
          <th width="14%">操作</th>
        </thead>
        <tbody>
          <%= render 'cooperative/competition_settings/shared/competition_prizes', competition: @competition %>
        </tbody>
      </table>
    </div>
  </div>
  <div style="margin-bottom: 8.5rem;"></div>
<%# end %>

<%= render partial: 'cooperative/shared/modal/upload_file_modal', locals: { title: '上传证书模板', accept: 'image/*' } %>