<template>
    <div class="farm-page">
        <custom-header name="我的农场"></custom-header>
        <div class="farm-list">
            <div class="list-item" v-for="item in farmList" :key="item.id" @click="handleItemClick(item)">
                <div class="item-info">
                    <div class="item-top">
                        <img class="left-img" src="@/assets/img/home/farm.png" alt="" />
                        <div class="left-content">
                            <div class="content-title">
                                <div class="title-left">
                                    <span class="title-text van-ellipsis">{{ item.name }}</span>
                                    <div class="content-tag" v-if="item.defaultOption">默认</div>
                                    <div class="content-text" v-else @click.stop="handleSetDefault(item)">设为默认</div>
                                </div>
                                <div class="item-btn">查看详情</div>
                            </div>
                            <div class="content-desc">
                                <div>服务作物：{{ item.speciesName }}</div>
                                <div>农场位置：{{ item.address }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import customHeader from "@/components/customHeader.vue";
import { ref, onMounted } from "vue";
import { ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();
const farmList = ref([]);
onMounted(() => {
    getFarmList();
});

const handleSetDefault = async ({id}) => {
    ElMessageBox.confirm('确定将该农场设为默认农场吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        const { code } = await VE_API.farm.updateFarm({
            farmId: id,
            defaultFarm: 1,
        });  
        if(code === 0){
            getFarmList();
        }
    }).catch(() => {});
};

const getFarmList = async () => {
    const { data } = await VE_API.farm.userFarmSelectOption();
    farmList.value = data || [];
};

const handleItemClick = (item) => {
    router.push({
        path: '/monitor',
        query: {
            farmId: item.id,
            defaultFarm: item.defaultOption,
            isHeaderShow: true,
        },
    });
};
</script>

<style scoped lang="scss">
.farm-page {
    width: 100%;
    height: 100vh;
    .farm-list {
        width: 100%;
        height: calc(100% - 40px);
        background-color: #f7f7f7;
        padding: 12px;
        box-sizing: border-box;
        overflow: auto;
        .list-item {
            background-color: #fff;
            border-radius: 10px;
            padding: 10px;
            display: flex;
            width: 100%;
            box-sizing: border-box;
            .item-info {
                width: 100%;
                .item-top {
                    width: 100%;
                    display: flex;
                    align-items: center;
                    gap: 12px;
                    .left-img {
                        width: 68px;
                        height: 68px;
                        border-radius: 8px;
                    }
                    .left-content {
                        width: calc(100% - 68px - 12px);
                        .content-title {
                            display: flex;
                            align-items: center;
                            justify-content: space-between;
                            margin-bottom: 4px;
                            font-size: 16px;
                            font-weight: 500;
                            .title-left{
                                display: flex;
                                align-items: center;
                                gap: 10px;
                                width: calc(100% - 62px);
                                .title-text{
                                    max-width: calc(100% - 60px);
                                }
                                .content-tag {
                                    background-color: #2199f8;
                                    color: #fff;
                                    padding: 2px 8px;
                                    border-radius: 15px;
                                    font-size: 12px;
                                    font-weight: 400;
                                }
                                .content-text {
                                    font-size: 12px;
                                    color: #2199f8;
                                    font-weight: 400;
                                }
                            }
                        }
                        .item-btn {
                            color: #a8a8a8;
                            font-size: 14px;
                            font-weight: 400;
                        }
                        .content-desc {
                            font-size: 12px;
                            color: #999999;
                            line-height: 18px;
                        }
                    }
                }
            }
        }
        .list-item + .list-item {
            margin-top: 12px;
        }
    }
}
</style>
