<template>
    <div class="danger-list">
        <div class="search-input-wrap">
            <div class="search-left" :style="{width:isSearchBtn ? '88%' : '100%'}" @click="startSearch">
                <md-input-item
                        class="search-input"
                        placeholder="请搜索危险源名称"
                        v-model="searchVal"
                >
                    <md-icon name="search" slot="left" @click.stop="searchDetail"></md-icon>
                    <!--<span slot="right">取消</span>-->
                </md-input-item>
            </div>
            <span class="cancel-btn"  v-if="isSearchBtn" @click="cancelSearch">取消</span>
        </div>

        <md-scroll-view
                ref="scrollView"
                :bouncing="false"
                :scrolling-x="false"
                :auto-reflow="true"
                @end-reached="$_onEndReached"
                class="my-scroll"
        >
            <div style="padding: 0 0.15rem 0.25rem 0.15rem;">
                <!--重大-->
                <div class="list-box"  v-for="(item, index) in riskDataList" :key="index">
                    <!--<div class="list-father" :class="item.dangerSourceLevel == 1 ? 'zhongda' : item.dangerSourceLevel == 2 ? 'jiaoda' : item.dangerSourceLevel == 3 ? 'yiban' : 'di'" @click="changeStatus(index)">
                        <h4 class="tit" v-if="item.dangerSourceLevel == 1">
                            <img src="../../assets/risk_list/weixianyuan_icon_red@3x.png" alt="">
                            {{item.riskCode}}
                        </h4>
                        <h4 class="tit" v-else-if="item.dangerSourceLevel == 2">
                            <img src="../../assets/risk_list/weixianyuan_icon_orange@3x" alt="">
                            {{item.riskCode}}
                        </h4>
                        <h4 class="tit" v-else-if="item.dangerSourceLevel == 3">
                            <img src="../../assets/risk_list/weixianyuan_icon_yellow@3x.png" alt="">
                            {{item.riskCode}}
                        </h4>
                        <h4 class="tit" v-else>
                            <img src="../../assets/risk_list/weixianyuan_icon_blue@3x.png" alt="">
                            {{item.riskCode}}
                        </h4>
                        <p class="wz"> {{item.rootDangerSrcName ? item.rootDangerSrcName : item.parentDangerSrcName}}</p>
                        <i class="iconfont" :class="index === showRiskIndex ? 'iconicon_arrow_bottom' : 'iconicon_arrow_top'"></i>
                    </div>-->

                    <div class="list-father zhongda" v-if="item.dangerSourceLevel == 1" @click="changeStatus(index, item.dangerSrcId, item.dangerSourceLevel)">
                        <h4 class="tit">
                            <img src="../../assets/risk_list/weixianyuan_icon_red@3x.png" alt="">
                            {{item.dangerSrcName}}
                        </h4>
                        <p class="wz"> 重大危险源</p>
                        <i v-if="item.childDangerSrcList.length > 0" class="iconfont" :class="index === showRiskIndex ? 'iconicon_arrow_top' : 'iconicon_arrow_bottom'"></i>
                    </div>
                    <div class="list-father jiaoda" v-else-if="item.dangerSourceLevel == 2" @click="changeStatus(index, item.dangerSrcId, item.dangerSourceLevel)">
                        <h4 class="tit">
                            <img src="../../assets/risk_list/weixianyuan_icon_orange@3x.png" alt="">
                            {{item.dangerSrcName}}
                        </h4>
                        <p class="wz"> 较大危险源</p>
                        <i v-if="item.childDangerSrcList.length > 0" class="iconfont" :class="index === showRiskIndex ? 'iconicon_arrow_top' : 'iconicon_arrow_bottom'"></i>
                    </div>
                    <div class="list-father yiban" v-else-if="item.dangerSourceLevel == 3" @click="changeStatus(index, item.dangerSrcId, item.dangerSourceLevel)">
                        <h4 class="tit">
                            <img src="../../assets/risk_list/weixianyuan_icon_yellow@3x.png" alt="">
                            {{item.dangerSrcName}}
                        </h4>
                        <p class="wz"> 一般危险源</p>
                        <i v-if="item.childDangerSrcList.length > 0" class="iconfont" :class="index === showRiskIndex ? 'iconicon_arrow_top' : 'iconicon_arrow_bottom'"></i>
                    </div>
                    <div class="list-father di" v-else @click="changeStatus(index, item.dangerSrcId, item.dangerSourceLevel)">
                        <h4 class="tit">
                            <img src="../../assets/risk_list/weixianyuan_icon_blue@3x.png" alt="">
                            {{item.dangerSrcName}}
                        </h4>
                        <p class="wz"> 低危险源</p>
                        <i v-if="item.childDangerSrcList.length > 0" class="iconfont" :class="index === showRiskIndex ? 'iconicon_arrow_top' : 'iconicon_arrow_bottom'"></i>
                    </div>

                    <div class="child-box" v-if="index === showRiskIndex && item.childDangerSrcList.length > 0">
                        <div class="list-child" v-for="(cell, idx) in item.childDangerSrcList" @click="goDetails(cell.dangerSrcId, cell.dangerSourceLevel)">
                            <h4 class="child-tit">{{cell.dangerSrcName}} </h4>
                            <i class="iconfont iconarrow-right"></i>
                        </div>
                    </div>
                </div>
                <div v-if="riskDataList.length == 0">
                    <empty-data-page></empty-data-page>
                </div>
            </div>
            <md-scroll-view-more
                    slot="more"
                    :is-finished="isFinished"
            >
            </md-scroll-view-more>
        </md-scroll-view>

    </div>
</template>

<script>
    import {
        TabBar,
        Swiper,
        SwiperItem,
        ScrollView,
        ScrollViewRefresh,
        ScrollViewMore,
        Button,
        InputItem,
        Icon,
        Tag,
        Toast,
    } from 'mand-mobile'
    import {queryDangerSrcListByPage} from '@/api/risk_list';
    import {mapGetters} from 'vuex';
    import {HeadBar, EmptyDataPage} from '@/components'


    export default {
        name: "",
        components: {
            [TabBar.name]: TabBar,
            [Swiper.name]: Swiper,
            [SwiperItem.name]: SwiperItem,
            [ScrollView.name]: ScrollView,
            [ScrollViewRefresh.name]: ScrollViewRefresh,
            [ScrollViewMore.name]: ScrollViewMore,
            [Button.name]: Button,
            [InputItem.name]: InputItem,
            [Icon.name]: Icon,
            [Tag.name]: Tag,
            [HeadBar.name]: HeadBar,
            [EmptyDataPage.name]: EmptyDataPage,
        },
        data() {
            return {
                searchVal: '',
                isSearchBtn: false,
                showRiskIndex: '',
                page: 0,
                isFinished: false,
                riskDataList: [],

            }
        },
        created() {
        },
        computed: {
            ...mapGetters([
                'job_ticket_aId',
                'job_ticket_cId',
            ])
        },
        mounted(){
            if(this.$route.query.fromUrl){
                this.$store.dispatch('request_config/setBaseUrl', this.$route.query.fromUrl)
            }else{
                this.$store.dispatch('request_config/setBaseUrl', location.host)
            }
            // 所以接口传 aId=294，cId=1
            if(this.$route.query.aId && this.$route.query.cId){
                this.$store.dispatch('job_ticket/setReqConfig', {
                    aId: this.$route.query.aId,
                    cId: this.$route.query.cId
                })
                this.$_onEndReached(this.searchVal)
            }else{
                this.$store.dispatch('job_ticket/setReqConfig', {
                    aId: '',
                    cId: ''
                })
            }
        },
        methods: {
            goDetails(id, level){
                this.$router.push({
                    path: '/danger_details',
                    query: {
                        ...this.$route.query,
                        dangerId: id,
                        level: level
                    }
                })
            },
            goSearch(){

            },
            async $_onEndReached(){
                if(this.isFinished){
                    return
                }
                this.page +=1
                await queryDangerSrcListByPage({
                    aId: this.job_ticket_aId,
                    cId: this.job_ticket_cId,
                    page: this.page,
                    keyword: this.searchVal,
                    limit: 10
                }).then(res => {
                    const {code, data, count, limit, page} = res;
                    if (code === 0) {
                        const lastPage = Math.ceil(count / limit);
                        if (page >= lastPage) {
                            this.isFinished = true
                        }
                        this.riskDataList = this.riskDataList.concat(data);
                        console.log(res)
                    }
                }).catch(err => {
                })
                this.$refs.scrollView.finishLoadMore()

            },
            searchDetail(){
                console.log(this.searchVal);
                this.page = 0;
                this.isFinished = false;
                this.showRiskIndex = '';
                this.$_onEndReached();
            },
            cancelSearch(){
                this.isSearchBtn = false;
                this.page = 0;
                this.isFinished = false;
                this.showRiskIndex = '';
                this.searchVal = ''
                this.$_onEndReached();
            },
            startSearch(){
                this.isSearchBtn = true;
                this.riskDataList = [];
            },
            changeStatus(n, id, level){
                if(this.riskDataList[n].childDangerSrcList.length <= 0){
                    this.$router.push({
                        path: '/danger_details',
                        query: {
                            ...this.$route.query,
                            dangerId: id,
                            level: level
                        }
                    })
                }else{
                    /*console.log(this.showRiskIndex);
                    console.log(n);*/
                    // 第一次点击展开，第二次点击关闭
                    if(this.showRiskIndex === n){
                        this.showRiskIndex = '';
                        return;
                    }
                    this.showRiskIndex = n;
                }
            }
        }
    }
</script>

<style scoped lang="scss" type="text/scss">
    .danger-list{
        height: 100vh;
        background: #F2F2F2;
        /*padding: 0.15rem;*/
        .search-input-wrap{
            padding: 15px;
            overflow:hidden;
            .search-left{
                width: 88%;
                float: left;
            }
            .cancel-btn{
                width: 12%;
                text-align: center;
                float: right;
                font-size: 0.28rem;
                margin-top: 0.3rem;
                cursor: pointer;
            }
            .search-input {
                border-radius: 10px;
                background: #FFF;
                padding: 0 15px;
            }
        }
        .my-scroll{
            height: calc(100vh - 1.3rem);
        }
        .list-box{
            margin-bottom: 0.2rem;
            border-radius: 0.15rem;
            box-shadow: 0 0 0.15rem #d5d3cd;
            .list-father{
                position: relative;
                padding: 0.3rem 0.2rem;
                /*border-radius: 0.15rem 0.15rem 0 0;*/
                border-radius: 0.15rem;
                .tit{
                    font-size: 0.3rem;
                    color: #222222;
                    img{
                        width: 0.36rem;
                        height: 0.36rem;
                        display: inline-block;
                        vertical-align: bottom;
                        margin-right: 0.08rem;
                    }
                }
                .wz{
                    color: #222222;
                    font-size: 0.22rem;
                    margin-top: 0.25rem;
                    margin-left: 0.52rem;
                }
                >i{
                    display: inline-block;
                    position: absolute;
                    color: #C9BBBB;
                    font-size: 0.29rem;
                    top: 0.56rem;
                    right: 0.2rem;
                }
            }
            .zhongda{
                background: #FFF3F3;
                .wz{
                    color: #F01716;
                }
            }
            .jiaoda{
                background: #FFF6EE;
                .wz{
                    color: #FF7800;
                }
            }
            .yiban{
                background: #FFFBEC;
                .wz{
                    color: #FFCC00;
                }
            }
            .di{
                background: #EBF5FF;
                .wz{
                    color: #3095FD;
                }
            }
            .child-box{
                background: #FFFFFF;
                border-radius: 0 0 0.15rem 0.15rem;
                .list-child{
                    padding: 0.3rem 0.2rem;
                    border-top: solid 1px #EFEEEE;
                    font-size: 0.3rem;
                    position: relative;
                    .child-tit{
                        color: #888888;
                        font-weight: normal;
                    }
                    .child-wz{
                        margin-top: 0.17rem;
                    }
                    i{
                        display: inline-block;
                        position: absolute;
                        color: #C9BBBB;
                        font-size: 0.29rem;
                        top: 0.38rem;
                        right: 0.2rem;
                    }
                }
                .list-child:last-of-type{
                    border-radius: 0 0 0.15rem 0.15rem;
                }
            }


        }

    }
</style>