<template>
    <view class="home-warpTwo">
        <view class="airbody">
            <view class="module">
                <view class="device-bottom" v-if="selectedPlan.length > 0">
                    <view
                        class="info-box"
                        v-for="(item, index) in selectedPlan"
                        :key="index"
                    >
                        <view class="info-text">
                            <text>{{ item.name }}</text>
                        </view>
                        <view class="edit_del">
                            <image
                                @click="deleteItem(item)"
                                src="../../static/delete.png"
                                alt=""
                            ></image>
                            <image
                                @click="editItem(item)"
                                style="margin-left: 30rpx"
                                src="../../static/edit.png"
                                alt=""
                            ></image>
                        </view>
                    </view>
                </view>
                <view v-else>
                    <view class="noPlan">暂无守护计划</view>
                </view>
            </view>
        </view>
        <view class="bottomTwo" v-if="this.planList.length > 0">
            <picker
                mode="selector"
                :range="planListName"
                @change="bindPickerChange"
                :value="selectedIndex"
            >
                <text>新增守护计划</text>
            </picker>
        </view>
        <view class="bottomTwo" v-else @click="addNoPlan">
            <text>新增守护计划</text>
        </view>

        <alarModel v-if="showModle" />
    </view>
</template>
<script>
export default {
    data() {
        return {
            devInfo: {},
            planList: [],
            planListName: [],
            selectedIndex: null,
            // 已经新增的守护计划
            selectedPlan: [],
            changFlage: false,
            showModle: false,
        };
    },
    computed: {},
    methods: {
        getPlanList() {
            this.$http
                .post("wap/alarm/plan/queryTpl", {
                    eventVal: "",
                    name: "",
                })
                .then((res) => {
                    if (res.data.code == 200) {
                        if (res.data.data.length > 0) {
                            this.planList = res.data.data;
                            this.planListName = res.data.data.map(
                                (ele) => ele.name
                            );
                        }
                    }
                });
        },
        confirmSelect() {
            if (this.selectedIndex !== null) {
                let selectePlans = this.planList.find(
                    (plan) => plan.name == this.planListName[this.selectedIndex]
                );
                let selectedPlan = {};
                selectedPlan.clientId = this.devInfo.clientId;
                if (!selectedPlan.alarmTimePlan) {
                    this.$set(selectedPlan, "alarmTimePlan", {});
                }
                selectedPlan.alarmTimePlan.startDate = this.$time(
                    new Date(),
                    1
                );
                selectedPlan.alarmTimePlan.stopDate = this.$time(
                    new Date(
                        new Date().setFullYear(new Date().getFullYear() + 1)
                    ),
                    1
                );
                selectedPlan.alarmTimePlan.timeRange =
                    selectePlans.alarmTimePlanTpl.timeRange;
                selectedPlan.alarmTimePlan.monthDays =
                    selectePlans.alarmTimePlanTpl.monthDays;
                selectedPlan.alarmTimePlan.weekdays =
                    selectePlans.alarmTimePlanTpl.weekdays;
                selectedPlan.name = selectePlans.name;
                selectedPlan.enable = 1;
                selectedPlan.linkagePushWechatService = 1;
                selectedPlan.eventVal = selectePlans.eventVal;

                // selectedPlan.alarmPlanId = selectePlans.id;

                // console.log(selectedPlan, 888888888);
                this.$http
                    .post(
                        "wap/alarm/plan/save",
                        {
                            ...selectedPlan,
                        },
                        {
                            header: {
                                "Content-Type":
                                    "application/json;charset=UTF-8",
                                token: uni.getStorageSync("tokenValue") || "",
                            },
                        }
                    )
                    .then((res) => {
                        if (res.data.code == 200) {
                            uni.showToast({
                                title: "添加计划成功",
                                icon: "success",
                                duration: 1500,
                            });
                        } else {
                            uni.showToast({
                                title: res.data.message,
                                icon: "none",
                                duration: 1500,
                            });
                        }
                        this.getSelectPlan();
                    });
            }
            this.changFlage = true;
        },
        bindPickerChange(e) {
            this.selectedIndex = e.detail.value;
            this.confirmSelect();
        },
        getSelectPlan() {
            this.$http
                .post(
                    "wap/alarm/plan/query",
                    {
                        clientId: this.devInfo.clientId,
                    },
                    {
                        header: {
                            "Content-Type": "application/json;charset=UTF-8",
                            token: uni.getStorageSync("tokenValue") || "",
                        },
                    }
                )
                .then((res) => {
                    if (res.data.code == 200) {
                        this.selectedPlan = res.data.data;
                        if (this.changFlage) {
                            this.changFlage = false;
                            this.editItem(
                                this.selectedPlan[this.selectedPlan.length - 1]
                            );
                        }
                    }
                });
        },
        deleteItem(item) {
            uni.showModal({
                content: "是否删除该计划",
                complete: (res) => {
                    if (res.confirm) {
                        this.$http
                            .post(
                                "wap/alarm/plan/del",
                                {
                                    id: item.id,
                                },
                                {
                                    header: {
                                        "Content-Type":
                                            "application/json;charset=UTF-8",
                                        token:
                                            uni.getStorageSync("tokenValue") ||
                                            "",
                                    },
                                }
                            )
                            .then((res) => {
                                if (res.data.code == 200) {
                                    uni.showToast({
                                        title: "守护计划删除成功",
                                        icon: "success",
                                        duration: 1500,
                                    });
                                } else {
                                    uni.showToast({
                                        title: res.data.message,
                                        icon: "none",
                                        duration: 1500,
                                    });
                                }
                                this.getSelectPlan();
                            });
                    }
                    if (res.cancel) {
                    }
                    return;
                },
            });
        },
        editItem(item) {
            uni.navigateTo({
                url:
                    "/pagesA/playSetting/playSetting?planInfo=" +
                    JSON.stringify(item) +
                    "&devInfo=" +
                    JSON.stringify(this.devInfo),
            });
        },
        addNoPlan() {
            uni.showModal({
                title: "提示",
                content: "暂无守护计划模版",
                success: (res) => {
                    if (res.confirm) {
                        uni.navigateBack({
                            delta: 1,
                        });
                    }
                    if (res.cancel) {
                        uni.navigateBack({
                            delta: 1,
                        });
                    }
                },
            });
        },
    },
    onLoad(options) {
        this.devInfo = JSON.parse(options.devInfo);
    },
    onShow() {
        this.showModle = true;
        this.getPlanList();
        this.getSelectPlan();
    },
    onHide() {
        this.showModle = false;
    },
    onUnload() {},
};
</script>
<style lang="less" scoped>
.home-warpTwo {
    position: relative;
    height: 100vh;
    background: linear-gradient(180deg, #ffdfdf 0%, #ffffff 30.3%);
    box-sizing: border-box;
    width: 750rpx;
    border-bottom-left-radius: 35rpx;
    border-bottom-right-radius: 35rpx;
    background: linear-gradient(180deg, #faede2 0%, #ffffff 100%);
    .airCantInfo {
        margin: 30rpx auto 0 auto;
        border: 18rpx solid #1f1f1f;
        position: relative;
    }
    .airbody {
        box-sizing: border-box;
        .header_top {
            // padding: 0rpx 40rpx;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .airTitle {
                font-weight: 500;
                color: #784c41;
                font-size: 32rpx;
                padding-left: 20rpx;
                padding-top: 20rpx;
            }
            .addfnt {
                display: flex;
                align-items: center;
                image {
                    margin-top: 7rpx;
                    width: 25rpx;
                    height: 25rpx;
                }
                .add_btn {
                    margin-left: 10rpx;
                    font-size: 32rpx;
                }
            }
        }

        .module {
            bottom: 0;
            box-sizing: border-box;
            width: 100%;
            background: #ffffff;
            border-radius: 20rpx 20rpx 20rpx 20rpx;
            .device-bottom {
                .no-data {
                    text-align: center;
                }
                .info-box {
                    display: flex;
                    align-items: center;
                    // width: 620rpx;
                    padding: 0 30rpx;
                    height: 110rpx;
                    background: #f8f8f8;
                    border-radius: 38rpx;
                    border-radius: 20rpx 20rpx 20rpx 20rpx;
                    font-family: PingFang SC, PingFang SC;
                    font-weight: 400;
                    font-size: 26rpx;
                    .info-text {
                        display: flex;
                        flex-direction: column;
                        margin-left: 20rpx;
                    }
                    image {
                        margin-bottom: 10rpx;
                        width: 75rpx;
                        height: 75rpx;
                    }
                    .edit_del {
                        margin-left: auto;
                        image {
                            width: 40rpx;
                            height: 40rpx;
                        }
                    }
                }
                .info-box:not(:first-child) {
                    margin-top: 20rpx;
                }
            }
            .noPlan {
                background-color: #f9efe5;
                padding-top: 30rpx;
                color: #999;
                font-size: 28rpx;
                text-align: center;
            }
        }
    }
    .bottomTwo {
        width: 750rpx;
        height: 120rpx;
        position: fixed;
        display: flex;
        justify-content: center;
        align-items: center;
        bottom: 0;
        left: 0;
        line-height: 120rpx;
        background: #f3e2dd;
    }

    .mask {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.4);
        z-index: 99;
    }
    .popup {
        position: fixed;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        width: 500rpx;
        background: #fff;
        border-radius: 16rpx;
        padding: 20rpx;
        z-index: 100;
        .popup-body {
            text-align: center;
            margin-bottom: 20rpx;

            .picker-text {
                padding: 20rpx;
                border-radius: 8rpx;
            }
        }
        .popup-footer {
            display: flex;
            justify-content: space-around;
            margin-top: 40rpx;
            .cancel-btn {
                width: 200rpx;
                height: 75rpx;
                background: #ffebe4;
                line-height: 75rpx;
                border-radius: 28rpx;
                font-weight: 500;
                color: #111111;
                font-size: 32rpx;
                text-align: center;
            }
            .sure-btn {
                width: 200rpx;
                height: 75rpx;
                background: #4f372e;
                line-height: 75rpx;
                border-radius: 28rpx;
                font-weight: 500;
                color: #ffffff;
                font-size: 32rpx;
                text-align: center;
            }
        }
    }
}
</style>
