<template>
    <headerTop @search="searchFn" @reset="resetFn" @sort="sortFn" @add="addFn"></headerTop>
    <addCom ref="addComRef" @send="initTableList"></addCom>
    <el-card shadow="always" class="content_body_row">
        <div class="content_body_table">
            <el-table :data="tableListInfo.list" border style="width: 100%" show-overflow-tooltip
                :max-height="'calc(100vh - 72px - 48px - 65px - 12px - 60px - 48px)'"
                :height="'calc(100vh - 72px - 48px - 65px - 12px - 60px  - 48px)'">
                <el-table-column type="index" width="72" label="序号" />
                <!-- <el-table-column prop="date" label="标题" min-width="150" /> -->
                <el-table-column prop="name" label="轮播图" min-width="120">
                    <template #default="scope">
                        <el-image :preview-teleported="true" style="width: 32px; height:32px" :src="scope.row.carUrl"
                            :zoom-rate="1.2" :max-scale="7" :min-scale="0.2" :preview-src-list="[scope.row.carUrl]"
                            :z-index="100" fit="cover" />
                    </template>
                </el-table-column>
                <el-table-column prop="state" label="跳转页面" min-width="120">
                    <template #default="scope">
                        <el-button type="text" @click="openFn(scope.row)">
                            链接
                        </el-button>
                    </template>
                </el-table-column>

                <el-table-column   label="是否启用" min-width="320">
                    <template #default="scope">
                        <el-switch v-model.trim="scope.row.status"  :before-change="beforeChange.bind(this, scope.row)" />
                    </template>
                </el-table-column>

                <el-table-column prop="createBy" label="创建人" min-width="400" />
                <el-table-column prop="createTime" label="创建日期" min-width="120" />
                <el-table-column prop="zip" label="操作" align="center" min-width="180">

                    <template #default="scope">
                        <el-button type="primary" size="small" @click.prevent="editRowFn(scope.row)">
                            编辑
                        </el-button>
                        <el-button type="danger" size="small" @click.prevent="deleteRowFn(scope.row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="paination_out">
                <Pagination :currentPage="paginationInfo.pageNum" :pageSize="paginationInfo.pageSize" @sendPage="getPageFn">
                </Pagination>
            </div>
        </div>
    </el-card>
</template>

<script lang="ts" setup>
import headerTop from "./com/headerTop.vue"
import addCom from "./com/addCom.vue"
import { ElConfirmBeforeOpert } from "utils/elementCom"
import { apiBmCarouselSetsListDelete, apiBmCarouselSetList, apiBmCarouselSetsAddorupdate } from "http/api/config"
import { ElMessage } from 'element-plus'
import moment from "moment";
const addComRef = ref()
const paginationInfo = reactive({
    pageNum: 1,
    pageSize: 15
})
let total = ref(0)


let tableListInfo = reactive({
    list: []
})


const editRowFn = (row: any) => {
    addComRef.value.edit(row.id)

}


const deleteItem = async (row: any) => {
    const itemID = row.id
    const index = tableListInfo.list.findIndex((ele: any) => ele.id == itemID)
    const res = await apiBmCarouselSetsListDelete(itemID)
    let tipMessage = "删除成功"

    tableListInfo.list.splice(index, 1)

    ElMessage({
        type: 'success',
        message: tipMessage
    })

}

const deleteRowFn = (row: any) => {
    ElConfirmBeforeOpert(
        '操作确认',
        '是否确定删除数据？',
        deleteItem,
        '确定',
        '取消',
        row
    )

}


const openFn = (row: any) => {
    if (!row.openUrl) {
        ElMessage({
            type: 'warning',
            message: "跳转链接不能为空"
        })
        return
    }
    window.open(row.openUrl, "_blank")
}

let searchParams: any = {
    carName: ''
}
const searchFn = (params: any) => {
    searchParams = params
    console.log("searchParams", params)
    searchParams.beginTime = moment(params.dataRange[0]).format('yyyy-MM-DD')
    searchParams.endTime = moment(params.dataRange[1]).format('yyyy-MM-DD')
    initTableList()
}

const resetFn = (params: any) => {
    searchParams = params
    console.log("searchParams", params)
    searchParams.beginTime = moment(params.dataRange[0]).format('yyyy-MM-DD')
    searchParams.endTime = moment(params.dataRange[1]).format('yyyy-MM-DD')
    initTableList()
}

const sortFn = () => {

}

const addFn = () => {
    addComRef.value.open({
        a: "111"
    })
}
const getPageFn = (val: any) => {
    paginationInfo.pageNum = val.pageNum
    paginationInfo.pageSize = val.pageSize
    initTableList()
}

const initTableList = async () => {
    const params = {
        ...searchParams,
        ...paginationInfo
    }
    const res: any = await apiBmCarouselSetList(params)
    console.log("apiGetCompanyInfoSelectList", res)
    tableListInfo.list = res.rows.map((ele: any) => {
        return {
            ...ele,
            status: ele.status == 1 ? true : false
        }
    })
    // tableListInfo.list = 
    total.value = res.total
}


const beforeChange = (ev: any) => {
    console.log("evbeforeChange", ev)
    return new Promise((resolve, reject) => {
        apiBmCarouselSetsAddorupdate({
            // 0 禁用 1 启用
            status: ev.status ? 0 : 1,
            id: ev.id
        }).then((res: any) => {
            console.log("apiBmCarouselSetsAddorupdate",res)
            if (res.code == 200) {
                resolve(true)
            } else {
                resolve(false)
            }
        }).catch(err => {
            resolve(false)
        })

    })


}
onMounted(() => {
    initTableList()
})

</script>

<style scoped lang="scss"></style>
