<template>
    <div class="base-container">
        <div class="back-icon" @click="goBack">
            <img src="@/assets/images/common/back-icon.png" />
            返回
        </div>
        <div class="custom-content">
            <div class="custom-l">
                <div class="box-title">
                    <img src="@/assets/images/common/chart-yellow.png" alt="" />
                    果树列表
                </div>
                <div class="select-wrap">
                    <el-select class="select-item" @change="handleAreaChange" v-model="areaVal" placeholder="分区" style="width: 146px">
                        <el-option label="全部分区" :value="0"></el-option>
                        <el-option v-for="(item, index) in areaOptions" :key="index" :label="item.name" :value="item.id" />
                    </el-select>
                    <el-select class="select-item" filterable @change="handleTypeChange" v-model="typeVal" placeholder="全部品类" style="width: 146px">
                        <el-option label="全部品类" :value="0"></el-option>
                        <el-option
                            v-for="(species, index) in speciesList"
                            :key="index"
                            :label="species.name"
                            :value="species.name"
                        />
                    </el-select>
                    <el-select class="select-item" v-model="ageVal" placeholder="树龄" style="width: 146px">
                        <el-option
                            v-for="item in ageOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="tree-list">
                    <div
                        class="list-item"
                        v-for="(item, index) in allTrees"
                        :key="index"
                    >
                        <div class="tree-icon">
                            <img class="tree-img" src="@/assets/images/foster-home/tree-item.png" alt="" />
                            <div class="tree-type-name-tag">{{ item.pz }}</div>
                        </div>
                        <div class="item-center">
                            <div class="center-t">
                                {{ item.fosterCode }}
                                <!-- <span class="type-tag">综合:94分</span>
                                <span class="type-tag">生态:92分</span> -->
                            </div>
                            <div class="center-item p-t-2 age-line">
                                <div class="age-wrap">
                                    <div class="has-age">
                                        <div class="age">栽种时间：<span class="unit">{{item.plantDate || '--'}}</span></div>
                                    </div>
                                    <div class="sort-line"></div>
                                </div>
                                树龄：<span class="unit">{{ (item.age && item.age+'年') || '--' }}</span>
                            </div>
                            <div class="center-item">栽种区域：<span>{{ (item.area && item.area + '区') || '--' }}</span></div>
                        </div>
                    </div>
                </div>
                <div class="pagination-wrap">
                    <el-pagination
                        background
                        v-model:current-page="currentPage"
                        v-model:page-size="pageSize"
                        :page-sizes="[10, 20, 50, 100]"
                        layout="total, sizes, prev, pager, next"
                        :total="totalVal"
                        @size-change="handleSizeChange"
                        @current-change="getSamplePage"
                        />
                </div>
            </div>
            <div class="custom-r">
                <div class="second-title">
                    <img src="@/assets/images/common/chart-yellow.png" alt="" />
                    批量编辑果树
                </div>
                <div class="custom-detail">
                    <div class="setting-item">
                        <span class="edit-label">树龄：</span>
                            <el-input-number
                                @change="settingSinglePrice"
                                class="number-input"
                                :controls="false"
                                placeholder="请输入树龄"
                                v-model="settingAge"
                                :min="0"
                            />
                            <span class="unit">年</span>
                    </div>
                    <div class="setting-item">
                        <span class="edit-label">品种：</span>
                            <el-select
                                class="blue-select"
                                v-model="settingPz"
                                filterable
                                style="width: 190px"
                                placeholder="请选择品种"
                            >
                                <el-option
                                    v-for="(species, index) in speciesList"
                                    :key="index"
                                    :label="species.name"
                                    :value="species.name"
                                />
                            </el-select>
                    </div>
                    <div class="setting-item date-picker">
                        <span class="edit-label">栽种时间：</span>
                            <!-- <el-input-number @change="settingSinglePrice" class="number-input" :controls="false" v-model="item.total" :min="0" /> -->
                        <el-date-picker
                            v-model="plantDate"
                            type="date"
                            format="YYYY-MM-DD"
                            style="width: 190px"
                            value-format="YYYY-MM-DD"
                            placeholder="请选择时间"
                        />
                    </div>
                    
                </div>
                <!-- 渐变主色按钮 -->
                <div class="btn-group">
                    <div class="btn cancel-btn">取消</div>
                    <div class="btn edit-btn" @click="settingForm">确认</div>
                </div>
            </div>
        </div>
    </div>
    <!-- 新增客户、编辑客户 -->
    <edit-client-popup ref="editClientRef"></edit-client-popup>
</template>

<script setup>
import { ref, computed, onMounted } from "vue";
import { useRouter } from "vue-router";
import editClientPopup from "@/components/editClientPopup.vue";
import { useStore } from "vuex";
import { ElMessage } from "element-plus";
let store = useStore();

const areaVal = ref(0);
const areaOptions = ref([]);
const typeVal = ref(0);
const ageVal = ref(0);
const ageOptions = ref([
    { label: "全部树龄", value: 0 },
    { label: "0-10年", value: 1 },
    { label: "10-20年", value: 2 },
]);

const currentPage = ref(1);
const pageSize = ref(20); // 每页显示20条数据
const totalVal = ref(0);

const allTrees = ref([]);

const router = useRouter();
function goBack() {
    router.go(-1);
}

onMounted(() => {
    getSamplePage()
    
    VE_API.manage_interface.speciesList({ farmId: 80865 }).then(({ data }) => {
        speciesList.value = data;
    });

    // 获取区域列表
    getBlueRegionList();
})

function getBlueRegionList() {
    VE_API.manage_interface.fetchRegionList({ farmId: 766 }).then(({ data }) => {
        areaOptions.value = data;
    });
}

function handleAreaChange() {
    currentPage.value = 1; // 重置页码
    getSamplePage();
}

function handleTypeChange() {
    currentPage.value = 1; // 重置页码
    getSamplePage();
}

function getSamplePage() {
    VE_API.manage_interface.fetchSamplePage({farmId: 80865, pz: typeVal.value ? typeVal.value : null, page: currentPage.value, limit: pageSize.value}).then(({data, count}) => {
        allTrees.value = data
        totalVal.value = count;
    })
}
function handleSizeChange(newSize) {
    pageSize.value = newSize;
    currentPage.value = 1; // 重置页码
    getSamplePage();
}


const editClientRef = ref(null);

// 批量编辑
const settingAge = ref(null);
const settingPz = ref(null);
const plantDate = ref(null);


const speciesList = ref([]);

function settingSinglePrice() {
    console.log("sss");
}

function settingForm() {
    const sampleIds = allTrees.value.map(item => item.sampleId);
    const params = {
        sampleIds,
        farmId: Number(sessionStorage.getItem("currentFarmId")),
        age: settingAge.value,
        pz: settingPz.value,
        plantDate: plantDate.value,
    };
    
    VE_API.manage_interface.batchOpenOrCloseSample(params).then(({code}) => {
        if (code === 0) {
            ElMessage.success("批量设置成功");
            getSamplePage();
            settingAge.value = null;
            settingPz.value = null;
            plantDate.value = null;
        }
    });
}
</script>

<style lang="scss" scoped>
.base-container {
    width: 100%;
    height: 100%;
    color: rgba(0, 0, 0, 0.9);
    position: relative;
    box-sizing: border-box;
    z-index: 1;

    .back-icon {
        position: absolute;
        z-index: 2;
        // left: 405px;
        right: 503px;
        top: 30px;
        cursor: pointer;
        margin-right: 20px;
        height: 40px;
        display: flex;
        align-items: center;
        box-sizing: border-box;
        padding: 0 28px;
        border: 1px solid rgba(255, 255, 255, 0.78);
        background: #2199F8;
        border-radius: 4px;
        color: #fff;
        img {
            width: 17px;
            margin-right: 10px;
        }
    }
    .custom-content {
        width: 100%;
        height: calc(100% - 36px);
        display: flex;
        justify-content: space-between;
        box-sizing: border-box;
        padding: 16px 20px 0 20px;
        .select-wrap {
            display: flex;
            align-items: center;
            .select-item + .select-item {
                margin-left: 5px;
            }
        }

        .type-tag {
            color: #ffd489;
            background: rgba(255, 212, 137, 0.06);
            padding: 2px 4px;
            font-size: 10px;
            margin-left: 4px;
            border-radius: 4px;
        }

        .custom-l {
            flex: 1;
            width: calc(100% - 473px);
            height: 100%;
            box-sizing: border-box;
            border: 1px solid rgba(0, 0, 0, 0.2);
            border-radius: 8px;
            padding: 14px 10px 22px 10px;
            position: relative;
            .box-title {
                font-size: 22px;
                display: flex;
                align-items: center;
                font-family: "PangMenZhengDao";
                margin-bottom: 16px;
                color: #2199F8;
                img {
                    margin-right: 8px;
                }
            }

            .tree-list {
                display: flex;
                flex-wrap: wrap;
                padding-top: 8px;
                max-height: calc(100% - 32px - 32px - 52px);
                overflow: auto;
            }
            .pagination-wrap {
                position: absolute;
                bottom: 12px;
                left: 50%;
                transform: translateX(-50%);

            }
        }
        .list-item {
            background: rgba(120, 120, 120, 0.05);
            padding: 8px 10px;
            border-radius: 5px;
            width: max-content;
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-right: 8px;
            margin-top: 12px;
            border: 2px solid transparent;
            cursor: pointer;
            width: calc(25% - 8px);
            box-sizing: border-box;
            color: #FFFFFF;
            &.selected {
                background: rgba(33, 153, 248, 0.1);
                border-color: #2199F8;
            }
            &.disabled {
                opacity: 0.5;
                cursor: not-allowed;
                border-color: #ccc !important;
            }
            &.right-tree-list {
                margin-top: 0px;
            }
            .item-info {
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
            .tree-icon {
                position: relative;
                .tree-type-name-tag {
                    position: absolute;
                    left: 0;
                    bottom: 0;
                    background: rgba(0, 0, 0, 0.6);
                    border-radius: 0 0 5px 5px;
                    font-size: 10px;
                    width: 100%;
                    text-align: center;
                    height: 18px;
                    line-height: 18px;
                    backdrop-filter: blur(4px);
                }
                .tree-img {
                    width: 63px;
                    height: 63px;
                    border-radius: 5px;
                    object-fit: cover;
                }
            }
            .item-center {
                flex: 1;
                padding-left: 12px;
                color: #000000;
                .center-t {
                    font-size: 14px;
                }
                .center-item {
                    color: #6c6c6c;
                    font-size: 12px;
                    padding-top: 2px;
                    span {
                        color: #000000;
                    }
                    ::v-deep {
                        .el-progress-bar__outer {
                            width: 100px;
                            background: rgba(242, 243, 245, 0.23);
                        }
                    }
                }
                
                .age-line {
                    display: flex;
                    align-items: center;
                    padding-top: 4px;
                    .age-wrap {
                        display: flex;
                        align-items: center;
                        .sort-line {
                            margin: 0 10px;
                            height: 10px;
                            width: 1px;
                            background: #6C6C6C;
                        }
                    }
                }
                .progress-wrap {
                    display: flex;
                    align-items: center;
                    .progress-text {
                        font-size: 12px;
                        color: #999999;
                        .over {
                            color: #FFD489;
                        }
                    }
                }
            }
        }
        
        .right-tree-list + .right-tree-list {
            margin-top: 12px;
        }
        .p-t-2 {
            padding-top: 2px;
        }
        .custom-r {
            width: 473px;
            height: 100%;
            box-sizing: border-box;
            margin-left: 18px;
            border: 1px solid rgba(0, 0, 0, 0.2);
            border-radius: 8px;
            .second-title {
                border-radius: 8px 8px 0 0;
                color: #2199F8;
                font-size: 22px;
                padding: 14px 12px;
                font-family: "PangMenZhengDao";
            }
            
            .user-name {
                font-size: 16px;
                color: rgba(0, 0, 0, 0.9);
                padding-bottom: 12px;
                font-weight: 500;
            }
            .custom-detail {
                padding: 0 10px;
                height: calc(100% - 60px - 60px);
                box-sizing: border-box;
                overflow: auto;
                position: relative;

                
                .number-input {
                    // margin: 0 12px;
                    ::v-deep {
                        &.el-input-number,
                        &.el-input {
                            width: 190px;
                            background: rgba(33, 153, 248, 0.08);
                            border: 1px solid #2199f8;
                            border-radius: 5px;
                        }
                        .el-input__wrapper {
                            background: transparent;
                            box-shadow: none;
                        }
                        .el-input-number.is-without-controls .el-input__wrapper {
                            padding: 0 8px;
                        }
                        .el-input__inner,
                        .el-input__inner::placeholder {
                            color: #2199f8;
                            text-align: center;
                        }
                    }
                }
                .blue-select {
                    ::v-deep {
                        .el-select__wrapper {
                            background: rgba(33, 153, 248, 0.08);
                            box-shadow: 0 0 0 1px #2199f8 inset;
                            font-size: 13px;
                            .el-select__input {
                                color: #2199f8;
                            }
                            .el-select__placeholder {
                                color: #2199f8;
                                text-align: center;
                            }
                            .el-select__caret {
                                color: #2199f8;
                            }
                        }
                    }
                }
                
                .date-picker {
                    ::v-deep {
                        .el-date-editor {
                            .el-input__wrapper {
                                background: rgba(33, 153, 248, 0.08);
                                box-shadow: 0 0 0 1px #2199f8 inset;
                            }
                            .el-input__inner,
                            .el-input__inner::placeholder {
                                color: #2199f8;
                            }
                            .el-input__prefix {
                                color: #2199f8;
                            }
                        }
                    }
                }

                .setting-item {
                    padding-bottom: 16px;
                    .edit-label {
                        display: inline-block;
                        width: 80px;
                    }
                    .unit {
                        padding-left: 4px;
                    }
                }
            }
            .total {
                padding-top: 12px;
            }
            .main-unit {
                color: #F3C11D;
            }
            .box-line {
                width: 100%;
                height: 1px;
                background: rgba(0, 0, 0, 0.2);
            }
            .selected-tree {
                // display: flex;
                margin: 12px 0;
                background: rgba(255, 255, 255, 0.04);
                padding: 0px 10px 12px 10px;
                border-radius: 8px;
                // max-height: calc(100% - 64px - 60px);
                // overflow: auto;
                box-sizing: border-box;
                .list-item {
                    width: 100%;
                    box-sizing: border-box;
                    margin-right: 0;
                }
            }
            .selected-tips {
                background: rgba(33, 153, 248, 0.1);
                border: 1px solid #2199F8;
                width: 100%;
                text-align: center;
                border-radius: 8px;
                height: 50px;
                line-height: 50px;
                color: #2199F8;
                box-sizing: border-box;
            }

            .bottom-btn {
                height: 60px;
                
            }
            .btn-group {
                display: flex;
                align-items: center;
                justify-content: end;
                height: 60px;
                border-top: 0.5px solid rgba(0, 0, 0, 0.2);
                width: 100%;
                padding: 10px 12px;
                box-sizing: border-box;
                border-radius: 0 0 8px 8px;
                .btn {
                    cursor: pointer;
                    padding: 8px 44px;
                    font-size: 16px;
                    border-radius: 4px;
                    border: 1px solid #2199F8;
                }
                .cancel-btn {
                    color: #2199F8;
                }
                .edit-btn {
                    background: #2199F8;
                    color: #fff;
                }
                .btn + .btn {
                    margin-left: 12px;
                }
            }
        }
    }
}
</style>
