<template>
    <div class="header">
        <div class="title">
          <img class="logo" src="@/assets/images/common/logo.png" alt="" />
          <span>{{headerName||"三门坡镇农业AI监测平台"}}</span>
          <img class="logo-icon" src="@/assets/images/common/logo-icon.png" alt="" />
        </div>
        <!-- <div class="focus-farm" v-show="!hideSwitch">
            <el-select
                filterable
                v-model="farmVal"
                placeholder="我的关注农场"
                style="width: 189px"
                popper-class="focus-farm-select"
                @change="toggleFarm"
            >
                <el-option v-for="item in options" :key="item.organId" :label="item.name" :value="item.organId" />
            </el-select>
        </div> -->
        <div class="date" v-show="showDate">
            <el-icon size="25"><MoreFilled /></el-icon>
            <div class="time">
                <div>{{ time }}</div>
                <span>{{ getCurrentFormattedTime("date") }} {{ getCurrentDayOfWeek() }}</span>
            </div>
        </div>
    </div>
    <!-- 四个方向的阴影 -->
    <div class="page-shadow" v-show="!hideShadow">
        <div class="page-bg bg-top"></div>
        <div class="page-bg bg-right"></div>
        <div class="page-bg bg-bottom"></div>
        <div class="page-bg bg-left"></div>
    </div>
</template>

<script setup>
import { onMounted, onUnmounted, ref } from "vue";
import { useRouter } from "vue-router";
import { convertPointToArray } from "@/utils/index";
import eventBus from "@/api/eventBus";

const router = useRouter();
const props = defineProps({
    showDate: {
        type: Boolean,
        defalut: false,
    },
    hideSwitch: {
        type: Boolean,
        defalut: false,
    },
    hideShadow: {
        type: Boolean,
        defalut: false,
    },
    autoGo: {
        type: Boolean,
        default: true,
    },
    headerName: {
        type: String,
        default: "",
    },
});

const farmVal = ref(sessionStorage.getItem("farmId")*1 || "");
const toggleFarm = (val) => {
    sessionStorage.setItem("farmId", farmVal.value);
    const curGarden = options.value.filter(item =>item.organId === val)
    sessionStorage.setItem("point", curGarden[0].wkt);
    eventBus.emit('garden:organId',val)
    router.push({ name: "Home" });
};

function getCurrentFormattedTime(type) {
    const now = new Date();

    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, "0"); // Months are zero based
    const day = String(now.getDate()).padStart(2, "0");
    const hours = String(now.getHours()).padStart(2, "0");
    const minutes = String(now.getMinutes()).padStart(2, "0");
    const seconds = String(now.getSeconds()).padStart(2, "0");
    if (type === "date") {
        return `${year}.${month}.${day}`;
    } else {
        return `${hours}:${minutes}:${seconds}`;
    }
}

function getCurrentDayOfWeek() {
    const now = new Date();
    const dayOfWeek = now.getDay();
    const daysOfWeek = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];

    return daysOfWeek[dayOfWeek];
}


const options = ref([]);
const userInfo = JSON.parse(sessionStorage.getItem("userinfo"));
const getGardenList = () => {
    const location = convertPointToArray(userInfo.location);
    const params = {
        userId: userInfo.userId,
        show3dFarm: true,
        location: "113.746646,22.970229",
    };
    console.log('userInfo', userInfo);
    if(userInfo.userName === "游客"){
      options.value = [{name: "柏桥村",organId:80865, wkt: "POINT(111.010804321 21.7759452836)"}]
    }else if(userInfo.userName === "LBY"){
      options.value = [{name: "荔枝博览园",organId:766, wkt: "POINT(113.61702297075017 23.584863449735067)"}]
    }else{
      if(userInfo.userName !== "13797066447"){
        VE_API.home.userGarden(params).then((res) => {
          options.value = []
          for(let item of res.data){
            options.value.push({name: item.name,organId:item.id, wkt: item.pointWkt})
          }
          farmVal.value = options.value[0].organId
          if(props.autoGo){
            toggleFarm(options.value[0].organId)
          }
        })
      }else{
        params["vip"] = 1
        VE_API.home.userGarden(params).then((res) => {
          let resData = []
          for(let item of res.data){
            resData.push({name: item.name,organId:item.id, wkt: item.pointWkt})
          }
          options.value = resData
        });
      }
    }
};

const time = ref("");
const timer = ref(null);
onMounted(() => {
    getGardenList();
    timer.value = setInterval(() => {
        time.value = getCurrentFormattedTime(new Date());
    }, 1000);
});

onUnmounted(() => {
    timer.value = null;
});
</script>

<style lang="scss" scoped>
.header {
    width: 100%;
    height: 74px;
    display: flex;
    justify-content: space-between;
    box-sizing: border-box;
    pointer-events: all;
    position: relative;

    .title {
      width: 100%;
      height: 100%;
      font-size: 24px;
      letter-spacing: 2px;
      padding-left: 20px;
      display: flex;
      align-items: center;
      box-sizing: border-box;
      background: url("@/assets/images/common/header-bg.png") no-repeat center center / 100% 100%;
      background-size: 100% 100%;
      background-position: left center;
      .logo {
        width: 23px;
        height: 26px;
      }
      .logo-icon {
        width: 33px;
        height: 12px;
      }
      span {
        margin: 0 5px;
        cursor: pointer;
        font-family: "PangMenZhengDao";
      }
    }
    .focus-farm {
        position: absolute;
        right: 192px;
        top: 23px;
        ::v-deep {
            .el-select__wrapper {
                background: rgba(255, 212, 137, 0.2);
                box-shadow: 0 0 0 1px rgba(255, 212, 137, 0.3) inset;
                height: 50px;
                line-height: 50px;
                .el-select__caret {
                    color: #ffd489;
                }
            }
            .el-select__placeholder {
                color: #f7be5a;
                font-size: 20px;
                font-family: "PangMenZhengDao";
                text-align: center;
            }
            .el-select__input {
                color: #f7be5a;
            }
        }
    }
    .date {
        display: flex;
        align-items: center;
        position: absolute;
        right: 30px;
        top: 24px;
        .time {
            margin-left: 14px;
            line-height: 18px;
            div {
                font-family: "PangMenZhengDao";
                letter-spacing: 1px;
                font-size: 16px;
            }
            span {
                font-size: 11px;
                font-family: "SOURCEHANTIFINE";
            }
        }
    }
}
// 阴影样式
.page-bg {
    position: fixed;
    z-index: -1;
}
.bg-top {
    top: 0;
    width: 100%;
    height: 200px;
    background: linear-gradient(0deg, rgba(0, 21, 31, 0), #00151f);
}
.bg-right {
    right: 0;
    width: 600px;
    height: 100%;
    background: linear-gradient(90deg, rgba(0, 21, 31, 0), rgb(0, 21, 31, 0.7));
}
.bg-bottom {
    bottom: 0;
    width: 100%;
    height: 200px;
    background: linear-gradient(180deg, rgba(0, 21, 31, 0), rgb(0, 21, 31, 0.7));
}
.bg-left {
    left: 0;
    width: 600px;
    height: 100%;
    background: linear-gradient(270deg, rgba(0, 21, 31, 0), rgb(0, 21, 31, 0.7));
}
</style>

<style lang="less">
.focus-farm-select {
    &.el-popper.is-light {
        background: #232323;
        border-color: rgba(255, 212, 137, 0.3);
        box-shadow: 0px 0px 12px rgba(255, 212, 137, 0.3);
        .el-select-dropdown__item {
            background: none;
            color: rgba(255, 212, 137, 0.6);
        }
        .el-select-dropdown__item.is-selected {
            background: rgba(255, 212, 137, 0.2);
            color: #ffd489;
        }
    }
    &.el-popper.is-light .el-popper__arrow:before {
        background: #232323;
        border-color: rgba(255, 212, 137, 0.3);
    }
}
</style>
