<template>
    <div>
        <div class="headers">
            <div class="headers-name">资质信息｜{{ total }}</div>
            <div style="position: relative;top: 6px;">
              
                <el-form :inline="true" :model="form" class="demo-form-inline">
                    
                    <el-form-item label="有效期至" size="small" style="position: relative;top: -3px;">
                        <el-date-picker
                            ref="datePicker"
                            v-model="form.valid_until"
                            type="date"
                            placeholder="选择日期"
                            value-format="yyyy-MM-dd"
                            class="selects"
                            style="width: 180px;"
                            append-to-body
                            @change="handleDateChange"
                            @visible-change="handleDatePickerVisible">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="" size="small" style="position: relative;top: -3px;">
                        <el-input  placeholder="请输入资质名称" v-model="form.qualification_name" class="input-with-select">
                          
                            <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">
           
          <div style="flex: 1;">
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column fixed type="index" label="序号" align="center" width="80px" :index="indexMethod">
                </el-table-column>
                <el-table-column prop="qualification_name" label="资质认证名称" align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="certificate_number" label="证书编号" align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="issue_date" label="发证日期" align="center" width="120">
                </el-table-column>
                <el-table-column prop="valid_until" label="有效期至" align="center" width="120">
                </el-table-column>
                <el-table-column prop="issuing_authority" label="发证机构" align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column fixed="right" label="操作" align="center" width="80">
                    <template slot-scope="scope">
                        <el-button @click="handleClick(scope.row)" type="text" size="small">详情</el-button>
                       
                    </template>
                </el-table-column>
            </el-table>
          </div>
          <div class="external-111" v-if="tableData.length">
                <div class="bianji" v-for="(width, index) in tableData" :key="index" :style="{ width: width + 'px' }">
                    待编辑
                </div>
            </div>
        </div>
        <div class="footer">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="currentPage" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper" :total="total">
                </el-pagination>
            </div>

     
    </div>
</template>
<script>
import {list,deletes} from '@/api/resource/qualification/enterprise'
export default {
    data() {
        return {
            listDate:[
                {
                    value1:"",
                    value2:"",
                    value3:"",
                },
                {
                    value1:"",
                    value2:"",
                    value3:"",
                }
            ],
            performanceShow:false,
            input:"",
            currentPage: 1,
            pageSize: 10,
            total: 0,
            tableData: [],
            options3: [
                {
                    value: 1,
                    label: "有效"
                },
                {
                    value: 2,
                    label: "无效"
                }
            ],
            options2: [
                {
                    value: 1,
                    label: "本年 (期)"
                },
                {
                    value: 2,
                    label: "2025"
                }
            ],
            options: [
                {
                    value: 1,
                    label: "全部"
                },
                {
                    value: 2,
                    label: "2025"
                }
            ],
            form: {
                valid_until: '', // 有效期至
                qualification_name: '' // 资质名称
            }
        }
    },
    mounted() {
        this.getList()
    },
    activated() {
        // 组件被 keep-alive 缓存后再次激活时重新获取数据
        this.getList()
    },
    beforeDestroy() {
        // 清理日期选择器，避免内存泄漏和DOM访问错误
        try {
            const datePicker = this.$refs.datePicker
            if (datePicker) {
                // 关闭日期选择器弹出层
                if (datePicker.pickerVisible) {
                    datePicker.pickerVisible = false
                }
                // 移除焦点
                if (datePicker.$el) {
                    const input = datePicker.$el.querySelector('input')
                    if (input) {
                        input.blur()
                    }
                }
            }
        } catch (e) {
            // 忽略清理错误，防止报错影响组件销毁
            console.warn('DatePicker cleanup warning:', e)
        }
    },
    methods: {
        // 获取列表数据
        async getList() {
            try {
                const params = {
                    page: this.currentPage,
                    limit: this.pageSize,
                    orderBy: 'id',
                    orderDir: 'desc'
                }
                // 添加筛选条件
                if (this.form.valid_until) {
                    params.valid_until = this.form.valid_until // 有效期至，格式YYYY-MM-DD
                }
                if (this.form.qualification_name) {
                    params.qualification_name = this.form.qualification_name // 资质名称
                }
                const res = await list(params)
                if (res && res.code === 1 && res.data) {
                    this.tableData = res.data.data || []
                    this.total = res.data.total || 0
                } else {
                    this.tableData = []
                    this.total = 0
                }
            } catch (error) {
                console.error('获取列表失败:', error)
                this.$message.error('获取列表失败，请稍后重试')
            }
        },
        // 日期变化
        handleDateChange() {
            this.currentPage = 1
            this.getList()
        },
        // 日期选择器可见性变化
        handleDatePickerVisible(val) {
            // 处理日期选择器显示/隐藏
            if (!val && this.$refs.datePicker) {
                // 当日期选择器关闭时，确保正确清理
                setTimeout(() => {
                    if (this.$refs.datePicker && this.$refs.datePicker.$el) {
                        try {
                            this.$refs.datePicker.$el.blur()
                        } catch (e) {
                            // 忽略错误
                        }
                    }
                }, 100)
            }
        },
        // 搜索
        handleSearch() {
            this.currentPage = 1
            this.getList()
        },
        //添加
        btnAdd(){
            this.$router.push('/enterpriseEdit')
        },
        // 序号计算方法
        indexMethod(index) {
            return (this.currentPage - 1) * this.pageSize + index + 1
        },
        // 分页
        handleSizeChange(val) {
            this.pageSize = val
            this.currentPage = 1
            this.getList()
        },
        handleCurrentChange(val) {
            this.currentPage = val
            this.getList()
        },
        // 详情
        handleClick(row) {
            if (row && row.id) {
                this.$emit('getDetail', row)
            }
        },
        // 删除
        async handleDelete(row) {
            if (!row.id) {
                this.$message.warning('数据异常，无法删除')
                return
            }
            this.$confirm('确认删除这条记录吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                try {
                    const res = await deletes(row.id)
                    if (res && res.code === 1) {
                        this.$message.success('删除成功')
                        this.getList()
                    } else {
                        this.$message.error(res?.msg || '删除失败，请稍后重试')
                    }
                } catch (error) {
                    console.error('删除失败:', error)
                    this.$message.error('删除失败，请稍后重试')
                }
            }).catch(() => {})
        }
    }
}
</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>
.bianji{
    width: 20px;
    /* height: 19%; */
    background: #2AB95F;
    color: white;
    margin-top: 14px;
    text-align: center;
    font-size: 11px;
    line-height: 14px;
    cursor: pointer;
}
.external-111 {
    width: 90px;
    position: relative;
    top: 43px;
}

.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
}
.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;
}
.input-with-select{
    border: 1px solid #3333;
}

.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;
}

.content-table {
    display: flex;
}

.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
}
</style>