<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>项目工程列表</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
    <script src="{{ url_for('static', filename='utils.js') }}"></script>
    <script src="{{ url_for('static', filename='sub_project_list.js') }}"></script>
    <style>
        .file-upload-box{
            width: 100%;
            display: flex;
            flex-direction: column;
        }
        .drag-drop-box {
            border: 2px dashed #ccc;
            border-radius: 4px;
            padding: 20px;
            text-align: center;
            cursor: pointer;
            transition: border-color 0.3s;
            color: #666;
        }
        .drag-drop-box.dragover {
            border-color: #2196F3;
            background-color: rgba(33, 150, 243, 0.1);
        }
         .file-list:empty{
             display: none;
         }
        .file-list {
            display: flex;
            flex-wrap: wrap;
            margin-top: 10px;
        }
        .file-item {
            display: flex;
            align-items: center;
            background-color: #f5f5f5;
            padding:5px 8px;
            border-radius: 5px;
            margin: 5px 8px;

        }
        .file-item span{
            color: #2196f3;
        }
        .file-item span.del {
            margin-left: 5px;
            cursor: pointer;
            color: #eeeeee;
            background: #2196f3;
            border-radius: 50%;
            width: 15px;
            height: 15px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        td .file{
            width: auto;
            margin: 0 5px;
            color: #2196f3;
            text-decoration: none;
        }
        td .file:hover{
            text-underline: #2196f3;
            text-decoration: underline;
        }
    </style>
    <script>
        function searchData(){
            const keyword = document.getElementById('search_input').value;
            const status = document.getElementById('status_select').value;
            window.location.href = `{{ url_for('project.sub_project_list',project_id=project.id) }}?k=${keyword}&s=${status}`;
        }
        function reSearchData(){
            window.location.href = `{{ url_for('project.sub_project_list',project_id=project.id) }}`;
        }
        function jumpToPage(page) {
            window.location.href = `{{ url_for('project.sub_project_list', project_id=project.id) }}?s={{status}}&k={{keyword}}&pp={{per_page}}&p=${page}`
        }
        function detail(id){
            window.location.href = `{{  url_for('project.project_item_list',sub_project_id='') }}${id}`
        }
    </script>
</head>
<body>
    <div class="box">
        <div class="box_header">
            <div class="project_info">
                <button class="btn btn-info btn-large" onclick="goTo(`{{ url_for('project.main_project_list') }}`)">返回</button>
                <h3 class="box_title">工程列表</h3>
                <span class="separator">|</span>
                <dl>
                    <dt>工程名称:</dt>
                    <dd>{{ project.project_name }}</dd>
                </dl>
            </div>
            {% include 'base/user_info.html' %}
        </div>
        <div class="box_header">
            <div class="btn_box">
                <button type="button" class="btn btn-success btn-large" onclick="addSubProject()">新建工程</button>
            </div>
            <div class="search_box">
                <select id="status_select" class="form-control form-select" title="状态选择">
                    <option value="-1" {% if status == -1 %}selected{% endif %}>所有状态</option>
                    <option value="0" {% if status == 0 %}selected{% endif %}>新建</option>
                    <option value="5" {% if status == 5 %}selected{% endif %}>完成</option>
                     <option value="11" {% if status == 11 %}selected{% endif %}>采集失败</option>
                    <option value="12" {% if status == 12 %}selected{% endif %}>分析失败</option>
                    <option value="13" {% if status == 13 %}selected{% endif %}>上传失败</option>
                     <option value="21" {% if status == 21 %}selected{% endif %}>采集中...</option>
                    <option value="22" {% if status == 22 %}selected{% endif %}>分析中...</option>
                    <option value="23" {% if status == 23 %}selected{% endif %}>上传中...</option>
                    <option value="31" {% if status == 31 %}selected{% endif %}>采集完成</option>
                    <option value="32" {% if status == 32 %}selected{% endif %}>分析完成</option>
                    <option value="33" {% if status == 32 %}selected{% endif %}>上传完成</option>
                </select>
                <input type="text" id="search_input" class="form-control" placeholder="请输入查询关键字" value="{{ keyword }}">
                <button type="button" class="btn btn-info btn-large" onclick="searchData()">查询</button>
                <button type="button" class="btn btn-warning btn-large" onclick="reSearchData()">重置</button>
            </div>
        </div>
        <div class="box-body">
            <table class="table">
                <thead>
                    <tr>
                        <th width="330px">工程名称</th>
                        <th width="25%">工程分类</th>
                        <th width="">文件</th>
<!--                        <th width="120px">标准版本</th>-->
                        <th width="120px">状态</th>
                        <th width="340px">操作</th>
                    </tr>
                </thead>
                <tbody>
                {% if sub_project_list %}
                    {% for item in sub_project_list %}
                        <tr data-id="{{item.id}}">
                            <td class="sub_project_name">
                                {{ item.sub_project_name if item.sub_project_name else '-' }}
                                <input type="hidden" class="form-control" title="工程名称" name="sub_project_name" value="{{ item.sub_project_name if item.sub_project_name else '' }}">
                            </td>
                            <td class="work_catalog">
                                {{ item.work_catalog if item.work_catalog else '-' }}
                                <input type="hidden" class="form-control" title="工作目录" name="work_catalog" value="{{ item.work_catalog if item.work_catalog else '' }}">
                            </td>
                            <td class="">
                                {% if item.file_paths %}
                                {% for file in item.file_paths.split(",") %}
                                <a class="file" href="{{ url_for('project.download_file') }}?f={{file}}&p={{item.project_id}}&sp={{item.id}}" download>{{ file|basename }}</a>
                                 {% endfor %}
                                 {% endif %}
                            </td>
<!--                            <td class="work_content">
                                {{ item.work_content if item.work_content else '-' }}
                                <input type="hidden" class="form-control" title="工作内容" value="{{ item.work_content if item.work_content else '' }}">
                            </td>-->
<!--                            <td class="standard_version">
                                {% if item.standard_version == '1' %}
                                    <span class="label label-warning">旧版</span>
                                    {% elif item.standard_version == '2' %}
                                    <span class="label label-info">新版</span>
                                    {% endif %}
                                <select class="form-control hide" title="标准版本">
                                    <option value="1" {% if item.standard_version == '1' %}selected{% endif %}>旧版</option>
                                    <option value="2" {% if item.standard_version == '2' %}selected{% endif %}>新版</option>
                                </select>
                            </td>-->
                            <td>
                                <span class="status">
                                    {% if item.status == 0 %}
                                    <span class="label label-default">新建</span>
                                    {% elif item.status == 21 %}
                                    <span class="label label-warning">采集中...</span>
                                    {% elif item.status == 22 %}
                                    <span class="label label-warning">分析中...</span>
                                    {% elif item.status == 23 %}
                                    <span class="label label-warning">上传中...</span>
                                    {% elif item.status == 31 %}
                                    <span class="label label-info">采集完成</span>
                                    {% elif item.status == 32 %}
                                    <span class="label label-info">分析完成</span>
                                    {% elif item.status == 33 %}
                                    <span class="label label-info">上传完成</span>
                                    {% elif item.status == 11 %}
                                    <span class="label label-danger">采集失败</span>
                                    {% elif item.status == 12 %}
                                    <span class="label label-danger">分析失败</span>
                                    {% elif item.status == 13 %}
                                    <span class="label label-danger">上传失败</span>
                                    {% elif item.status == 5 %}
                                    <span class="label label-success">完成</span>
                                    {% endif %}
                                </span>
                            </td>
                            <td>
                            {% if item.status == 0 %}
                                <button class="btn btn-success" type="button" onclick="startSubProjectTask('{{ item.id }}')">开始任务</button>
                                {% elif item.status == 11 %}
                                <button class="btn btn-success" type="button" onclick="reStartSubProjectTask('{{ item.id }}')">重新采集</button>
                                {% elif item.status == 12 %}
                                <button class="btn btn-success" type="button" onclick="reStartProcessSubProjectTask('{{ item.id }}')">重新处理</button>
                                {% elif item.status == 13 %}
                                <button class="btn btn-success" type="button" onclick="reStartSendSubProjectTask('{{ item.id }}')">重新发送</button>
                                {% endif %}
                                <button class="btn btn-info" type="button" onclick="detail('{{ item.id }}')">详情</button>
                                {% if item.status != 21  and item.status != 22 %}
                                <button class="btn btn-info" onclick="updateSubProject(this.parentNode.parentNode.parentNode,'{{ item.id }}')">编辑</button>
                                <button class="btn btn-danger" onclick="deleteSubProject('{{ item.id }}')">删除</button>
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                {% else %}
                    <tr>
                        <td colspan="15">没有工程数据</td>
                    </tr>
                {% endif %}
                </tbody>
            </table>
             <div class="pagination">
                <div class="pagination-info">
                    {% set total_pages = (total_count|int + per_page|int - 1)//per_page %}
                   <span class="page">
                        总共 {{ total_count }} 条数据，每页 {{ per_page }} 条，当前第 {{ page }} 页 / 共 {{ total_pages }} 页
                   </span>
                </div>
                <div class="pagination-links">
                    {% if page > 1 %}
                    <span class="page page-link" onclick="jumpToPage(1)">首页</span>
                    {% endif %} {% if page > 1 %}
                    <span class="page page-link" onclick="jumpToPage(`{{page - 1}}`)">上一页</span>
                    {% endif %} {% set start_page = [1, page - 2]|max %} {% set end_page = [total_pages, page + 2]|min %} {% if start_page > 1 %}
                    <span class="page page-link" onclick="jumpToPage(1)">1</span>
                    {% if start_page > 2 %}
                    <span class="page">...</span>
                    {% endif %} {% endif %} {% for p in range(start_page, end_page + 1) %} {% if p == page %}
                    <span class="page page-link active" onclick="jumpToPage(`{{ p }}`)">{{ p }}</span>
                    {% else %}
                    <span class="page page-link" onclick="jumpToPage(`{{ p }}`)">{{ p }}</span>
                    {% endif %} {% endfor %} {% if end_page < total_pages %} {% if end_page < total_pages - 1 %}
                    <span class="page">...</span>
                    {% endif %}
                    <span class="page page-link" onclick="jumpToPage(`{{ total_pages }}`)">{{ total_pages }}</span>
                    {% endif %} {% if page < total_pages %}
                    <span class="page page-link" onclick="jumpToPage(`{{ page+1 }}`)">下一页</span>
                    {% endif %} {% if page < total_pages %}
                    <span class="page page-link" onclick="jumpToPage(`{{ total_pages }}`)">末页</span>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
    <div class="modal" id="modal-sub_project" 
    ondragover="handleDragOver(event)"
    ondragleave="handleDragLeave(event)"
    ondrop="handleDrop(event)">
        <div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<h3 class="modal-title"><span></span>工程</h3>
					<span class="close-btn" onclick="hide_modal('#modal-sub_project')">&times;</span>
				</div>
				<div class="modal-body">
					<div class="form-group">
                    <input type="hidden" id="sub_id" class="form-control">
                    <input type="hidden" id="project_id" value="{{project.id}}">
                    <div>
                        <label for="sub_project_name">工程名称：</label>
                        <input type="text" id="sub_project_name" class="form-control" placeholder="工程名称">
                    </div>
                    <div class="file-upload-area">
                        <label>工程数据：</label>
                        <div class="file-upload-box">
                            <div class="drag-drop-box" id="dragDropArea"
                             >
                            <div class="drag-text">
                                <span>点击选择文件或拖拽文件至此</span>
                                <input type="file" id="sub_project_file" class="form-control" accept=".xlsx,.xls,.csv" multiple
                                    style="display:none"
                                    onchange="handleFileSelect(event)">
                            </div>
                        </div>

                        <div class="file-list" id="fileList"></div>
                        </div>
                    </div>
                    <div>
                        <label for="delete_old_data">删除原数据：</label>
                        <input type="checkbox" id="delete_old_data" class="form-control" style="width: auto;flex: 0;">
                    </div>
                    <div>
                        <label for="work_catalog">工程类别：</label>
                        <input type="text" id="work_catalog" class="form-control" placeholder="工作目录"></input>
                    </div>
                  <!--  <div>
                        <label for="work_content">工程内容：</label>
                        <textarea id="work_content" class="form-control large" placeholder="工作内容"></textarea>
                    </div>-->
                    <!--   <div>
                       <label for="standard_version">标准版本：</label>
                       <select id="standard_version" class="form-control">
                           <option value="1">旧版</option>
                           <option value="2">新版</option>
                       </select>
                   </div>-->

            </div>
            </div>
				<div class="modal-footer">
					<button class="btn btn-success btn-large" onclick="saveSubProject()">保存</button>
					<button class="btn btn-warning btn-large" onclick="hide_modal('#modal-sub_project')">取消</button>
				</div>
			</div>
        </div>
    </div>
</body>
</html>