<template>
    <el-dialog
        v-model="dialogVisible"
        width="65%"
        align-center
        class="picture-preview-wrap v-dialog"
        :show-close="false"
        append-to-body
        @close="closeDialog"
    >
        <div class="picture-file">
            <div class="left-img">
                <!-- v-loading="isLoadingImg"  element-loading-background="rgba(0, 0, 0, 0.3)" -->
                <album-carousel-item
                    lbum-carousel-item
                    v-if="images"
                    :key="nameRef"
                    :name="nameRef"
                    :farmId="farmId"
                    :images="images"
                    :lock="lock"
                ></album-carousel-item>
            </div>
            <div class="file-wrap">
                <div class="file-title">
                    <img src="@/assets/images/common/chart-yellow.png" alt="" />
                    果树档案
                    <span class="tag" v-if="showTag">小农户</span>
                </div>
                <div class="file-content">

                    <div class="content-box">
                        <div class="overview-file">
                            <div class="box-title">总体档案</div>
                            <!-- <div class="base-data">
                                <div class="base-item" v-for="item in photoBaseData" :key="item.label">
                                    <span class="label">{{ item.label }}</span>
                                    <div class="value">{{ item.value }}</div>
                                </div>
                            </div> -->
                            <div class="box-form">
                                <el-form
                                    ref="ruleFormRef"
                                    :model="ruleForm"
                                    :rules="rules"
                                    label-position="left"
                                    label-width="92px"
                                >
                                    <el-form-item label="品种" prop="pz">
                                        <el-input v-model="ruleForm.pz" placeholder="请输入品种" style="width: 240px;" />
                                    </el-form-item>
                                    <el-form-item label="树龄" prop="age">
                                        <el-input-number
                                            class="number-input"
                                            :controls="false"
                                            placeholder="请输入树龄"
                                            v-model="ruleForm.age"
                                            :min="0"
                                            style="width: 240px;"
                                        />
                                    </el-form-item>
                                    <el-form-item label="栽种时间" prop="time">
                                        <el-date-picker
                                            v-model="ruleForm.time"
                                            type="date"
                                            format="YYYY.MM.DD"
                                            style="width: 240px;"
                                            value-format="YYYY.MM.DD"
                                            placeholder="请选择栽种时间"
                                        />
                                    </el-form-item>
                                    <el-form-item label="守护人" prop="user">
                                        <el-select v-model="ruleForm.user" style="width: 240px;" placeholder="请选择守护人">
                                            <el-option label="张三" value="张三" />
                                            <el-option label="张四" value="张四" />
                                        </el-select>
                                    </el-form-item>
                                </el-form>
                            </div>
                        </div>

                        <div class="overview-file">
                            <div class="box-title">产量详情</div>
                            <div class="box-wrap">
                                <div class="box-item" v-for="(item, index) in outputBox" :key="index">
                                    <div class="item-name">{{ item.name }}</div>
                                    <div class="item-val">{{ item.value }}</div>
                                </div>
                            </div>
                        </div>

                        <div class="overview-file">
                            <div class="box-title">质量详情</div>
                            <div class="box-wrap">
                                <div class="box-item" v-for="(item, index) in qualityBox" :key="index">
                                    <div class="item-name">{{ item.name }}</div>
                                    <div class="item-val">{{ item.value }}</div>
                                </div>
                            </div>
                        </div>
                    </div>

                    
                    <div class="btn-group">
                        <div class="btn cancel-btn" @click="saveEdit(0)">取消</div>
                        <div class="btn edit-btn" @click="saveEdit(1)">确认修改</div>
                    </div>
                </div>
            </div>
        </div>
    </el-dialog>
</template>

<script setup>
import { ref, computed, onMounted, reactive, onActivated } from "vue";
import "./cacheImg.js";
import AlbumCarouselItem from "./albumCarouselItem";
import { dateFormat } from "@/utils/date_util.js";
import eventBus from "@/api/eventBus";

const lock = ref(false);
const farmId = ref(766);
const nameRef = ref("");

const isLoadingImg = ref(true);
const images = ref(null);
const dialogVisible = ref(false);
// 获取当前日期
const currentDate = new Date();
// 获取当前日期的前一个月
const startDate = new Date(currentDate);
startDate.setMonth(currentDate.getMonth() - 1);

// 格式化日期
const formattedStartDate = dateFormat(startDate, "YY-mm-dd");
const formattedEndDate = dateFormat(currentDate, "YY-mm-dd");

eventBus.on("change:watermark", function (name) {
    nameRef.value = name;
});

const outputBox = ref([
    { id: 1, name: "产量估计", value: "" },
    { id: 2, name: "高质果率", value: "" },
]);

const qualityBox = ref([
    { id: 5, name: "通风率", value: "" },
    { id: 6, name: "透光率", value: "" },
    { id: 7, name: "病虫比例", value: "" },
]);
const showTag = ref(false);

eventBus.off("click:point", handleClickPoint);
eventBus.on("click:point", handleClickPoint);

function handleClickPoint({ farmId, sampleId, data }) {
    console.log("click:point");
    sampleId = data.id;
    getSampleFiles(data.geoHashSample);
    photoBaseData.value[0].value = data.pz;
    isLoadingImg.value = true;
    let startDate = new Date(currentDate);
    startDate.setMonth(currentDate.getMonth() - 2);

    // 格式化日期
    let params = { sampleId, farmId, startDate: dateFormat(startDate, "YY-mm-dd") };
    VE_API.miniimage.list(params).then((res) => {
        if (res.code === 0) {
            dialogVisible.value = true;
            images.value = res.data;
            isLoadingImg.value = false;
        }
    });
    showTag.value = data.nonghu == 1 ? true : false;
}

// eventBus.off("albumCarousel", toggleActiveImg);
// eventBus.on("albumCarousel", toggleActiveImg);

// const currentIndex = ref(0);
// function toggleActiveImg(index) {
//     currentIndex.value = index;
// }

const getSampleFiles = (geoHash) => {
    VE_API.mini_farm.getSampleFiles({ geoHashSample: geoHash }).then(({ data }) => {
        photoBaseData.value[0].value = data.meta_info.type_id;
        let pj = "--";
        if (data.meta_info.crown) {
            pj = Math.sqrt(data.meta_info.crown * 1.2).toFixed(1);
        }
        photoBaseData.value[1].value = data.meta_info.crown + "平方米(蓬径" + pj + "米)";
        photoBaseData.value[2].value = data.meta_info.branch_num;

        photoBaseData.value[3].value = data.meta_info.age + "年";
        // if(data.production_info.production){
        //   data.production_info.production = (data.production_info.production * 1.3).toFixed(1)
        // }
        outputBox.value[0].value = data.production_info.production + "斤";
        outputBox.value[1].value = data.production_info.quality.toFixed(0) + "%";
        qualityBox.value[0].value = data.ecology_info.ventilation + "%";
        qualityBox.value[1].value = data.ecology_info.transmittance + "%";
        qualityBox.value[2].value = data.ecology_info.dp_situation + "%";
    });
};

const photoBaseData = ref([
    {
        label: "品种",
        value: "--",
    },
    {
        label: "冠幅表面积",
        value: "--",
    },
    {
        label: "总枝条",
        value: "--",
    },
    {
        label: "树龄",
        value: "--",
    },
]);

const activeOuput = ref(null);

function closeDialog() {
    activeOuput.value = null;
    eventBus.emit("change:watermark", "");
    eventBus.emit("resetImgIndex");
    ruleFormRef.value.resetFields()
}

// 修改档案信息

const ruleFormRef = ref();
const ruleForm = reactive({
    pz: "",
    age: null,
    time: "",
    user: "",
});

const rules = reactive({
    pz: [{ required: true, message: "请输入品种", trigger: "blur" }],
    age: [{ required: true, message: "请输入树龄", trigger: "blur" }],
    time: [
        {
            type: "date",
            required: true,
            message: "请选择栽种时间",
            trigger: "change",
        },
    ],
});

async function saveEdit(isToSave) {
    if (isToSave) {
        // 保存
        await ruleFormRef.value.validate((valid, fields) => {
            if (valid) {
                console.log('submit!')
            } else {
                console.log('error submit!', fields)
            }
        })
    }
}
</script>

<style lang="scss" scoped>
@import "src/styles/index";
.picture-file {
    display: flex;
    .left-img {
        min-width: 780px;
        min-height: 300px;
    }
    .file-wrap {
        background: url("@/assets/images/home/file-bg.png") no-repeat top center / 100% 100%;
        margin-left: 12px;
        padding: 12px;
        .file-title {
            font-size: 20px;
            color: #2199f8;
            display: flex;
            align-items: center;

            .tag {
                border: 1px solid #2199f8;
                border-radius: 4px;
                font-size: 12px;
                display: inline-block;
                width: 44px;
                height: 20px;
                text-align: center;
                line-height: 18px;
                margin-left: 3px;
                padding: 1px 4px;
            }
        }
        .overview-file {
            padding-top: 20px;
            .box-title {
                font-size: 16px;
                padding-left: 13px;
                margin-bottom: 16px;
                position: relative;
                display: flex;
                justify-content: space-between;
                color: #000000;
                &::before {
                    content: "";
                    position: absolute;
                    left: 0;
                    top: 5px;
                    width: 3px;
                    height: 16px;
                    background: #000000;
                    border-radius: 2px;
                }
            }
            .title {
                color: #f3c11d;
                font-size: 16px;
                font-family: "PangMenZhengDao";
                margin-bottom: 20px;
                .big {
                    width: 13px;
                    height: 13px;
                    margin: -10px 0 0 4px;
                }
                .small {
                    width: 7px;
                    height: 7px;
                    margin-left: -3px;
                }
            }
            .base-data {
                background: rgba(207, 207, 207, 0.1);
                border-radius: 4px;
                padding: 6px 0;
                display: flex;
                .base-item {
                    flex: 1;
                    text-align: center;
                    .label {
                        font-size: 12px;
                        color: #666666;
                    }
                    .value {
                        padding-top: 2px;
                        font-size: 16px;
                        color: #ffffff;
                    }
                }
                .base-item + .base-item {
                    border-left: 1px solid rgba(102, 102, 102, 0.42);
                }
            }
            .list {
                margin-top: 15px;
                width: max-content;
                font-size: 14px;
                .list-item {
                    color: #bbbbbb;
                    display: flex;
                    margin-bottom: 8px;
                    .list-name {
                        color: #f3c11d;
                        margin-right: 6px;
                        img {
                            width: 17px;
                            height: 13px;
                        }
                    }
                }
            }
            .box-form {
                ::v-deep {
                    .el-input-number .el-input__inner {
                        text-align: left;
                    }
                    .el-form-item__label {
                        font-size: 16px;
                    }
                    .el-form-item--default {
                        margin-bottom: 12px;
                    }
                }
            }
        }
        .btn-group {
            margin-top: 20px;
            display: flex;
            align-items: center;
            justify-content: end;
            width: 100%;
            padding: 10px 12px 0 12px;
            border-top: 1px solid rgba(0, 0, 0, 0.2);
            box-sizing: border-box;
            border-radius: 0 0 8px 8px;
            .btn {
                cursor: pointer;
                padding: 10px 20px;
                font-size: 14px;
                border-radius: 4px;
                color: #2199F8;
            }
            .cancel-btn {
                color: #000000;
                background: rgba(206, 206, 206, 0.4);
            }
            .edit-btn {
                background: #2199F8;
                color: #FFFFFF;
            }
            .btn + .btn {
                margin-left: 12px;
            }
        }
        .overview-file + .overview-file {
            margin-top: 8px;
        }
        .file-content {
            height: calc(100% - 30px);
            display: flex;
            flex-direction: column;
            .content-box {
                flex: 1;
            }
        }
        .box-wrap {
            display: flex;
            .box-item {
                min-width: 109px;
                box-sizing: border-box;
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                padding: 6px;
                background: rgba(169, 169, 169, 0.1);
                border-radius: 4px;
                cursor: pointer;
                .item-name {
                    font-size: 12px;
                    color: rgba(102, 102, 102, 0.86);
                    width: max-content;
                }
                .item-val {
                    font-size: 18px;
                    color: #000000;
                    width: max-content;
                    padding-top: 3px;
                }
                &.active {
                    background: rgba(255, 212, 137, 0.16);
                    border: 1px solid #ffd489;
                    .item-name {
                        color: #bbbbbb;
                    }
                }
            }
            .box-item + .box-item {
                margin-left: 8px;
            }
        }
    }
}
</style>
<style lang="scss">
.picture-preview-wrap {
    background: none;
}
</style>
