<template>
    <headerTop @search="searchFn" @reset="resetFn" @export="exportFn" @add="addFn"></headerTop>
    <addCom ref="addComRef"></addCom>
    <el-card shadow="always" class="content_body_row">
        <el-table :data="tableListInfo.list" style="width: 100%" border 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="v_code" label="模板编号" min-width="100" />
            <el-table-column prop="v_applyModule" label="应用模块" min-width="120" />
            <el-table-column prop="v_attachment" label="附件" min-width="120">

                <template #default="scope">
                    <el-button type="text" @click="linkFn(scope.row)">附件</el-button>
                </template>
            </el-table-column>

            <el-table-column prop="v_isEnable" label="是否启用" min-width="220">
                <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 label="操作" width="210" align="center">

                <template #default="scope">
                    <el-button type="primary" size="small" @click.prevent="showRowFn(scope.row)">
                        查看
                    </el-button>
                    <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>
    </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 { apiServiceGreementInfoList, apiServiceExport, apiDeleteServiceGreementInfo, apiServiceGreementInfoUpdate } from "http/api/enterprise"
import { exportBlob } from "utils/index"
import moment from "moment";
import { ElMessage } from "element-plus"

const addComRef = ref()
const paginationInfo = reactive({
    pageNum: 1,
    pageSize: 15
})
let total = ref(0)

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

const showRowFn = (row: any) => {
    addComRef.value.showFn(row)
}


const editRowFn = (row: any) => {
    addComRef.value.edit({
        a: "111"
    })

}

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

    tableListInfo.list.splice(index, 1)

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

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

}

let searchParams: any = {

}
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 linkFn = (url: any) => {
    if (!url) {
        ElMessage({
            type: 'warning',
            message: "没有文件"
        })
        return
    }
    window.open(url, "_blank")
}

const exportFn = async () => {
    const res = await apiServiceExport({})
    exportBlob(res)

}

const addFn = () => {
    addComRef.value.open({
        a: "111"
    })
}

const beforeChange = (ev: any) => {
    console.log("evbeforeChange", ev)
    return new Promise((resolve, reject) => {
        //         {
        // 	"serviceGreementDto" : {
        // 		"serviceName":"开始",
        // 		"serviceId":1

        // 	}
        // 	,
        // 	"fileInfoDto":{
        // 		"fileUrl":666,
        // 		"modelId":1
        // 	}

        // }
        const params = {
            serviceGreementDto: {
                serviceId: ev.serviceId,
                status: ev.status ? 0 : 1,
            },
            fileInfoDto:{
                modelId:ev.serviceId
            }
        }

        apiServiceGreementInfoUpdate(params).then((res: any) => {
            console.log("apiBmCarouselSetsAddorupdate", res)
            if (res.code == 200) {
                resolve(true)
            } else {
                resolve(false)
            }
        }).catch(err => {
            resolve(false)
        })

    })


}
const getPageFn = (val: any) => {
    paginationInfo.pageNum = val.pageNum
    paginationInfo.pageSize = val.pageSize
    initTableList()
}

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



</script>

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