<template lang="html">
  <div class="div-4">
    <div :style="{height:headheight,background:'#eaeff4'}">
      <div class="div-2">
        <div class="form-group">
          <label v-show="full_screen" class="control-label">安检类型</label>
          <v-select id="c_plan_year" :value.sync="plan_type" v-show="full_screen"
                    :value-single="true"
                    :options='plan_types' close-on-select clear-button> </v-select>
          <button class="but-1" @click="barrdios" v-show="full_screen"><img src='../../../assets/echarts6.png' class="img-1"/>&nbsp;全屏显示
          </button>
          <button @click="showall" v-show="show_all" class="but-2">返回</button>
        </div>
      </div>
    </div>
    <div v-bind:style="{height: barheight,width:'100%'}">

      <div class="flex-row" style="height: 90%;">
        <div class="span">
          <div class="form-inline" v-if="echartsshow" style="height: auto;margin-bottom: 5%;margin-top: 5%">
            <div class="form-group">
              <label for="startDate">年份:</label>
              <v-select id="c_plan_year" :value.sync="year"
                        :value-single="true"
                        :options='c_year_list' close-on-select clear-button> </v-select>
            </div>
            <div class="form-group">
              <label  class="control-label">安检类型</label>
              <v-select id="c_plan_year" :value.sync="plan_type"
                        :value-single="true"
                        :options='plan_types' close-on-select clear-button> </v-select>
            </div>
          </div>
          <div id="safedetail"></div>
        </div>
      <!--调用子组件绘图查表-->
      <div v-show="echartsshow">
        <h4  align="center" v-if="plan_type == ''">安检完成率一览表（按分公司）</h4>
        <h4  align="center" v-if="plan_type == 'false'">年度安检完成率一览表（按分公司）</h4>
        <h4  align="center" v-if="plan_type == 'true'">首次通气安检完成率一览表（按分公司）</h4>
        <criteria-paged :model="model" v-ref:crip >
          <data-grid :model="model" partial='list' id="datagridstyle1" v-ref:grid1 classname="table table-bordered table table-hover">
            <template partial='head'>
              <tr>
                <th><nobr>分公司</nobr></th>
                <th><nobr>计划量</nobr></th>
                <th><nobr>安检量</nobr></th>
                <th><nobr>完成率</nobr></th>
              </tr>
            </template>
            <template partial='body'  >
              <tr class="tr-1">
                <td class="td-1" style="text-align: center;">
                  {{row.f_subcompany}}
                </td>
                <td class="td-1" style="text-align: center;">
                  {{row.zongliang}}
                </td>
                <td class="td-1" style="text-align: center;">
                  {{row.yijian}}
                </td>
                <td class="td-1" style="text-align: center;">
                  {{row.checkrate}}
                </td>
              </tr>
            </template>
            <template partial='foot'>
              <tr>
                <td colspan="1" style="text-align: right">合计：</td>
                <td style="text-align: center"><nobr>{{$parent.$parent.$parent.count.zongliang}}</nobr></td>
                <td style="text-align: center"><nobr>{{$parent.$parent.$parent.count.yijian}}</nobr></td>
                <td style="text-align: center"><nobr>{{$parent.$parent.$parent.count.checkrate}}</nobr></td>
              </tr>
            </template>
          </data-grid>
        </criteria-paged>

      </div>
  </div>
  </div>
  </div>
</template>

<script>
  import * as Util from '../../Util'
  import co from 'co'
  import echarts from 'echarts'
  import {HttpResetClass } from 'vue-client'
  import {PagedList} from 'vue-client'


  let getData = function * (self) {
    let load = new HttpResetClass()
    self.set()
    load.load('POST','rs/sql/CheckComplete', {data: {year:self.year,orgid:self.orgid,plan_type:self.plan_type,f_safecheck_type:self.f_safecheck_type}},
      {resolveMsg: null, rejectMsg: null})
      .then((res) => {
      let legendname = []
      let zongliangArray = []
      let yijianArray = []
      let scaledata = []
      self.count.zongliang = 0
      self.count.yijian = 0
      self.count.checkrate = '0.00%'
      for (var i = 0; i < res.data.length; i++) {
      legendname[i] = res.data[i].f_subcompany
      zongliangArray[i] = res.data[i].zongliang
      yijianArray[i] = res.data[i].yijian
        self.count.zongliang += res.data[i].zongliang
        self.count.yijian += res.data[i].yijian

      var scale=( res.data[i].yijian/ res.data[i].zongliang).toFixed(4)
      scaledata.push(scale)
    }
    self.count.checkrate =  (100*self.count.yijian/ self.count.zongliang).toFixed(0)+'%'
    //掉绘图
    self.set(scaledata,legendname,zongliangArray,yijianArray)
    self.settable()
  })
  }
  export default {
    title: '用户数量分布',
    data () {
      let options = []
      let ye = (new Date()).getFullYear()
      for(let i = ye - 5 ; i <= ye; i++ )
      {
        options.push({label: i + '', value: i + ''})
      }
      return {
        full_screen: true,//控制全屏按键显示状态
        show_all: false,//控制返回按键显示状态
        //控制图表的显示状态
        echartsshow: false,
        detailshows: true,

        //控制图表的style
        barheight: '88.37%',
        headheight: '11.61%',
        styledate: {
          echartsheight: '95%',
          echartsfloat: 'none',
        },
        c_year_list: options,
        f:this.$login.f,
        chosenOrg: {
          id: 0
        },
        count:{
          zongliang:0,
          yijian:0,
          checkrate:'0.00%',
        },
        model:new PagedList('rs/sql/CheckComplete',20, {year:'this.year',orgid:'this.orgid',plan_type:'this.plan_type',f_safecheck_type: 'this.f_safecheck_type'}),
        orgid: `('${this.$login.f.orgid}')`,
        startDate:(new Date()).getFullYear()+'-01-01',
        endDate: Util.toStandardDateString(),
        year:(new Date()).getFullYear(),
        xc: '',
        plan_types:[{label:'全部',value:''},{label:'年度安检单',value:'false'},{label:'首次通气单',value:'true'}],
        plan_type:'',
        f_safecheck_type:''
      }
    },
    props:{
      f_filialeids:{
        type:String
      }
    },
    methods: {
      barrdios() {//全屏按键事件
        console.log("全屏显示事件")
        this.barshow = true
        this.detailshows = false
        this.echartsshow = true
        this.show_all = true
        this.full_screen = false
        this.buttonshow = true
        this.showdetail = false
        this.styledate.echartsfloat = 'left'
        this.styledate.echartsheight = '65%'
        this.echartswidth = '50%'
        this.headheight = '8%'
        this.$parent.echarts = '3'
        this.$emit('echarts-changed')
        this.newecharts = true
        this.searchdata()
        console.log(this.$parent.echarts, '主图状态')
      },
      showall() {//返回按键事件
        //修改显示状态
        this.barshow = false
        this.detailshows = true
        this.echartsshow = false
        this.buttonshow = false
        this.newecharts = false
        this.show_all = false
        this.full_screen = true
        this.showdetail = false
        this.styledate.echartsheight = '95%'
        this.echartswidth = '95%'
        this.$parent.echarts = -1
        this.headheight = '11.61%'
        this.$emit('echarts-changed', this.echarts)
      },
      settable() {
        this.model= new PagedList('rs/sql/CheckComplete',20, {year:'this.year',orgid:'this.orgid',plan_type:'this.plan_type',f_safecheck_type: 'this.f_safecheck_type'})
        this.model.year = this.year
        this.model.orgid = this.orgid
        this.model.plan_type = this.plan_type
        this.model.f_safecheck_type = this.f_safecheck_type
        this.model.search('1=1')
      },
      set(scaledata,legendname,zongliangArray,yijianArray) {
        let serieData = [{     //绘图数据
          name: '计划量',
          type: "bar",
          barWidth: '15%',
          barGap: "0",
          itemStyle: {
            normal: {
              barBorderRadius: 15,
              borderColor: '#acf3ed',
              color: '#fff',
              label: {
                show: true,
                textStyle: {
                  color: "#4a7cae"
                },
                position: "top",
                formatter: function (p) {
                  return p.value > 0 ? (p.value) : '';
                }
              }
            }
          },
          data: zongliangArray,
        },
          {     //绘图数据
            name: '入户量',
            type: "bar",
            barWidth: '15%',
            barGap: "-100%",
            itemStyle: {
              normal: {
                barBorderRadius: 11,
                color: '',
                label: {
                  show: true,
                  textStyle: {
                    color: "#fccb05"
                  },
                  position: "insideTop",
                  formatter: function (p) {
                    return p.value > 0 ? (p.value) : '';
                  }
                }
              }
            },
            data: yijianArray,
          }]

        this.xc = echarts.init(document.getElementById('safedetail'))
        let text = ''
        if(this.plan_type == ''){
          text = '居民安检计划及完成率'
        }
        if(this.plan_type == 'false'){
          text = '居民年度安检计划及完成率'
        }
        if(this.plan_type == 'true'){
          text = '居民首次通气安检计划及完成率'
        }
        this.xc.setOption({
            title : {
              text: text,
              x: 'left'
            },
            color: ['#3398DB'],
            tooltip: {
              trigger: 'axis',
              axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
              },

              extraCssText: 'width: auto;height: auto;'
            },
            dataZoom: [{
              type: 'inside',
              show: true,
              xAxisIndex: [0],
              left: '9%',
              bottom: -5,
              start: 2,
              end: 90 //初始化滚动条
            }],
            grid: {
              left: '10%',
              right: '10%',
              bottom: '8%',
              containLabel: true
            },
            legend: {
              data: ['计划量', '入户量'],
              right: 10,
              top: 12,
              textStyle: {
                color: "black"
              },
              itemWidth: 12,
              itemHeight: 10,
              // itemGap: 35
            },
            xAxis: [
              {
                name: "分公司",
                type: 'category',
                axisTick: {
                  alignWithLabel: true
                },
                nameTextStyle: {color: "#6f8ba8"},
                axisLabel: {
                  show: true,
                  textStyle: {color: "#6f8ba8"},
                  formatter:function (value) {
                    var l=0
                    for(l;l<legendname.length;l++){
                      if(legendname[l]==value){
                        break
                      }
                    }
                    var h=eval(scaledata[l]+'*100').toString()
                    var index=h.indexOf('.')

                    var s=h.substring(0,index+2)
                    console.log("h:"+h)
                    console.log("index:"+index)
                    console.log("s:"+s)
                    return value +' \n '+(Math.round(s))+'%'
                  }
                },
                data: legendname
              }
            ],
            yAxis: [{
              type: "value",
              name: "数量",
              nameTextStyle: {color: "#6f8ba8"},
              splitLine: {show: true,lineStyle:{type:'dashed',color:'#e1e1e1'}},
              axisTick: {show: true},
              axisLine: {show: true,lineStyle :{color:'#999999'}},
              axisLabel: {show: true, textStyle: {color: "#6f8ba8"}}
            }],
            series: serieData
          }

          , true)
      },
      searchdata () {
        let getGen =getData(this)
        co(getGen)
      }
    },
    ready () {
      // this.orgid = `('${this.$login.f.orgid}')`
      this.year =(new Date()).getFullYear()
      // this.searchdata()
    },
    watch: {
      'year' (val) {
        console.log("我是watch")
        this.searchdata()
      },
      'plan_type' (val) {
        this.f_safecheck_type = ''
        if (this.plan_type == 'false') {
          this.f_safecheck_type = '年度普检'
        }
        if (this.plan_type == 'true') {
          this.f_safecheck_type = '首次通气'
        }
        console.log("我是plan_type")
        this.searchdata()
      },
      'f_filialeids'(){
         console.log('status改变了')
        this.orgid = this.f_filialeids
        this.searchdata()
      }
    }

  }
</script>
<style scoped>
  .div-1 {
    height: 85%;
    width: 10%;
    margin-left: 5%;
    float: left;
    margin-top: 1%;
  }

  .div-2 {
    float: right;
    width: 80%;
    height: 55.56%;
    margin-right: 1%;
    margin-top: 0.5%;
  }

  .div-3 {
    float: left;
    margin-left: 2%;
    padding-top: 2%;
    width: 96%;
    height: 99%;
  }

  .div-4 {
    background-color: #fafafa;
  }

  table {
    height: 100%;
    width: 100%;
    font-size: 12px
  }

  tr {
    height: 25%;
    width: 100%
  }

  td {
    width: 11%;
    font-size: 12px;
    color: #666666;
  }

  .td-1 {
    width: 16%;
    color: #90addf;
    font-size: 12px
  }

  .td-2 {
    width: 15%;
    color: #90addf;
    font-size: 12px
  }

  span {
    color: #90addf;
    font-size: 12px
  }

  .span-1 {
    color: #999999;
    font-size: 12px
  }

  .p-1 {
    font-size: 15px;
    height: 99%;
    width: 99%;
    font-weight: bold;
    color: #333333;
  }

  button {
    width: 10%;
    margin-top: 0%;
    margin-left: 2%;
    float: right;
    background-color: #73ca73;
    border-radius: 4px;
    border: 1px solid #73ca73;
    color: #ffffff;
    font-size: 15px;
    height: 120%;
  }

  .but-1 {
    width: 17%;
    float: right;
    border-radius: 4px;
    font-size: 12px;
    height: 130%;
  }

  .but-2 {
    background-color: #88b5e2;
    border: #88b5e2;
    width: 6%;
  }

  img {
    width: 5px;
    height: 18px;
  }

  .img-1 {
    height: 12px;
    width: auto;
  }

</style>

