<template>
    <div>
        <div class="user-search">
            <el-input style="width: 200px;margin-right: 10px;" v-model="query.title" placeholder="请输入文章标题" />
            <el-date-picker v-model="dateTime" type="daterange" range-separator="至" start-placeholder="开始日期"
                end-placeholder="结束日期" value-format="YYYY-MM-DD" @change="handleDateChange" clearable
                style="margin-right: 10px;" />
            <el-button type="primary" icon="Search" @click="handleSearch">搜索</el-button>
            <el-button icon="Refresh" @click="handleReset">重置</el-button>
            <el-button type="primary" @click="handleBatchDelete" :disabled="selectedRows.length === 0">批量删除</el-button>
            <el-button style="float: right;" type="primary" @click="addNews">添加文章</el-button>
        </div>
        <div class="user-table">
            <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange" row-key="id">
                <el-table-column type="selection" width="55" align="center" reserve-selection />
                <el-table-column prop="title" label="文章标题" />
                <el-table-column prop="content" label="患教图片">
                    <template #default="scope">
                        <span v-if="!scope.row.cover_image">--</span>
                        <el-image v-else style="width: 36px; height: 36px" :src="scope.row.cover_image" :zoom-rate="1.2" :max-scale="7"
                            :min-scale="0.2" :preview-src-list="[scope.row.cover_image]" fit="cover" preview-teleported />
                    </template>
                </el-table-column>
                <el-table-column prop="created_at" label="创建时间" />
                <el-table-column label="操作" width="100" align="center">
                    <template #default="scope">
                        <el-link type="primary" @click="editNews(scope.row)">
                            <el-icon type="primary">
                                <Edit />
                            </el-icon>
                        </el-link>
                        <el-popconfirm title="确定删除吗？" @confirm="handleDeleteNews(scope.row)">
                            <template #reference>
                                <el-link type="danger">
                                    <el-icon type="danger">
                                        <Delete />
                                    </el-icon>
                                </el-link>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
            <div class="user-pagination">
                <el-pagination background layout="total, sizes, prev, pager, next, jumper" :total="total"
                    :page-size="query.page_size" :current-page="query.page" @size-change="handleSizeChange"
                    @current-change="handleCurrentChange" />
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { news, deleteNews } from '@/api/news';
const router = useRouter()
const tableData = ref([
    {
        title: '文章标题',
        author: '张三',
        createTime: '2021-01-01',
        updateTime: '2021-01-01',
    },
])
const search = ref('')
const addNews = () => {
    router.push('/addNew')
}
const editNews = (row) => {
    sessionStorage.setItem('works', row.content);
    sessionStorage.setItem('title', row.title);
    sessionStorage.setItem('cover_image', row.cover_image);
    router.push(`/addNew?id=${row.id}`)
}

const handleDeleteNews = (row) => {
    deleteNews({ ids: `${row.id}` }).then(res => {
        ElMessage.success('删除成功')
        getNews()
    }).catch(err => {
        console.log(err)
    })
}

const selectedRows = ref([])
const handleSelectionChange = (selection) => {
    selectedRows.value = selection
}
const handleBatchDelete = () => {
    deleteNews({ ids: selectedRows.value.map(item => item.id).join(',') }).then(res => {
        ElMessage.success('删除成功')
        getNews()
    }).catch(err => {
        console.log(err)
    })
}
const dateTime = ref([])
const handleDateChange = () => {
    query.value.start_time = dateTime.value[0]
    query.value.end_time = dateTime.value[1]
    getNews()
}
const query = ref({
    page: 1,
    page_size: 20,
    title: '',
    start_time: '',
    end_time: '',
})
const handleReset = () => {
    dateTime.value = []
    query.value = {
        username: '',
        mobile: '',
        operative_date: '',
        next_follow_date: '',
        risk_type: '',
        page: 1,
        page_size: 20
    }
    getNews()
}
const total = ref(0)
const getNews = () => {
    news(query.value).then(res => {
        tableData.value = res.list
        total.value = res.total
    })
}
const handleSearch = () => {
    query.value.page = 1
    getNews()
}

const handleSizeChange = (e) => {
    query.value.page_size = e
    query.value.page = 1
    getNews()
}
const handleCurrentChange = (e) => {
    query.value.page = e
    getNews()
}
onMounted(() => {
    getNews()
})
</script>

<style scoped>
.user-search {
    margin-bottom: 10px;
}

.content-wrap {
    max-height: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
}

.el-link+.el-link {
    margin-left: 10px;
}

.user-pagination {
    margin-top: 20px;
    text-align: right;
    display: flex;
    justify-content: right;
}
</style>