<template>
  <div id='app'   v-show="checkshow && !$parent.childshow" >
    <div class="flex-row auto" style="justify-content: space-between">
      <div class="form-group">
        <!--<label for="f_subcompany">分公司：</label>-->
        <!--<v-select id="f_subcompany"-->
        <!--placeholder='请选择分公司'-->
        <!--:value.sync="$parent.$parent.model.f_subcompany"-->
        <!--:options='$parent.$parent.f_divisions'-->
        <!--close-on-select clear-button></v-select>-->
        <label >开始日期:</label>
        <datepicker id="startDate" placeholder="开始日期"
                    v-model="startDate"
                    readonly="readonly"
                    :value.sync="startDate"
                    :disabled-days-of-Week="[]"
                    :format="'yyyy-MM'"
                    :show-reset-button="reset">
        </datepicker>
        <label >结束日期:</label>
        <datepicker id="endDate" placeholder="结束日期"
                    readonly="readonly"
                    v-model="endDate"
                    :value.sync="endDate"
                    :disabled-days-of-Week="[]"
                    :format="'yyyy-MM'"
                    :show-reset-button="reset">
        </datepicker>
        <button class="btn btn-success width-100" @click="search()">查询</button>
        <button class="btn btn-success width-100" @click="returnBack()">返回</button>
        <!--<button class="btn btn-success" @click="$parent.$parent.confirmReport()">保存报表</button>-->
      </div>
      <span></span>
      <!--   <div class="form-group">
           <print-table :model='$parent.model'
                        :top='40' :left='60' :right='40' :bottom='60'
                        :thead='"<th>序号</th>\n"+
                       "<th>安检员</th>" +
                           "<th>计划安检数</th>"+
                             "<th>已检</th>"+
                             "<th>入户</th>"+
                               "<th>拒检</th>"+
                               "<th>到访不遇</th>"+
                               "<th>未使用天然气</th>"+
                               "<th>隐患总数</th>"+
                               "<th>A类隐患</th>"+
                               "<th>B类隐患</th>"+
                               "<th>C类隐患</th>"+
                               "<th>隐患超期</th>"+
                               "<th>隐患到访不遇</th>"+
                             "<th>安检率</th>"+
                                "<th>入户率</th>"'
                        :fields='["$index+1","f_subcompany","cplan","call","centry","cdeny","cabsent","nouse","dangernum","A","B","C","overduenum","nohandledanger","checkrate","entery_rate"]'>
           </print-table>-->
      <div class="form-group">
        <export-excel :data="{condition: '1=1',startDate: startDate, endDate: endDate,f_subcompany: clickname.company}"
                      :bean= exportbean
                      sqlurl="rs/logic/exportfile"></export-excel>
      </div>
    </div>
    <criteria-paged :model="model" :pager='false' v-ref:paged1>
      <!--<criteria partial='criteria' v-ref:criteria>-->
      <!--<div novalidate class="form-inline auto " partial >-->
      <!---->
      <!--</div>-->
      <!--</criteria>-->
      <data-grid :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th style="text-align: center"><nobr>年月</nobr></th>
            <!--<th style="text-align: center"><nobr>年月</nobr></th>-->
            <th style="text-align: center"><nobr>计划安检数</nobr></th>
            <!--<th style="text-align: center"><nobr>无计划安检数</nobr></th>-->
            <th style="text-align: center"><nobr>已检</nobr></th>
            <th style="text-align: center"><nobr>入户</nobr></th>
            <th style="text-align: center"><nobr>拒检</nobr></th>
            <th style="text-align: center"><nobr>到访不遇</nobr></th>
            <th style="text-align: center"><nobr>未使用天然气</nobr></th>
            <th style="text-align: center"><nobr>隐患</nobr></th>
            <th style="text-align: center"><nobr>安检率</nobr></th>
            <th style="text-align: center"><nobr>入户率</nobr></th>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align: center"><a @click="$parent.$parent.$parent.searchchecker(row.f_subcompany)"><nobr>{{row.check_date}}</nobr></a></td>
          <!--<td style="text-align: center"><nobr>{{row.check_date}}</nobr></td>-->
          <td style="text-align: center"><nobr>{{row.cplan}}</nobr></td>
          <!--<td style="text-align: center"><nobr>{{row.noplan}}</nobr></td>-->
          <td style="text-align: center"><nobr>{{row.call}}</nobr></td>
          <td style="text-align: center"><nobr>{{row.centry}}</nobr></td>
          <td style="text-align: center"><nobr>{{row.cdeny}}</nobr></td>
          <td style="text-align: center"><nobr>{{row.cabsent}}</nobr></td>
          <td style="text-align: center"><nobr>{{row.nouse}}</nobr></td>
          <td style="text-align: center"><nobr><a @click = "$parent.$parent.$parent.showDefect(row)">{{row.dangernum}}</a></nobr></td>
          <td style="text-align: center"><nobr>{{row.checkrate}}</nobr></td>
          <td style="text-align: center"><nobr>{{row.entery_rate}}</nobr></td>
        </template>
        <template partial='foot'>
          <tr>
            <td colspan="1" style="text-align: right">合计：</td>
            <td style="text-align: center"><nobr>{{$parent.$parent.$parent.rate.cplan}}</nobr></td>
            <!--<td style="text-align: center"><nobr>{{$parent.$parent.$parent.rate.noplan}}</nobr></td>-->
            <td style="text-align: center"><nobr>{{$parent.$parent.$parent.rate.call}}</nobr></td>
            <td style="text-align: center"><nobr>{{$parent.$parent.$parent.rate.centry}}</nobr></td>
            <td style="text-align: center"><nobr>{{$parent.$parent.$parent.rate.cdeny}}</nobr></td>
            <td style="text-align: center"><nobr>{{$parent.$parent.$parent.rate.cabsent}}</nobr></td>
            <td style="text-align: center"><nobr>{{$parent.$parent.$parent.rate.nouse}}</nobr></td>
            <td style="text-align: center"><nobr><a @click = "$parent.$parent.$parent.showDefect($parent.$parent.$parent.rate)">{{$parent.$parent.$parent.rate.dangernum}}</a></nobr></td>
            <td style="text-align: center"><nobr>{{$parent.$parent.$parent.rate.checkrate}}</nobr></td>
            <td style="text-align: center"><nobr>{{$parent.$parent.$parent.rate.entery_rate}}</nobr></td>
          </tr>
        </template>
      </data-grid>
    </criteria-paged>

    <modal :show.sync="show" v-ref:modal small backdrop="false">
      <header slot="modal-header" class="modal-header">
        <h4 class="modal-title">输入文件名称</h4>
      </header>
      <article slot="modal-body" class="modal-body">
        <div class="form-group">
          <input  type="text" class="form-control" v-model="filename" placeholder='保存的文件名'>
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">
        <button v-show="show" type="button" class="btn btn-default" @click='close'>取消</button>
        <button v-show="show" type="button" class="btn btn-success" @click='confirm(filename)'>确认</button>
      </footer>
    </modal>

    <modal :show.sync="showdefect2" v-ref:modal small backdrop="true">
      <article slot="modal-body" class="modal-body">
        <table class="table table-bordered">
          <tr><td>隐患等级A</td><td>隐患等级B</td><td>隐患等级C</td></tr>
          <tr>
            <td style="text-align: center"><nobr>{{defectA}}</nobr></td>
            <td style="text-align: center"><nobr>{{defectB}}</nobr></td>
            <td style="text-align: center"><nobr>{{defectC}}</nobr></td>
          </tr>
        </table>
      </article>
      <div slot="modal-footer" class="modal-footer" v-show="false">
      </div>
    </modal>

  </div>
  <checker-rate  v-show="!checkshow && !$parent.childshow" :clickname="clickname"></checker-rate>
</template>

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


  let saveFile = function * (self) {
// 线验证文件是否重名
    let count = yield self.$resetpost('rs/sql/singleTable',
      {data: {tablename: 't_report_record', condition: `f_files_name = '${self.filename}'`}},
      {resolveMsg: null, rejectMsg: null})
    if (count.data.length > 0) {
      self.$showAlert('无法保存，文件名重名', 'warning', 3000)
      return
    }
    let saveBack = yield self.$resetpost('rs/logic/saveReport', {f_files_content: self.$refs.paged.$els.handcollect.innerHTML,
      f_files_name: self.filename, f_files_type: self.data.f_report_type, f_component_name: 'contrast',
      f_operator: Util.f.name, f_query_month: `${self.model.model.startDate}-${self.model.model.endDate}`,
      f_outlets: Util.f.f_parentname})

    if (saveBack.data.status === 'succeed') {
      self.show = false
      self.filename = ''
      // 后台保存完成，通知刷新
      self.$dispatch('save-success', self.filename)
    }
  }


  export default {
    title: '安检率报表',
    props: ['data','clickname'],
    data () {
      return {
        exportbean: "安检率月度报表",
        model:new PagedList(),
        reportStr: null,
        f:Util.f,
        defectA:'',
        defectB:'',
        defectC:'',
        showdefect2:false,
        show: false,
        f_subcompany:'',
        startDate:"",
        endDate:"",
        rate:{
          cplan:0,
          noplan:0,
          call:0,
          centry:0,
          cdeny:0,
          cabsent:0,
          nouse:0,
          dangernum:0,
          a:0,
          b:0,
          c:0,
          createoverdue:0,
          nohandledanger:0,
          overduenum:0,
          nocabsent:0,
          checkrate:'',
          entery_rate:'',
        },
        startDate:'',
        endDate:'',
        filename: '',
        companyname:'',
        checkshow:true,
        f_divisions: Vue.f_subcompany != '' ? [{label: Vue.f_subcompany, value: Vue.f_subcompany}]:this.$getParams('安检系统用子公司', [{label:'请选择分公司',value:''}]),

      }
    },
    ready () {
      this.startDate = this.clickname.date.startDate
      this.endDate = this.clickname.date.endDate
      this.search()
    },

    methods: {
      searchchecker(filename){
        this.checkshow=false
        this.clickname={company:filename,date:this.clickname.date}
      },
      returnBack(){
        this.$parent.childshow=true
      },
      showDefect(row){
        this.showdefect2 = true
        this.defectA = row.a
        this.defectB = row.b
        this.defectC = row.c
      },

      search(){
        this.model=new PagedList('rs/sql/QueryMonthCheckRate',999, {startDate: '\''+this.startDate+'\'', endDate: '\''+this.endDate+'\'',f_subcompany: '\''+this.clickname.company+'\''})
        this.model.search("1=1")
      },
      // 根据文件路径获取储存的报表内容
      getFileContent(path) {
        this.$resetpost('rs/logic/getReportFileContent', {f_files_path: path}, {resolveMsg: null, rejectMsg: null}).then((res) => {
          this.reportStr = res.data.filecontent
        this.model.state = '正确'
      })
      },
      confirm () {
        if (!this.filename || this.filename === '') {
          this.$showAlert('无法保存，文件名不能为空', 'warning', 3000)
        }
        let saveGen = saveFile(this)
        co(saveGen)
      },
      close () {
        this.show = false
      },
      // 将报表保存成文件
      confirmReport () {
        this.show = true
        this.filename = this.data.f_report_name + Util.toStandardDateString()
      }
    },
    watch: {
      'clickname':{
        deep:true,
        handler(val){
          console.log('clickname变化')
          this.startDate=this.clickname.date.startDate
          this.endDate=this.clickname.date.endDate
          this.search()
        }
      },
      'data' (val) {
        if (val.f_files_path) {
          this.getFileContent(val.f_files_path)
        }else {
          this.reportStr = null
        }
      },
      'model.rows'(){
        this.rate.cplan = 0
        this.rate.noplan = 0
        this.rate.call = 0
        this.rate.centry = 0
        this.rate.cdeny = 0
        this.rate.cabsent = 0
        this.rate.nouse = 0
        this.rate.dangernum = 0
        this.rate.a = 0
        this.rate.b = 0
        this.rate.c = 0
        this.rate.createoverdue  = 0
        this.rate.nohandledanger = 0
        this.rate.overduenum = 0
        this.rate.nocabsent = 0
        for (let i = 0; i < this.model.count; i++) {
          if (this.model.rows[i]) {
            this.rate.cplan += this.model.rows[i].cplan
            this.rate.noplan += this.model.rows[i].noplan
            this.rate.call += this.model.rows[i].call
            this.rate.centry += this.model.rows[i].centry
            this.rate.cdeny += this.model.rows[i].cdeny
            this.rate.cabsent += this.model.rows[i].cabsent
            this.rate.nouse += this.model.rows[i].nouse
            this.rate.dangernum += this.model.rows[i].dangernum
            this.rate.a += this.model.rows[i].a
            this.rate.b += this.model.rows[i].b
            this.rate.c += this.model.rows[i].c
            this.rate.createoverdue  += this.model.rows[i].createoverdue
            this.rate.nocabsent += this.model.rows[i].nocabsent
            this.rate.overduenum += this.model.rows[i].overduenum
            this.rate.nohandledanger += this.model.rows[i].nohandledanger
          }
        }
        if (this.rate.cplan == 0) {
          this.rate.checkrate = '0.00%'
        } else {
          this.rate.checkrate = ((this.rate.call / this.rate.cplan) * 100).toFixed(2) + '%'
        }
        if(this.rate.call == 0){
          this.rate.entery_rate = '0.00%'
        }else {
          this.rate.entery_rate = ((this.rate.centry / this.rate.call) * 100).toFixed(2) + '%'
        }
      }

    }
  }
</script>
