<template>
    <div>
        <div class="headers">
            <div class="headers-name">校企合作明细 ｜2</div>
            <div>
              
                <el-form :inline="true" :model="form" class="demo-form-inline">
                    <el-form-item label="项目类型" size="small" style="position: relative;top: -3px;">
                        <el-select v-model="form.value3" placeholder="请选择" class="selects" @change="handleSearch">
                            <el-option v-for="item in projectTypeOptions" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="项目状态" size="small" style="position: relative;top: -3px;">
                        <el-select v-model="form.value4" placeholder="请选择" class="selects" @change="handleSearch">
                            <el-option v-for="item in projectStatusOptions" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="项目名称" size="small" style="position: relative;top: -3px;">
                        <el-input placeholder="请输入项目名称/项目编号" v-model="input3" class="input-with-select" @keyup.enter.native="handleSearch">
                            <el-button slot="append" icon="el-icon-search" @click="handleSearch"></el-button>
                        </el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="btnAdd" size="small" class="tianjia">添加项目</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <div class="content-table">
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column fixed type="index" label="序号" align="center" width="80" :index="getIndex">
                </el-table-column>
                <el-table-column prop="project_code" label="项目代号" align="center">
                </el-table-column>
                <el-table-column prop="project_name" label="项目名称" width="300px">
                </el-table-column>
                <el-table-column prop="project_type" label="项目类型" align="center">
                    <template slot-scope="scope">
                        {{ formatProjectType(scope.row.project_type) }}
                    </template>
                </el-table-column>
                <el-table-column prop="level" label="部门" align="center">
                    <template slot-scope="scope">
                        {{ formatLevel(scope.row.level) }}
                    </template>
                </el-table-column>
                <el-table-column prop="launch_date" label="立项时间" align="center">
                    <template slot-scope="scope">
                        {{ formatDate(scope.row.launch_date) }}
                    </template>
                </el-table-column>
                <el-table-column label="项目起至时间" align="center" width="240px">
                    <template slot-scope="scope">
                        {{ formatDate(scope.row.start_date) }} 至 {{ formatDate(scope.row.end_date) }}
                    </template>
                </el-table-column>
                <el-table-column prop="data_completeness" label="资料完整度" align="center">
                </el-table-column>
                <el-table-column fixed="right" label="操作" align="center" width="160">
                    <template slot-scope="scope">
                        <el-button @click="handleClick(scope.row)" type="text" size="small">主页</el-button>
                        <el-button @click="showDeleteDialog(scope.row)" type="text" size="small" style="color: #F56C6C;">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="footer">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="currentPage" :page-sizes="[100, 200, 300, 400]" :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper" :total="total">
                </el-pagination>
            </div>
        </div>

     
    <!-- 添加部门 -->
    <el-dialog :visible.sync="dialogVisible" width="60%">
            <div class="dialog-header">添加部门</div>
            <div class="content-table">
                       <div class="content-areas">
                         <div class="contents-left">
                           <span style="margin-left: 20px;">项目名称</span>
                         </div>
                         <div class="contents-right">
                            <span style="margin-left: 20px;">
                                <el-input v-model="input" placeholder="请输入内容" style="width: 70%;"></el-input>
                            </span>
                         </div>
                       </div>
                      
                       <div class="content-areas content-areas2" style="top: 2px;">
                         <div class="contents-left" style="border-top: none;">
                           <span class="table-name">项目代号</span>
                         </div>
                         <div class="contents-right" style="border: none;" >
                            <div class="rightss rightss1" ><span class="table-name">
                                <el-input v-model="input" placeholder="请输入内容" ></el-input>
                            </span></div>
                            <div class="rightss rightss2" style="border-top: none;"><span class="table-name">立项时间</span></div>
                            <div class="rightss rightss3" ><span class="table-name">
                                <el-date-picker
                               
                                v-model="value1"
                                type="date"
                                placeholder="选择日期">
                                </el-date-picker>
                            </span></div>
                         </div>
                       </div>
                      <div class="content-areas content-areas2" style="top: 4px;">
                         <div class="contents-left" style="border-top: none;">
                           <span class="table-name">项目开始时间</span>
                         </div>
                         <div class="contents-right" style="border: none;">
                            <div class="rightss rightss1" ><span class="table-name">
                                <el-date-picker
                                v-model="value1"
                                type="date"
                                placeholder="选择日期">
                                </el-date-picker>
                            </span></div>
                            <div class="rightss rightss2" style="border-top: none;"><span class="table-name">项目是否结束</span></div>
                            <div class="rightss rightss3" ><span class="table-name">
                                <el-select v-model="value" placeholder="请选择" class="selects2">
                                    <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                    </el-option>
                                </el-select>
                            </span></div>
                         </div>
                       </div>
                       <div class="content-areas content-areas2" style="top: 6px;">
                         <div class="contents-left" style="border-top: none;">
                           <span class="table-name">项目结束时间</span>
                         </div>
                         <div class="contents-right" style="border: none;">
                            <div class="rightss rightss1" ><span class="table-name">
                                <el-date-picker
                                v-model="value1"
                                type="date"
                                placeholder="选择日期">
                                </el-date-picker>
                            </span></div>
                            <div class="rightss rightss2" style="border-top: none;"><span class="table-name">项目类型</span></div>
                            <div class="rightss rightss3" ><span class="table-name">
                                <el-select v-model="value5" placeholder="请选择" class="selects2">
                                    <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                    </el-option>
                                </el-select>
                            </span></div>
                         </div>
                       </div>
                       <div class="content-areas content-areas2" style="top: 8px;">
                         <div class="contents-left" style="border-top: none;">
                           <span class="table-name">部门</span>
                         </div>
                         <div class="contents-right" style="border: none;">
                            <div class="rightss rightss1" ><span class="table-name">
                                <el-select v-model="value6" placeholder="请选择" class="selects2">
                                    <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                    </el-option>
                                </el-select>
                            </span></div>
                            <div class="rightss rightss2" style="border-top: none;"><span class="table-name">级别</span></div>
                            <div class="rightss rightss3" ><span class="table-name">
                                <el-select v-model="value7" placeholder="请选择" class="selects2">
                                    <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                    </el-option>
                                </el-select>
                            </span></div>
                         </div>
                       </div>
                       
                  
                    </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false" size="small">取 消</el-button>
                <el-button type="primary" style="background-color: #5165f7;" @click="dialogVisible = false" size="small">确 定</el-button>
            </div>
        </el-dialog>

        <!-- 删除确认对话框 -->
        <el-dialog :visible.sync="deleteDialogVisible" width="400px" :show-close="false" center>
            <div class="delete-dialog-content">
                <img src="@/assets/err.png" alt="删除" class="delete-icon" />
                <div class="delete-title">确认删除</div>
                <div class="delete-message">确定要删除该项目吗？删除后无法恢复。</div>
                <div class="delete-buttons">
                    <el-button @click="cancelDelete">取消</el-button>
                    <el-button type="danger" @click="confirmDelete">确定删除</el-button>
                </div>
            </div>
        </el-dialog>
     
    </div>
</template>
<script>
import { list, deletes } from '@/api/projectmanagement/ProjectStatus'
export default {
    data() {
        return {
            input3:"",
            value1:"",
            value4:"",
            value5:"",
            value6:"",
            value7:"",
            value:"",
            listDate:[
                {
                    value1:"",
                    value2:"",
                    value3:"",
                },
                {
                    value1:"",
                    value2:"",
                    value3:"",
                }
            ],
            performanceShow:false,
            input:"",
            dialogVisible: false,
            currentPage: 1,
            pageSize: 100,
            total: 0,
            tableData: [],
            // 删除确认框
            deleteDialogVisible: false,
            deleteRow: null, // 待删除的行数据
            // 项目类型选项
            projectTypeOptions: [
                {
                    value: '',
                    label: '全部'
                },
                {
                    value: '1',
                    label: '自研项目'
                },
                {
                    value: '2',
                    label: '科研项目'
                }
            ],
            // 项目状态选项
            projectStatusOptions: [
                {
                    value: '',
                    label: '全部'
                },
                {
                    value: '0',
                    label: '已结束'
                },
                {
                    value: '1',
                    label: '未结束'
                }
            ],
            options3: [
                {
                    value: 1,
                    label: "有效"
                },
                {
                    value: 2,
                    label: "无效"
                }
            ],
            options2: [
                {
                    value: 1,
                    label: "本年 (期)"
                },
                {
                    value: 2,
                    label: "2025"
                }
            ],
            options: [
                {
                    value: 1,
                    label: "全部"
                },
                {
                    value: 2,
                    label: "2025"
                }
            ],
            form: {
                value3: "",
                value4: "",
                value: "全部",
                value2: "本年 (期)"
            }
        }
    },
    mounted() {
        this.getList()
    },
    methods: {
        // 获取列表数据
        async getList() {
            try {
                const params = {
                    page: this.currentPage,
                    limit: this.pageSize,
                    project_type: this.form.value3 || '',
                    is_finished: this.form.value4 || '',
                    project_name: this.input3 || ''
                }
                const res = await list(params)
                if (res && res.code === 1) {
                    this.tableData = res.data.data || res.data.list || []
                    this.total = res.data.total || 0
                } else {
                    this.$message.error(res?.msg || '获取列表失败')
                    this.tableData = []
                    this.total = 0
                }
            } catch (error) {
                console.error('获取列表失败:', error)
                this.$message.error('获取列表失败')
                this.tableData = []
                this.total = 0
            }
        },
        // 搜索
        handleSearch() {
            this.currentPage = 1
            this.getList()
        },
        // 分页 - 每页条数改变
        handleSizeChange(val) {
            this.pageSize = val
            this.currentPage = 1
            this.getList()
        },
        // 分页 - 当前页改变
        handleCurrentChange(val) {
            this.currentPage = val
            this.getList()
        },
        // 格式化项目类型
        formatProjectType(type) {
            if (type === 1 || type === '1') {
                return '自研项目'
            } else if (type === 2 || type === '2') {
                return '科研项目'
            }
            return '--'
        },
        // 格式化项目级别（部门）
        formatLevel(level) {
            const levelMap = {
                1: '科技部门国家',
                2: '科技部门省部',
                3: '科技部门市厅',
                4: '交通运输部门国家',
                5: '交通运输部门省部',
                6: '交通运输部门市厅',
                7: '工业和信息化部门国家',
                8: '工业和信息化部门省部',
                9: '工业和信息化部门市厅',
                10: '大数据部门国家',
                11: '大数据部门省部',
                12: '大数据部门市厅',
                13: '发展和改革委员会国家',
                14: '发展和改革委员会省部',
                15: '发展和改革委员会市厅',
                16: '其他部门国家',
                17: '其他部门省部',
                18: '其他部门市厅'
            }
            if (level === null || level === undefined || level === '') {
                return '--'
            }
            const levelNum = typeof level === 'string' ? parseInt(level) : level
            return levelMap[levelNum] || '--'
        },
        // 格式化日期
        formatDate(date) {
            if (!date) return '--'
            // 如果是Unix时间戳（秒），转换为毫秒
            if (typeof date === 'number') {
                const timestamp = date.toString().length === 10 ? date * 1000 : date
                const dateObj = new Date(timestamp)
                if (isNaN(dateObj.getTime())) return '--'
                const year = dateObj.getFullYear()
                const month = String(dateObj.getMonth() + 1).padStart(2, '0')
                const day = String(dateObj.getDate()).padStart(2, '0')
                return `${year}-${month}-${day}`
            }
            // 如果是字符串日期
            if (typeof date === 'string' && date.includes('T')) {
                return date.split('T')[0]
            }
            return date
        },
        // 计算序号
        getIndex(index) {
            return (this.currentPage - 1) * this.pageSize + index + 1
        },
        //添加
        btnAdd(){
            this.dialogVisible=true
        },
        // 主页/详情
        handleClick(row) {
            this.$emit('getDetail', row)
        },
        // 显示删除对话框
        showDeleteDialog(row) {
            this.deleteRow = row
            this.deleteDialogVisible = true
        },
        // 取消删除
        cancelDelete() {
            this.deleteDialogVisible = false
            this.deleteRow = null
        },
        // 确认删除
        async confirmDelete() {
            if (!this.deleteRow || !this.deleteRow.id) {
                this.$message.error('删除失败：缺少必要参数')
                this.deleteDialogVisible = false
                return
            }
            try {
                const res = await deletes(this.deleteRow.id)
                if (res && res.code === 1) {
                    this.deleteDialogVisible = false
                    this.$message.success('删除成功')
                    this.deleteRow = null
                    this.getList()
                } else {
                    this.$message.error(res?.msg || '删除失败')
                }
            } catch (error) {
                console.error('删除失败:', error)
                this.$message.error('删除失败')
            }
        }
    }
}
</script>
<style>
.el-tooltip__popper.is-dark {
    color: #F04B4B !important;
    background: #FCF1EB !important;
}

.el-tooltip__popper .popper__arrow {
    border-top-color: #FCF1EB !important;
    /* 上方箭头 */
}

.el-tooltip__popper .popper__arrow::after {
    border-top-color: #FCF1EB !important;
}

/* 其他方向的箭头（按需添加） */
.el-tooltip__popper[x-placement^='bottom'] .popper__arrow {
    border-bottom-color: #FCF1EB !important;
}

.el-tooltip__popper[x-placement^='left'] .popper__arrow {
    border-left-color: #FCF1EB !important;
}

.el-tooltip__popper[x-placement^='right'] .popper__arrow {
    border-right-color: #FCF1EB !important;
}
</style>
<style scoped>
.selects2{
    width: 90%;
}
.adds{
    width: 30px;
    height: 20px;
    background: #DCE0FD;
    color: #5065F6;
    position: relative;
    left: 54px;
    font-size: 14px;
    text-align: center;
    line-height: 18px;
    cursor: pointer;
}
.lies:hover .closes {
    opacity: 1;; /* 鼠标滑过 .lies 时才显示 */
}
.closes{
    background: #FCF1EB;
    width: 20px;
    height: 30px;
    position: relative;
    top: 5px;
    cursor: pointer;
    text-align: center;
    line-height: 30px;
    color: #F04B4B;
    font-size: 20px;
    opacity: 0;
}
.first-border{
    /* border-top: 1px solid #DCE0FD */
}
.liee{
    background-color: #DCE0FD;
    color: #17192C;
    text-align:center
}
.content-areas{
    display: flex;
    height: 50px;
    flex-wrap: wrap;
}
.contents-left{
    width: 30%;
    border: 1px solid #DCE0FD;
    background: #EDEFFE;;
    line-height: 50px;
    font-size: 14px;
    color: #363636;
    
}
.contents-right{
    flex: 1;
    border: 1px solid #DCE0FD;
    line-height: 50px;
    font-size: 14px;
    color: #363636;
    display: flex
}
.content-areas2{
    position: relative;
    top: 1px;

}
.table-name{
    margin-left: 20px;
}
.rightss2{
    background: #EDEFFE;
}
.rightss1{
    border-left: none !important;
    border-right: none !important;
   
}
.rightss{
    height: 100%;
    border: 1px solid #DCE0FD;
    width: 100%;
    border-top: none;

}
.lie{
    border: 1px solid #3333;
    width: 100px;
    height: 40px;
    line-height: 40px;
    border-right: none;
    border: none;
    border-bottom: 1px solid #EDEFFE
}
.lies{
display: flex;
/* border-bottom: 1px solid #EDEFFE */
}
.zhuyi{
   color: #5165f7;
    font-size: 14px;
    text-align: center;
    position: relative;
    top: -12px;
}
:deep(.el-input__wrapper) {
  box-shadow: none !important;
  padding-left: 7 !important;
  background: none !important;
}
/* 去掉内部输入框的padding */
:deep(.el-input__inner) {
    padding-left: 7 !important;
  border: none !important;
  background: none !important;
}
.th1{
    width: 120px;         /* 设置固定宽度（可以根据需要调整） */
  font-size: 13px;      /* 字体大小比默认小 */
  text-align: right;    /* 右对齐 */
  padding-right: 8px;
  border-right: 1px solid #DCE0FD;
  background: #EDEFFE !important;
  border-bottom: 1px solid #DCE0FD !important;
}
.container {
    width: 72%;
   margin: 0 auto;
}

h1 {
    color: #333;
    text-align: center;
    margin-bottom: 25px;
    font-weight: 600;
    font-size: 24px;
}

.department-table {
    width: 100%;
    border-collapse: collapse;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}
.department-table {
    width: 100%;
    border-collapse: collapse;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}
.department-table tr{
    border: 1px solid #DCE0FD;
    cursor: pointer;
}

.department-table th {
   
    /* 淡紫色表头 */
    color: #5a4a6a;
    font-weight: 600;
    text-align: left;
    padding: 16px 20px;

}

.department-table td {
    background-color: white;
    padding: 5px 20px;
    border-bottom: 1px solid #f0f0f0;
    color: #333;
}

.department-table tr:last-child td {
    border-bottom: none;
}

.department-table tr:hover td {
    background-color: #f9f9f9;
    transition: background-color 0.2s;
}

.empty-value {
    color: #999;
    font-style: italic;
}

.footer {
    text-align: center;
    margin-top: 25px;
    color: #888;
    font-size: 14px;
}

.contents {
    width: 90%;
    margin: 0 auto;
}

:deep .el-dialog__body {
    padding: 0 !important;
}

.dialog-footer {
    text-align: center;
    margin: 0 auto;
    margin-top: 12px;
}

.dialog-header {
    width: 100%;
    height: 54px;
    background: #F7F7FA;
    text-align: center;
    line-height: 54px;
    position: relative;
    top: -28px;
    color: #363636;
    font-weight: bold;
    font-size: 18px
}

:deep .el-tooltip__popper.is-dark {
    color: #F04B4B !important;
}

.footer {
    display: flex;
    justify-content: end;
    margin-top: 20px;
}

.tianjia {
    position: relative;
    top: -9px;
    background-color: #5065F6;
}

.tianjia2 {
    position: relative;
    top: -9px;
    background-color: #EDEFFE;
    color: #5065F6;
}

::v-deep .el-table thead {
    background-color: #DCE0FD !important;
    color: #363636 !important;
}

::v-deep .el-table__header th {
    background-color: #DCE0FD !important;
    color: #363636 !important;
}
.input-with-select{
    border: 1px solid #3333;
}

.content-table {
    display: flex;
    flex-direction: column;
}

.formitem {
    position: relative;
    top: -5px;
}

::deep .el-form-item__label {
    font-size: 15px !important;
}

.selects {
    width: 100px;
    border: 1px solid #3333;
}

.headers-name {
    font-size: 15px;
    position: relative;
    top: 14px;
}

.tubiao {
    position: relative;
    top: 22px;
    z-index: 99;
    left: -20px;
    cursor: pointer;
}

.headers {
    display: flex;
    justify-content: space-between
}

.delete-dialog-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px;
}

.delete-icon {
    margin-bottom: 16px;
}

.delete-title {
    font-size: 18px;
    font-weight: bold;
    color: #333;
    margin-bottom: 12px;
}

.delete-message {
    font-size: 14px;
    color: #666;
    margin-bottom: 24px;
    text-align: center;
}

.delete-buttons {
    display: flex;
    gap: 16px;
}
</style>