
<template>
    <div>
        <div style=" font-size: 24px ;text-align: center;color:black">个人信息及日常安排</div>
        <el-calendar style="width: 650px;height: 100px;">
            <template
                    slot="dateCell"
                    slot-scope="{date, data}">
                <div style="min-height: 60px; word-break:break-all; "  @click="handleClick(data.day)" @mouseover="handleMouse(data.day)" >
                   <div>{{ data.day.split('-').slice(1).join('-') }}</div>
                  <div v-if="arr.find(v=>v.date===data.day)" v-html="arr.find(v=>v.date===data.day).content"></div>
                </div>
            </template>
        </el-calendar>
    <el-card style="width: 500px;float: right;margin-right: 100px; color: black; background-color: #bffcf5;">
        <el-form label-width="80px" size="small" :rules="rules" ref="userForm" :model="form" status-icon="true">
            <el-upload
                class="avatar-uploader"
                :action="'http://' + serverIp +':9090/file/upload'"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :headers="importHeaders"
        >
            <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>

            <el-form-item label="用户名" >
                <el-input v-model="form.username" disabled autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="昵称" prop="nickname">
                <el-input v-model="form.nickname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
                <el-input v-model="form.email" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="电话" prop="phone">
                <el-input v-model="form.phone" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="地址" prop="address">
                <el-input type="textarea" v-model="form.address" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="save">确 定</el-button>
                <el-button type="primary" size="small"  @click="goBack">返回</el-button>
            </el-form-item>
        </el-form>
    </el-card>
        <el-dialog title="日程安排" :visible.sync="dialogVisible">
            <el-form :model="calendars" label-width="100">
                <el-form-item label="日程内容" >
                    <el-input v-model="calendars.content" autocomplete="off" style="width: 85%"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="save1">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog title="今天要做的事" :visible.sync="visible"width="20%">
        <el-card >
         <div v-html="content" style="line-height: 30px;text-align: left;padding: 0 80px"></div>
            <el-button  type="primary" style="margin-top: 20px" @click="saveContent">我知道了</el-button>
        </el-card>
        </el-dialog>
   </div>
</template>

<script>
    import {serverIp} from "../../public/config";

    export default {
        name: "Person",
        data() {
            return {
                date:'',
                value: new Date(),
                arr:[],
                rules: {
                    nickname: [
                        { required: true, message: '请输入昵称', trigger: 'blur' },
                            //{ min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请输入电子邮箱', trigger: 'blur' },
                        //   { pattern:/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/, message: '请输入正确的邮箱格式', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, message: '请输入电话号码', trigger: 'blur' },
                        //    { pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/ ,message: '手机号码格式不正确', trigger: ['blur','change'] }
                    ],
                    address: [
                        { required: true, message: '请输入地址', trigger: 'blur' },
                       // { min: 3, max: 10, message: '长度在 3 到 10个字符',  trigger: 'blur'}
                    ],
                },
                visible:false,
                dialogVisible:false,
                serverIp: serverIp,
                form: {},
                calendars:{},
                user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
                importHeaders: {token:  JSON.parse(localStorage.getItem('user')).token}
            }
        },
        created() {
          this.load()

        },
        methods: {
            async getUser() {
                return (await this.request.get("/user/username/" + this.user.username)).data
            },
            save() {
                this.$refs['userForm'].validate((valid) => {
                    if (valid) {

                        this.request.post("/user", this.form).then(res => {
                            if (res.code === '200') {
                                this.$message.success("保存成功")

                                // 触发父级更新User的方法
                                this.$emit("refreshUser")

                                // 更新浏览器存储的用户信息
                                this.getUser().then(res => {
                                    res.token = JSON.parse(localStorage.getItem("user")).token
                                    localStorage.setItem("user", JSON.stringify(res))
                                })

                            } else {
                                this.$message.error("保存失败")
                            }

                        })
                    }
                });
            },
            load() {

                this.getUser().then(res => {
                    console.log(res)
                    this.form = res
                })

                this.request.get("/calendar").then(res =>{
                  this.arr=res.data
                  const dateStorage= localStorage.getItem('calendarVisible')
                    var date=new Date().toJSON().split('T').join(' ').substr(0,10)
                    const data=this.arr.find(v=>v.date===date)//从数据库查出的数据对象
                    if (!dateStorage||(data&&dateStorage!==data.date)){
                        //弹出提醒
                        this.content=data.content
                        this.date=data.date
                        this.visible=true

                    }

                })

            },
            save1(){
                this.request.post("/calendar", this.calendars).then(res => {
                    if (res.code === '200') {
                        this.$message.success("日程设置成功")
                        this.dialogVisible = false
                        this.load()
                    } else {
                        this.$message.error(res.msg)
                    }
                })
            },
            handleAvatarSuccess(res) {
                this.form.avatarUrl = res
            },
            goBack(){

                this.$router.go(-1);
            },
            handleClick(date){
                this.request.get("/calendar/date/"+date).then(res => {
                    if(res.data){
                        this.calendars = res.data
                    }
                    if(!res.data){
                        this.calendars={date:date}
                    }
                })
                this.dialogVisible = true
            },
            handleMouse(date){
            let content=this.arr.find(v=> v.date===date)? this.arr.find(v=> v.date===date).content : ''
            },
            saveContent(){
                this.visible=false;
                localStorage.setItem('calendarVisible',this.date)
            }
        }
    }
</script>

<style>
    .avatar-uploader {
        text-align: center;
        padding-bottom: 10px;
    }
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 138px;
        height: 138px;
        line-height: 138px;
        text-align: center;
    }
    .avatar {
        width: 138px;
        height: 138px;
        display: block;
    }
    .el-calendar__header {

        padding-top: 90px;
    }
        .el-calendar-table .el-calendar-day{
            height: auto !important;
            min-height: 60px;
            text-align: left;
        }
    .el-calendar__body {
        background-color: #f6fcff;
    }
</style>