<template>
    <div>
        <el-card>
            <el-button type="primary" @click="() => router.push({ path: '/product/add' })">添加产品服务
            </el-button>
            <!-- 添加el-dialog组件 -->
            <el-table :data="tableData" border style="width: 100%;margin-top:20px">
                <el-table-column label="序号" width="60">
                    <template #default="scope">
                        {{ scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column label="图片" width="120">
                    <template #default="scope">
                        <img :src="scope.row.url" alt="图片" style="max-width: 100%; height: auto;">
                    </template>
                </el-table-column>
                <el-table-column prop="title" label="名称" width="180">
                    <template #default="scope">
                        {{ scope.row.title }}
                    </template>
                </el-table-column>
                <el-table-column prop="title" label="是否首页显示" width="180">
                    <template #default="scope">
                        <el-switch v-model="scope.row.isIndex" size="large" active-text="显示" inactive-text="不显示"
                            @change="updateProductsIsindex(scope.row)" />
                    </template>
                </el-table-column>
                <el-table-column prop="ptype" label="产品类型" width="180">
                    <template #default="scope">
                        {{ ptypeMapping[scope.row.ptype] || '服务' }}
                    </template>
                </el-table-column>
                <el-table-column prop="type" label="类型" width="180">
                    <template #default="scope">
                        {{ typeMapping[scope.row.type] || '其他' }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200">
                    <template #default="scope">
                        <el-button type="danger" size="small" @click="deleteProduct(scope.row.ID)">删除</el-button>
                        <!--            <el-button type="primary" size="small" @click="deleteResource(scope.row.ID)">修改</el-button>-->
                        <el-button size="small"
                            @click="() => router.push({ path: '/product/detail', query: { id: scope.row.ID } })">
                            详情
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination style="margin-top:20px" :current-page="searchForm.page" :page-size="searchForm.size"
                :total="total" @current-change="handleCurrentChange" />
        </el-card>
    </div>
</template>

<script setup>
import { onMounted, reactive, ref } from "vue";
import { ElMessage, ElMessageBox } from 'element-plus';
import { useRouter } from 'vue-router'
import resource from "../../api/resource";
import product from "../../api/product.js";
const isIndex = ref(false)
const router = useRouter();
// Dom 挂载之后
onMounted(() => {
    getProductAll();
})
// 资源数据
const ptypeMapping = {
    hardware: '硬件',
    software: '软件',
    example: '合作案列',
    4: ''
}
const typeMapping = {
    serve: '服务',
    product: '产品',
    example: '合作案列',
    4: ''
}
const productDetail = reactive({
    ID: '',
    type: '',
    isIndex: '',
    ptype: '',
    title: '',
    parentId: '',
    synopsis: '',
    detail: '',
    url: '',
    product_introduction: '',
    technical_parameters: '',
    instructions: '',
    supporting_software: '',
    optional_accessories: '',
    is_active: '',
})
const showUpload = ref(false);
const showUploadDialog = ref(false);
let tableData = ref([]);
let total = ref(0);
const toggleUpload = () => {
    showUpload.value = !showUpload.value;
};
// 搜索条件
const searchForm = reactive({
    page: 1,
    size: 10,
    desc: 'created_at desc'
})
// 获取资源列表
const getProductAll = async () => {
    const res = await product.getProductList(searchForm);
    tableData.value = res.data.Data.Data;
    total.value = res.data.Data.Size;
}
const updateProducts = async (row) => {
    const res = await product.updateProduct({ id: row.ID, isIndex: row.isIndex });
    if (res.data.Code === 200) {
        ElMessage.success("修改成功")
        // await getProductAll();
    } else {
        ElMessage.error(res.data.Msg)
    }
}
//根据id修改产品状态
const updateProductsIsindex = async (row) => {
    const res = await product.updateProductIsindex({ id: row.ID, isIndex: row.isIndex });
    if (res.data.Code === 200) {
        ElMessage.success("修改成功")
        // await getProductAll();
    } else {
        ElMessage.error(res.data.Msg)
    }
}

const handleSizeChange = (size) => {
    searchForm.size = size;
    getProductList();
}
const handleCurrentChange = (current) => {
    searchForm.page = current;
    getProductAll();
}
const searchUser = () => {
    searchForm.current = 1;
    getProductAll();
}
// 删除资源信息
const deleteProduct = (id) => {
    ElMessageBox.confirm(
        '确定要删除该资源信息吗?',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(async () => {
        const res = await product.deleteProduct({ id: id });
        if (res.data.code === 200) {
            ElMessage.success("删除成功")
            await getProductAll();
        } else {
            ElMessage.error(res.data.Msg)
        }
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '取消删除',
        })
    })
}
</script>

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