<template>
    <headerTop @search="searchFn" @reset="resetFn" @sort="sortFn" @add="addFn" @export="exportFn"></headerTop>
    <addCom ref="addComRef" @send="initTableList"></addCom>
    <el-card shadow="always" class="content_body_row">
        <el-table :data="tableListInfo.list" border style="width: 100%" show-overflow-tooltip
            :max-height="'calc(100vh - 72px - 48px - 65px - 12px - 60px - 48px - 20px)'"
            :height="'calc(100vh - 72px - 48px - 65px - 12px - 60px  - 48px  - 20px)'"
            :header-cell-style="{ background: '#F2F3F5' }">
            <el-table-column type="index" width="72" label="序号" />
            <el-table-column prop="name" label="企业类型" min-width="150" />
            <el-table-column prop="status" label="是否启用" min-width="220">
                <template #default="scope">
                    <el-switch v-model.trim="scope.row.status" :before-change="beforeChange.bind(this, scope.row)" />
                    <!-- 1 启用
                        0 禁用  -->
                </template>
            </el-table-column>
            <el-table-column prop="zip" label="操作" align="center" width="160">

                <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" :total="total"
                @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 { ElMessage } from 'element-plus'
import { apiSelectCompanyTypeList, apiDeleteCompanyType, apiExportExcel, apiAddOrUpdateCompanyType } from "http/api/enterprise"
 
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)

}

const deleteItem = async (row: any) => {
    const itemID = row.id
    const index = tableListInfo.list.findIndex((ele: any) => ele.id == itemID)
    const res = await apiDeleteCompanyType(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
    initTableList()
}

const resetFn =  (params: any) => {
    searchParams = params
    initTableList()
}

const sortFn = () => {

}
const exportFn = async () => {
    const res = await apiExportExcel()
    console.log("res-apiExportExcel", res)
    ElMessage({
        type: 'success',
        message: "导出成功"
    })

    
}



const addFn = () => {
    addComRef.value.open({
        a: "111"
    })
}
const beforeChange = (ev: any) => {
    console.log("evbeforeChange",ev)
    return new Promise((resolve, reject) => {
        apiAddOrUpdateCompanyType({
            name: ev.name,
            status: ev.status ? '0' : '1',
            id:ev.id
        }).then((res: any) => {
            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
    }
    const res: any = await apiSelectCompanyTypeList(params)
    console.log("apiSelectCompanyTypeList", res)
    tableListInfo.list = res.rows.map((ele: any) => {
        const item = {
            ...ele,
            status: ele.status == '1' ? true : false,
            id:ele.id
        }
        return item
    })
    // tableListInfo.list = 
    total.value = res.total

}
onMounted(() => {
    initTableList()
})
</script>

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