<!--
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本系统已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2020 陈尼克 all rights reserved.
 * 版权所有，侵权必究！
 *
-->

<template>
    <div>
        <LHeader v-bind:text="text"></LHeader>
        <div class="card">
            <div>检查编号：{{nums.no}}</div>
            <div>检查名称：{{nums.name}}</div>
            <div>检查类型：{{nums.type}}</div>
            <div>检查开始时间：{{nums.planStartTime}}</div>
            <div>检查结束时间：{{nums.planEndTime}}</div>
            <div>检查地点：{{nums.address}}</div>
        </div>
        <div class="button-more">
            <van-button type="primary" round size="large" @click="problem(nums)" :disabled="finish=='true'?true:false">临时问题上报{{'('+nums.addTroubleNumber+')'}}</van-button>
            <van-button type="primary" round size="large" @click="formwork">检查清单模板{{'('+nums.listNumber+')'}}</van-button>
            <van-button type="primary" round size="large" @click="note" :disabled="nums.troubleNumber<1?true:false">检查问题记录{{'('+nums.troubleNumber+')'}}</van-button>
            <van-button type="info" round size="large" :disabled="finish=='true'?true:false" @click="end(nums)">结束上报</van-button>
        </div>
        <tab-bar :index="1"></tab-bar>
    </div>
</template>

<script>
import tabBar from "@/components/TabBar";
import LHeader from '@/components/header.vue'
import { getFun,postFun } from '@/service/table'
export default {
    name: 'problem',
    components: {
        "LHeader":LHeader,
        "tabBar":tabBar
    },
    data() {
        return {
            noTroubleFlag:false,//是否有问题提交参数
            id:'',//问题上报清单列表列表id
            nums:{},//问题上报清单列表
            text:'问题上报',//顶部文本
            finish:'false',//提交状态
            submitTroubleNumber:0//问题记录问题提交数量
        }
    },
    mounted() {
        //请求新数据
        if(this.$route.params.id){
            this.id=sessionStorage.getItem('id')
            this.finish=sessionStorage.getItem('finish')
            console.log(this.finish)
            this.init()
        }
        //缓存数据获取---不更新检查执行id时，获取缓存执行id
        else if(sessionStorage.getItem('id')){
            this.id=sessionStorage.getItem('id')
            this.finish=sessionStorage.getItem('finish')
            //问题记录提交时候刷新问题记录数量
            if(this.$route.query.pageName){
                this.init()
            }else if(sessionStorage.getItem('noteDelete')){
                this.init()
            }else{
                //当清单模板，问题列表，问题记录不进行提交操作返回该页面调用缓存数据
                this.nums=JSON.parse(sessionStorage.getItem('nums'))
                this.submitTroubleNumber=JSON.parse(sessionStorage.getItem('submitTroubleNumber'))
            }
        }
    },
    methods: {
        /* 请求清单列表和模板数值接口 */
        init(){
            this.$toast.loading({
                message:'加载中...',
                forbidClick: true,
                loadingType: 'spinner',
                duration: 0
            })
            this.nums=[];
            getFun('check/carry/count',{planId:this.id}).then((Response)=>{
                this.$toast.clear()
                this.submitTroubleNumber=Response.data.submitTroubleNumber
                this.nums=Response.data
                sessionStorage.setItem('deptNameList',JSON.stringify(Response.data.deptNameList))
                sessionStorage.setItem('deptIdList',JSON.stringify(Response.data.deptIdList))
                sessionStorage.setItem('projectFlag',Response.data.projectFlag)
                sessionStorage.setItem('nums',JSON.stringify(this.nums))
                sessionStorage.setItem('submitTroubleNumber',this.submitTroubleNumber)
            }).catch(()=>{
                this.$toast.clear()
                this.$toast.fail('加载失败,请稍后再试')
            })
        },
        /*  结束上报接口请求 */
        end(nums){
            if(nums.troubleNumber>0){
                if(!this.submitTroubleNumber){
                    this.$toast.fail('您需要至少提交一个问题才能结束上报！')
                    return
                }
            }
            if(this.submitTroubleNumber==0){
                this.noTroubleFlag=true
            }else{
                this.noTroubleFlag=false
            }
            this.$dialog.confirm({
                title: '提示',
                message: '您已提交'+this.submitTroubleNumber+'个问题'+'确定结束检查上报？',
            }).then(()=>{
                postFun('check/carry/trouble/finish',{planId:this.id,noTroubleFlag:this.noTroubleFlag}).then((Response)=>{
                    this.$toast.clear()
                    this.$router.push('/checked')
                }).catch(()=>{
                    this.$toast.clear()
                    this.$toast.fail('加载失败,请稍后再试')
                })
            })
        },
        problem(nums){
            var obj={
                projectFlag:nums.projectFlag,
                deptIdList:nums.deptIdList,
                deptNameList:nums.deptNameList
            }
            this.$router.push({name:'list',query:{id:this.id,obj:JSON.stringify(obj)}})
        },
        note(){
            this.$router.push({
                name:'note',
                query:{
                    id:this.nums.id,
                    finish:this.finish
                }
            })
            console.log(this.nums.id)
            sessionStorage.setItem('noteId',this.nums.id)
            sessionStorage.setItem('checkedFinish',this.finish)
        },
        formwork(){
            this.$router.push({name:'formwork',query:{id:this.id,finish:this.finish}})
        },
    },
}
</script>
<style lang="less" scoped>
    .card{
        width:90%;
        box-shadow: 0px 0px 10px 2px #F3F3F3;
        padding: 0.25rem;
        margin:0.4rem auto;
        background: white;
        border-radius: 4%;
    }
    .card div{
        font-size: 0.4rem;
        line-height: 0.8rem;
    }
    .button-more{
        text-align: center;
    }
    .van-button{
        width: 80%;
        margin-top: 0.4rem;
    }
</style>
