<template>
    <view class="box">
        <template v-if="shareList.length > 0">
            <view class="meauList" v-for="item in shareList" :key="item.devId">
                <view class="menu-item">
                    <view class="meanLeft">
                        <text>分享人号码</text>
                    </view>
                    <view class="meanRight">
                        <text>{{ item.sharerPhone }}</text></view
                    >
                </view>
                <view class="menu-item">
                    <view class="meanLeft">
                        <text>设备名称</text>
                    </view>
                    <view class="meanRight">
                        <text>{{ item.devName }}</text></view
                    >
                </view>
                <view class="menu-item">
                    <view class="meanLeft">
                        <text>设备序列号</text>
                    </view>
                    <view class="meanRight">
                        <text>{{ item.clientId }}</text></view
                    >
                </view>
                <view class="menu-item">
                    <view class="meanLeft">
                        <text>分享时间</text>
                    </view>
                    <view class="meanRight">
                        <text>{{ item.shareTime }}</text></view
                    >
                </view>
                <view class="menu-item">
                    <view class="meanLeft">
                        <text>分享状态</text>
                    </view>
                    <view class="meanRight">
                        <text v-if="item.state == 0" style="color: #7a5446"
                            >待确认</text
                        >
                        <text v-if="item.state == 1" style="color: #22b85e"
                            >已确认</text
                        >
                        <text v-if="item.state == 2" style="color: #ee3535"
                            >已拒绝</text
                        >
                    </view>
                </view>
                <view class="menu-bottom" v-if="item.state == 0">
                    <view class="meanLeft" @click="acceptDevice(item)"
                        >授权</view
                    >
                    <view class="meanRight" @click="refuseDevice(item)"
                        >拒绝</view
                    >
                </view>
            </view>
        </template>
        <view v-else class="noData">
            <text v-if="status === 0">暂无分享设备</text>
            <text v-else>暂无分享记录</text>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            shareList: [],
            status: null,
        };
    },
    methods: {
        getShareList() {
            uni.showLoading({
                title: "加载中...",
            });
            this.$http
                .post("wap/share/queryDevShare", {
                    userId: uni.getStorageSync("userId"),
                    state: this.status,
                })
                .then((res) => {
                    uni.hideLoading();
                    if (res.data.data) {
                        this.shareList = res.data.data;
                    } else {
                        this.shareList = [];
                    }
                });
        },
        acceptDevice(item) {
            let shareConfirmParam = {
                devId: item.devId,
                sharedUserId: item.sharedUserId,
                state: 1,
            };
            this.$http
                .post("wap/share/Confirm", shareConfirmParam, {
                    header: {
                        "Content-Type": "application/json;charset=UTF-8",
                    },
                })
                .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.getShareList();
                });
        },
        refuseDevice(item) {
            let shareConfirmParam = {
                devId: item.devId,
                sharedUserId: item.sharedUserId,
                state: 2,
            };
            this.$http
                .post("wap/share/Confirm", shareConfirmParam, {
                    header: {
                        "Content-Type": "application/json;charset=UTF-8",
                    },
                })
                .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.getShareList();
                });
        },
    },
    onShow() {
        this.getShareList();
    },

    onPullDownRefresh() {
        this.getShareList();
        setTimeout(() => {
            uni.stopPullDownRefresh();
        }, 1000);
    },
    onLoad(options) {
        const shareType = options.shareType;
        if (shareType) {
            uni.setNavigationBarTitle({
                title: shareType === "list" ? "分享记录" : "分享设备",
            });
        }
        if (shareType === "list") {
            this.status = null;
        } else if (shareType === "sure") {
            this.status = 0;
        }
    },
};
</script>

<style lang="less" scoped>
// Main Styles
.box {
    padding: 10rpx 18rpx;
    width: 100vw;
    height: 100vh;
    background: linear-gradient(180deg, #faede2 0%, #f4f4f4 100%);
    box-sizing: border-box;
    .meauList {
        width: 710rpx;
        margin: 18rpx auto;
        background: #fff;
        border-radius: 16rpx;
        box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
        overflow: hidden;
        transition: all 0.3s ease;

        .menu-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin: 0 32rpx;
            padding: 32rpx 0;
            border-bottom: 1rpx solid #f0f0f0;

            &:last-of-type {
                border-bottom: none;
            }

            .meanLeft {
                flex: 1;
                color: #666;
                font-size: 28rpx;
                font-weight: 400;
            }

            .meanRight {
                flex: 1;
                text-align: right;
                color: #333;
                font-size: 28rpx;
                font-weight: 500;
                padding-left: 20rpx;
            }
        }

        .menu-bottom {
            display: flex;
            padding: 24rpx 32rpx;
            border-top: 1rpx solid #f0f0f0;

            .meanLeft,
            .meanRight {
                flex: 1;
                height: 80rpx;
                display: flex;
                align-items: center;
                justify-content: center;
                border-radius: 8rpx;
                font-size: 30rpx;
                font-weight: 500;
                transition: all 0.2s;

                &:active {
                    opacity: 0.8;
                }
            }

            .meanLeft {
                background: #7b5245;
                color: white;
                margin-right: 16rpx;
            }

            .meanRight {
                background: fade(#ff4d4f, 10%);
                color: #ff4d4f;
                border: 1rpx solid fade(#ff4d4f, 30%);
            }
        }
    }

    .noData {
        text-align: center;
        padding: 20rpx;
        color: #999;
        font-size: 28rpx;
    }
}
</style>
