<template>
  <div id='app' v-show="childshow" >
    <criteria-paged :model="model" :pager='false' v-ref:paged>
      <criteria partial='criteria' @condition-changed='search' v-ref:criteria>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
            <div class="col-sm-4 form-group form-input-group" style="width: 20%">
              <label class="control-label">开始日期</label>
              <datepicker id="startDate" placeholder="开始日期"
                          v-model="model.startDate" style="margin-top: 8px"
                          readonly="readonly"
                          :value.sync="model.startDate"
                          :disabled-days-of-Week="[]"
                          :format="'yyyy-MM'"
                          :show-reset-button="reset">
              </datepicker>
            </div>
            <div class="col-sm-4 form-group form-input-group" style="width: 20%">
              <label class="control-label">结束日期</label>
              <datepicker id="endDate" placeholder="结束日期"
                          readonly="readonly" style="margin-top: 8px"
                          v-model="model.endDate"
                          :value.sync="model.endDate"
                          :disabled-days-of-Week="[]"
                          :format="'yyyy-MM'"
                          :show-reset-button="reset">
              </datepicker>
            </div>
            <div class="col-sm-4 form-group form-input-group" style="width: 20%;margin-top:8px;">
              <button class="button_spacing button_search" @click="search()">查询</button>
            </div>
              <!--<button class="btn btn-success" @click="$parent.$parent.confirmReport()">保存报表</button>-->

            <!--<span></span>-->
          <div class="col-sm-4 form-group form-input-group" style="float:right;width: 20%;margin-top:8px;">
              <print-table :model='$parent.model' class="button_export"
                           :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>消除超期</th>"+
                                  "<th>消除到访不遇</th>"+
                                "<th>安检率</th>"+
                                   "<th>入户率</th>"'
                           :fields='["$index+1","f_subcompany",,"cplan","call","centry","cdeny","cabsent","nouse","dangernum","overduenum","nohandledanger","checkrate","entery_rate"]'>
              </print-table>
            <div style="width: 20px;height: 10px"> </div>
              <export-excel class="button_export" :data="{condition: '1=1',startDate: model.startDate, endDate: model.endDate,f_subcompany: $parent.$parent.model.f_subcompany[0]}"
                            :bean=$parent.$parent.exportbean
                            sqlurl="rs/logic/exportfile"></export-excel>
            </div>
          </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>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align: center"><nobr><a @click="$parent.$parent.$parent.searchchecker(row.f_subcompany,$parent.$parent.$parent.model.model)">{{row.f_subcompany}}</a></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 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="showdefect1" v-ref:modal small backdrop="true">
      <article slot="modal-body" class="modal-body">
        <table class="table table-bordered">
          <tr>
            <td><nobr>隐患等级A</nobr></td>
            <td><nobr>隐患等级B</nobr></td>
            <td><nobr>隐患等级C</nobr></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>
  <month-rate  v-if="!childshow" :clickname="clickname"></month-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',],
    data () {
      let model = new PagedList('rs/sql/QueryCheckRate',999, {startDate: 'this.model.startDate', endDate: 'this.model.endDate',f_subcompany: 'this.f_subcompany[0]'})
       model.f_subcompany = [Vue.f_subcompany]
      return {
        exportbean: "安检率报表",
        model:model,
        reportStr: null,
        f:Util.f,
        defectA:'',
        defectB:'',
        defectC:'',
        showdefect1:false,
        show: false,
        f_subcompany:'',
        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,
          createdanger:0,
          overduenum:0,
          nohandledanger:0,
          checkrate:'',
          entery_rate:'',
        },
        filename: '',
        f_divisions: Vue.f_subcompany != '' ? [{label: Vue.f_subcompany, value: Vue.f_subcompany}]:this.$getParams('安检系统用子公司', [{label:'请选择分公司',value:''}]),
        childshow:true,
        clickname:''
      }
    },
    ready () {
      var d = new Date()
      let month = d.getMonth()
      this.$refs.paged.$refs.criteria.model.startDate = d.getFullYear() + '-' + (month<10? '0'+ month : month)
      this.$refs.paged.$refs.criteria.model.endDate = Util.toStandardYearMonth()

    },

    methods: {
      searchchecker(filename,date){
        console.log(date)
        this.childshow=false
        this.clickname={company:filename,date:date}
      },

      search(){
        this.model=new PagedList('rs/sql/QueryCheckerRate',999, {startDate: '\''+this.startDate+'\'', endDate: '\''+this.endDate+'\'',f_subcompany: '\''+this.clickname.company+'\''})
        this.model.search("1=1")
      },

      showDefect(row){
        this.showdefect1 = true
        this.defectA = row.a
        this.defectB = row.b
        this.defectC = row.c
      },

      // 根据文件路径获取储存的报表内容
      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: {
      '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.createdanger = 0
        this.rate.overduenum = 0
        this.rate.nohandledanger = 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>
