<template>
    <div class="hiddendanger-report">
      <div class="table">
            <el-tabs v-model="activeName" @tab-click="handleClick" type="card">
                <el-tab-pane label="新隐患" name="0"></el-tab-pane>
                <el-tab-pane label="已上报" name="1"></el-tab-pane>
                <el-tab-pane label="未上报" name="2"></el-tab-pane>
            </el-tabs>        
            <el-table :data="tableData">
                <el-table-column
                    type="index"
                    label="序号"
                    width="50">
                </el-table-column>
                <el-table-column
                    prop="hdangerTile"
                    label="隐患名称"
                >
                </el-table-column>
                <el-table-column
                    prop="hdangerLevel"
                    label="隐患等级"
                >
                <template slot-scope="{row}" >{{row.hdangerLevel|hdangerLevelFilter}}</template>                       
                </el-table-column>

                <el-table-column prop="hdangerDesc" label="描述" > </el-table-column>
                <el-table-column
                    prop="createdBy"
                    label="发现者"
                >
                </el-table-column>
                <el-table-column
                    prop="createdTime"
                    label="发现时间"
                >
                <template slot-scope="{row}" >{{formatDate(row.createdTime)}}</template>  
                </el-table-column>

                <el-table-column
                    prop="dangerSrcName"
                    label="所属危险源"
                >
                </el-table-column>
                <el-table-column
                    prop="handle"
                    label="操作"
                    v-if="activeName!=='1'"
                >
                    <template slot-scope="{row}" >
                        <el-button v-if="activeName!=='1'" type="primary" @click="handleReport(1,row)" size="mini">允许</el-button>
                        <el-button v-if="activeName==='0'" type="danger" @click="handleReport(2,row)" size="mini">禁止</el-button>
                    </template>                                       
                </el-table-column>                   
            </el-table>                                
        </div>
    </div>
</template>
<script>
import axios from 'axios'
  export default {
    name: "hiddendanger",
    data() {
        return {
            baseTableData:[],
            tableData:[],
            baseUrl: this.$ip,
            defaultImg: 'this.src="' + require('@/assets/image/defaultImg.png') + '"',
            pageSize:10,
            activeName:"0"
        }
    },
    filters:{
        hdangerLevelFilter(val){
            let enums={1:"重大",2:"较大",3:"一般",4:"较低"}
            return enums[val]
        }
    },
    created(){
        this.getTableList()
    },
    methods:{
        getTableList(){
            this.filterTable()
        },
        filterTable(type="0"){  
            axios({
                url:"http://www.xycqzj.com/report2plat/GetAllHdanger"
            }).then((res)=>{
                if(res){
                    let content = res.data
                    if(!content) return
                    switch(type){
                        case "0" :
                            content=content.filter((item)=>item.hdType===0)
                        break
                        case "1" :
                            content=content.filter((item)=>item.hdType===1)
                        break
                        case "2" :
                            content=content.filter((item)=>item.hdType===2)
                        break                                  
                    }
                    this.tableData=content
                }
            })
        },       
        handleClick(tab) {
            this.filterTable(tab.name)        
        },
        handleReport(type,rows){
            if(type===1){
                this.$confirm('上报后无法撤销, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios({
                        url:"http://www.xycqzj.com/report2plat/SetHdReportStatus",
                        method:'post',
                        data:{
                            hdangerId:rows.hdangerId,
                            reportType:type
                        }
                    }).then(()=>{
                        this.filterTable()
                        this.$message('上报成功！');
                    }).catch(()=>{
                        this.$message('上报失败！');                   
                    })
                }) 
            }else{
                axios({
                    url:"http://www.xycqzj.com/report2plat/SetHdReportStatus",
                    method:"post",
                    data:{
                        hdangerId:rows.hdangerId,
                        reportType:type
                    }
                }).then(()=>{
                    this.filterTable()
                    this.$message('禁止上报成功!'); 
                }).catch(()=>{
                    this.$message('禁止上报失败!');                    
                })            
            
            }       
        }
        ,formatDate(time,format='YY-MM-DD hh:mm:ss'){
            var date = new Date(time);

            var year = date.getFullYear(),
                month = date.getMonth()+1,//月份是从0开始的
                day = date.getDate(),
                hour = date.getHours(),
                min = date.getMinutes(),
                sec = date.getSeconds();
            var preArr = Array.apply(null,Array(10)).map(function(elem, index) {
                return '0'+index;
            });

            var newTime = format.replace(/YY/g,year)
                                .replace(/MM/g,preArr[month]||month)
                                .replace(/DD/g,preArr[day]||day)
                                .replace(/hh/g,preArr[hour]||hour)
                                .replace(/mm/g,preArr[min]||min)
                                .replace(/ss/g,preArr[sec]||sec);

            return newTime;			
        }  
    }
}
</script>
<style src="./report.css"></style>