<template>
  <div>
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' class="search_area" @condition-changed='$parent.search' v-ref:cri>
        <!--<form class="form-inline" partial>-->
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>

          <div class="col-sm-2 ">
            <label class="font_normal_body">起始时间</label>
            <datepicker
              :value.sync="$parent.$parent.f_check_start"
              placeholder='起始时间' style="width:60%"
              :disabled-days-of-week="[]"
              :format="'yyyy-MM-dd 00:00:00'"
              :show-rest-button="reset">
            </datepicker>
          </div>
          <div class="col-sm-2 ">
            <label class="font_normal_body">结束时间</label>
            <datepicker
              :value.sync="$parent.$parent.f_check_end"
              placeholder='结束时间' style="width:60%"
              :disabled-days-of-week="[]"
              :format="'yyyy-MM-dd 23:59:59'"
              :show-rest-button="reset">
            </datepicker>
          </div>
          <div class="col-sm-4 " style="width: 20%" >
            <label class="font_normal_body">安&nbsp;&nbsp;检&nbsp;员</label>
            <v-select :value="$parent.$parent.f_checker_id"
                      :value-single="true"
                      class="select select_list" style="width: 60%"
                      :options='$parent.$parent.checkersid' placeholder='安检员'
                      close-on-select
                      v-on:change="$parent.$parent.setcheckid"
                      ></v-select>
          </div>
          <div class="col-sm-2" style="width: 20%">
            <label class="font_normal_body">安检员</label>
            <input type="text" class="form-control" style="width: 60%" v-model="model.f_check_name"
                      placeholder="请输入安检员名称">
          </div>
          <div class="col-sm-4" style="width: 20%">
            <label class="font_normal_body">所属部门</label>
            <v-select id="f_entry"
                      placeholder='所属部门'
                      class="select select_list" style="width: 60%"
                      v-model='model.f_dep'
                      :value.sync="model.f_dep"
                      :options='$parent.$parent.deps'
                      close-on-select clear-button value-single> </v-select>
          </div>
          <div class="col-sm-2 ">
                        <export-excel   :data="$parent.$parent.searchData"
                            :field="$parent.$parent.excelHeaders"
                            sqlurl="rs/logic/SafeExportExcel" sql-name="getCollectByCheckPlan" template-name='安检汇总统计'></export-excel>
            <button type="button" style="float: right; margin-top: 8px;" class="button_search button_spacing" @click="search()">查询</button>
          </div>

          <div>
            {{$parent.$parent.model.chosenOrg.fullname}}
          </div>
          <!--</form>-->
        </div>
      </criteria>
      <data-grid :model="model" partial='list' v-ref:grid class="list_area table_sy">
        <template partial='head'>
          <tr>
            <th>开始时间</th>
            <th>结束时间</th>
            <th>安检员</th>
            <th>工作内容</th>
            <th>到户总数</th>
            <th>入户总数</th>
            <th>总工作时长</th>
            <th>户内总时长</th>
            <th>平均户内时长</th>
            <th>单户最长用时</th>
            <th>单户最短用时</th>
            <th>安全协议签订数量</th>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align: center">{{row.starttime}}</td>
          <td style="text-align: center">{{row.endtime}}</td>
          <td style="text-align: center">{{row.f_checker_name}}</td>
          <td style="text-align: center">{{row.f_no_checkplan}}</td>
          <td style="text-align: center">{{row.allcount}}</td>
          <td style="text-align: center">{{row.incount}}</td>
          <td style="text-align: center">{{row.totalworkhour}}</td>
          <td style="text-align: center">{{row.totalentrytime}}</td>
          <td style="text-align: center">{{row.averageentrytime}}</td>
          <td style="text-align: center">{{row.householdmax}}</td>
          <td style="text-align: center">{{row.householdmin}}</td>
          <td style="text-align: center">{{row.iscount}}</td>
        </template>
<!--        <template partial="foot">-->
<!--          <td style="text-align: center"></td>-->
<!--          <td style="text-align: center"></td>-->
<!--          <td style="text-align: center"></td>-->
<!--          <td style="text-align: center"><span><b>合计：</b></span></td>-->
<!--          <td style="text-align: center">{{model.sums.allcount}}</td>-->
<!--          <td style="text-align: center">{{model.sums.incount}}</td>-->
<!--          <td style="text-align: center">{{model.sums.totalworkhour}}</td>-->
<!--          <td style="text-align: center">{{model.sums.totalentrytime}}</td>-->
<!--          <td style="text-align: center">{{model.sums.averageentrytime}}</td>-->
<!--          <td style="text-align: center">{{model.sums.householdmax}}</td>-->
<!--          <td style="text-align: center">{{model.sums.householdmin}}</td>-->
<!--          <td style="text-align: center">{{model.sums.iscount}}</td>-->
<!--        </template>-->
      </data-grid>
    </criteria-paged>
  </div>
</template>
<script>
import {PagedList} from 'vue-client'
import co from 'co'
import Vue from 'vue'
import * as Util from '../Util'

let select = function * (self) {
  let getGasman = yield self.$resetpost('/rs/search', {
    source: 'tool.getChildrenOfResName($安检员$)',
    userid: self.$login.f.id
  }, {resolveMsg: null, rejectMsg: null})
  console.log('获取安检员', getGasman)
  getGasman.data.forEach((checker)=>{
    self.checkersid.push({label:checker.name,value:checker.id})
  })
  self.checkersid.push({label:'全部',value:''})
}
export default {
  title: '安检员日工作报表',
  data() {
    let options = []
    let year = (new Date()).getFullYear()
    for (let i = year - 8; i <= year + 1; i++)
      options.push({label: i + '', value: i + ''})
    let model = new PagedList('rs/sql/getCollectByCheckPlan', 20, {
    }, {allcount: '', incount: '', totalworkhour: '', totalentrytime: '', averageentrytime: '', householdmax: '', householdmin: '',iscount:''})
    return {
      excelHeaders:{
        'starttime': '开始时间',
        'endtime': '结束时间',
        'f_checker_name': '安检员',
        'f_no_checkplan': '计划类型',
        'allcount': '到户总数',
        'incount': '入户总数',
        'totalworkhour': '总工作时长',
        'totalentrytime': '户内总时长',
        'averageentrytime': '平均户内时长',
        'householdmax': '单户最长用时',
        'householdmin':'单户最短用时',
        'iscount':'安全协议签订数量'
      },
      checkersid:[{label:'全部',value:''}],
      f_checker_id:'',
      searchData:{

        condition:"1=1",
        f_filialeids:'('+this.$login.f.orgid+')'
      },
      deps:[
        {label:'全部',value:''},
        {label:'客户服务部',value:'客户服务部'},
        {label:'外包安检维修',value:'外包安检维修'}
      ],
      checker: this.$login.f,
      checkers: [],
      checkersname: [],
      checkStates: this.$appdata.getParam('安检状态'),
      f: this.$login.f,
      model: model,
      c_year_list: options,
      f_check_start:'',
      f_check_end: ''
    }
  },
  methods: {
    hiddenr() {
      this.$parent.showItem = !this.$parent.showItem
    },
    search(args) {
      args.condition1='1=1'
      args.condition2=''
      args.condition3=''
      console.log(args)
      if (this.f_check_start) {
        args.condition += ` AND a.f_offsite_time >= ${"'"+this.f_check_start+"'"} `
        args.condition1 += ` AND f_offsite_time >= ${"'"+this.f_check_start+"'"} `
        args.condition2 = this.f_check_start
      }
      if (this.f_check_end) {
        args.condition += ` and a.f_offsite_time <= ${"'"+this.f_check_end+"'"} `
        args.condition1 += ` and f_offsite_time <= ${"'"+this.f_check_end+"'"} `
        args.condition3 = this.f_check_end
      }
      if (this.f_checker_id) {
        args.condition += ` and a.f_checker_id = ${"'"+this.f_checker_id+"'"} `
        args.condition1 += ` and f_checker_id = ${"'"+this.f_checker_id+"'"} `
      }
      if (args.model.f_check_name) {
        args.condition += ` and a.f_checker_name like ${"'"+args.model.f_check_name+"%'"} `
        args.condition1 += ` and f_checker_name like ${"'"+args.model.f_check_name+"%'"} `
      }
      if (args.model.f_dep) {
        args.condition += ` and a.f_sub_dep = ${"'"+args.model.f_dep+"'"} `
        args.condition1 += ` and f_sub_dep = ${"'"+args.model.f_dep+"'"} `
      }

      this.model.search({condition:args.condition,condition1:args.condition1,condition2:args.condition2,condition3:args.condition3}, args.model)
    },
    getRes(obj){
      this.model.f_filialeids = this.$login.convertToIn(obj.resids);
      console.log(this.model.f_filialeids)
    },
    setcheckid(val){
      console.log("进入checkid")
      this.f_checker_id=val
    }
  },
  ready() {
    this.f_check_start=Util.toStartDateString()
    this.model.id = this.$login.f.id
    this.model.f_subcompany = this.$login.f.f_fengongsi
    this.model.f_filialeid = this.$login.f.orgid
    let gen = select(this)
    return co(gen)
  },

  watch: {
    'model.condition'(val) {
      if(val){
        this.searchData.condition = val
      }
    },
    'model.f_checker'(val) {
      if(val){
        this.searchData.f_checker = val
      }
    },
    'checker'() {
      // console.log('1')
      console.log("f_checker", this.checker)
      this.model.chosenOrg = this.checker[0]
    },
    // 'model.rows.length'() {
    //   // 如果查询无结果，将合计置为0
    //   if(this.model.rows.length < 1){
    //     this.model.sums.c = 0
    //     this.model.sums.checked = 0
    //     this.model.sums.unchecked = 0
    //   }
    // }
  },
  computed: {
    selected() {
      // console.log('2')
      // this.$parent.showItem=!this.$parent.showItem
      return this.$refs.paged.$refs.grid.selected
    }
  }
}
</script>
