<template>
    <div>
        <van-nav-bar title="查看日报" left-text="返回" @click-left="back" fixed left-arrow/>
        
        <div class="login_form">
            <van-sticky :offset-top="46">
            <van-field readonly clickable name="datetimePicker" :value="nowTime" label="时间选择" placeholder="点击选择时间" @click="showPicker = true"/>
            </van-sticky>
            <van-popup v-model="showPicker" position="bottom">
                <van-datetime-picker v-model="currentDate" type="date" :min-date="minDate" :max-date="maxDate" @confirm="changeTime" @cancel="showPicker = false"/>
            </van-popup>
            <van-skeleton  :v-if="report.length!=0" v-for="(item,index) in report" title avatar :row="3" :loading="false" :key="index">
                <van-panel class="one_report" :title="item.name" :status="statusTxt[item.state]">
                    <div class="form_text">
                        <span style="margin-right:20px;margin-left:5px;font-size:14px;">
                            <!-- <i v-if="parseFloat(item.reportTime)>parseFloat(item.calculateTime)+0.5" 
                                style="color:red;margin-right:8px;" class="fa fa-exclamation-triangle"></i> -->
                            总填报：
                            <span :style="parseFloat(item.reportTime)>parseFloat(item.calculateTime)+0.5?'color:red':''">{{item.reportTime}}h</span>
                        </span>
                        <!-- <span>系统智能统计：{{item.calculateTime}}h</span> -->
                    </div>
                    <div v-for="(item1,index1) in item.data" class="one_report_data" :key="index1">
                        <div class="project_title">项目：{{item1.project}}</div>
                        <div class="project_title" v-if="item1.taskId != null" >任务：{{item1.taskName}}</div>
                        <div class="project_time">时长：
                            <span v-if="item1.reportTimeType == 0" style="margin-right:10px;">{{fullDayTxt[item1.timeType]}}</span>
                            <span v-if="item1.reportTimeType == 2" style="margin-right:10px;">{{item1.startTime+'-'+item1.endTime}}</span>{{item1.time}}h
                            <div class="button" v-if="item1.isOvertime == 1">加班</div>
                        </div>
                        <div class="project_content">事项：<span v-html="item1.content"></span></div>
                        <van-divider />
                    </div>
                </van-panel>
            </van-skeleton>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                hasWaiting: false,
                state: 0,
                user: JSON.parse(localStorage.userInfo),
                minDate: new Date(2010, 0, 1),
                maxDate: new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate()),
                currentDate: new Date(),
                nowTime: this.format(new Date(new Date()),"yyyy-MM-dd"),
                showPicker: false,
                report: [],
                fullDayTxt:['全天','上午','下午'],
                statusTxt:["待审核", "已通过", "已驳回", "已撤销"],
            };
        },
        created() {
        },
        methods: {
            // 返回
            back() {
                history.back();
            },

             // 时间转换
            format(date, pattern) {
                pattern = pattern || "yyyy-MM-dd";
                var _this = this;
                return pattern.replace(/([yMdhsm])(\1*)/g, function ($0) {
                    switch ($0.charAt(0)) {
                        case 'y': return _this.padding(date.getFullYear(), $0.length);
                        case 'M': return _this.padding(date.getMonth() + 1, $0.length);
                        case 'd': return _this.padding(date.getDate(), $0.length);
                        case 'w': return date.getDay() + 1;
                        case 'h': return _this.padding(date.getHours(), $0.length);
                        case 'm': return _this.padding(date.getMinutes(), $0.length);
                        case 's': return _this.padding(date.getSeconds(), $0.length);
                    }
                });
            },

            padding(s, len) {
                var len = len - (s + '').length;
                for (var i = 0; i < len; i++) { s = '0' + s; }
                return s;
            },

            // 改变时间
            changeTime(time) {
                this.nowTime = this.format(new Date(time),"yyyy-MM-dd");
                this.currentDate = time;
                this.showPicker = false;
                this.getReport();
            },

            // 获取日报
            getReport() {
                this.hasWaiting = false;
                const toast = this.$toast.loading({
                    forbidClick: true,
                    duration: 0
                });
                this.$axios.post("/report/getReportList", {date: this.nowTime})
                .then(res => {
                    if(res.code == "ok") {
                        toast.clear();
                        this.report = res.data;
                        console.log(this.report);
                        //计算状态
                        for (var i=0;i<this.report.length; i++) {
                            var item = this.report[i];
                            if (item.state == 0) {
                                this.hasWaiting = true;
                            }
                        }
                    } else {
                        toast.clear();
                        this.$toast.fail('获取失败:'+res.msg);
                    }
                }).catch(err=> {toast.clear();});
            },
        },

        mounted() {
            if (sessionStorage.targetDate != null) {
                this.nowTime = sessionStorage.targetDate;
                sessionStorage.removeItem('targetDate');
            }
            this.getReport();
        }
    };
</script>

<style lang="less" scoped>
    .login_form {
        margin-top: 46px;
    }

    .one_report {
        margin-bottom: 15px;
        font-size:14px;
    }

    .form_text {
        margin: 15px 0 15px;
        padding: 0 12px;
    }
    
    .form_btn {
        text-align: right;
    }

    .form_btn button {
        margin-left: 10px;
    }

    .one_report_data {
        margin-bottom: 10px;
        padding: 0 22px;
        div {
            line-height: 30px;
        }
    }
</style>
<style lang="less">
    .van-nav-bar .van-icon , .van-nav-bar__text {
        color: #20a0ff;
    }
    .button {
        float: right;
        width: 50px;
        height: 25px;
        line-height: 25px;
        text-align: center;
        border: 1px solid red;
        color: red;
        box-sizing: border-box;
        border-radius: 10px;
        font-size: 14px;
        display: flex;
        justify-content: center;
        align-items: center;
    }
</style>
