<template>
  <div>
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' class="search_area" @condition-changed='$parent.search' v-ref:cri>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row">
            <div v-if="$parent.$parent.isShow">
              <label class="font_normal_body">&nbsp;安检员&nbsp;&nbsp;&nbsp;</label>
              <v-select
                style="width:60%"
                :value.sync="model.f_checker_id"
                :value-single="true"
                :options='$parent.$parent.checkerman'
                placeholder='请选择安检员'
                close-on-select></v-select>
            </div>

            <div >
              <label class="font_normal_body">安检起始</label>
              <datepicker
                :value.sync="$parent.$parent.f_start_time"
                placeholder='安检起始' style="width:60%"
                :show-rest-button="reset">
              </datepicker>
            </div>
            <div :class="{'form-group col-sm-3':$parent.$parent.$parent.showItem,'form-group col-sm-2':!$parent.$parent.$parent.showItem}">
              <label class="font_normal_body">安检截止</label>
              <datepicker
                :value.sync="$parent.$parent.f_end_time"
                placeholder='安检截止' style="width:60%"
                :show-rest-button="reset">
              </datepicker>
            </div>
            <div style="width: auto;margin-top:8px;float: right" :class="{'form-group col-sm-3':$parent.$parent.$parent.showItem,'form-group col-sm-2':!$parent.$parent.$parent.showItem}">
              <button type="button" class="button_search button_spacing" @click="search()">查询</button>
              <div class="button_spacing"
                   :class="{'button_shrink_left':$parent.$parent.$parent.showItem,'button_shrink_right':!$parent.$parent.$parent.showItem}"
                   @click="$parent.$parent.hiddenr()"></div>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' v-ref:grid class="list_area table_sy">
        <template partial='head'>
          <tr>
            <th>安检员</th>
            <th style="text-align: center;white-space:nowrap">
              计划<br>
              总数
            </th>
            <th>入户</th>
            <th style="text-align: center;white-space:nowrap">
              到访<br>
              不遇
            </th>
            <th>拒检</th>
            <th>已检</th>
            <th>未检</th>
            <th>安检率</th>
            <th>入户率</th>
          </tr>
        </template>
        <template partial='body'>
          <td style="text-align: center">{{row.f_checker}}</td>
          <td style="text-align: center">{{row.plannum}}</td>
          <td style="text-align: center">{{row.ruhu}}</td>
          <td style="text-align: center">{{row.daofang}}</td>
          <td style="text-align: center">{{row.jujian}}</td>
          <td style="text-align: center">{{row.ruhu+row.daofang+row.jujian}}</td>
          <td style="text-align: center">{{row.plannum-(row.ruhu+row.daofang+row.jujian)}}</td>
          <td style="text-align: center">{{((row.ruhu+row.daofang+row.jujian)/row.plannum).toFixed(2)}}%</td>
          <td style="text-align: center">{{(row.ruhu/row.plannum).toFixed(2)}}%</td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>
<script>
import {PagedList} from 'vue-client'
import { HttpResetClass } from 'vue-client'
import Vue from 'vue'
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(`${this.$androidUtil.getProxyUrl()}/rs/sql/planCount`, 20, {
      f_filialeids: 'this.f_filialeids',
      f_start_time: 'this.f_start_time',
      condition1:'this.condition1',
      condition2:'this.condition2',
      condition3:'this.condition3',
      f_end_time: 'this.f_end_time'
    })
    model.f_filialeids = '('+Vue.user.orgid+')'
    return {
      searchData:{
        condition:"1=1",
        condition1:"1=1",
        condition2:"1=1",
        condition3:"and 1=1",
        f_plan_year:year[0],
        f_checker:'',
        f_filialeids:'('+Vue.user.orgid+')',
        f_start_time:'',
        f_end_time:''
      },
      checker: Vue.user,
      checkers: [],
      checkerman: [],
      checkersname: [],
      checkStates: this.$appdata.getParam('安检状态'),
      f: Vue.user,
      criteriaShow: false,
      model: model,
      c_year_list: options,
      f_check_start:'',
      f_check_end: '',
      f_start_time:'',
      f_end_time:'',
      isShow:false
    }
  },
  methods: {
    //判断当前用户是否有管理员角色
    getrole(){
      if(Vue.user.rolesnames.indexOf('管理员') > -1){
        this.isShow=true
      }
    },
    getEmp() {
      this.checkerman=[]
      let per = {source: 'this.getRights().where(row.getType()==$user$).where(row.getAttributes().get($rolestr$).indexOf($安检员$) != -1))', userid: `${Vue.user.id}`}
      let http = new HttpResetClass()
      http.load('POST', `${this.$androidUtil.getProxyUrl()}/rs/search`, {data: per}, {resolveMsg: null, rejectMsg: null}).then((res) => {
        res.data.forEach((user)=>{
          this.checkerman.push({label:user.name, value: user.id})
        })
      })
    },
    hidden() {
      this.criteriaShow = !this.criteriaShow
    },
    hiddenr() {
      this.$parent.showItem = !this.$parent.showItem
    },
    search(args) {
      let tempStr = args.condition
      let condition1 =tempStr
      let condition2 =tempStr
      let condition3 = ' and 1=1 '
      if (this.f_check_start) {
        condition1 += ` AND f_plan_year >= ${this.f_check_start.substr(0,4)} `
        condition1 += ` AND f_plan_month >= ${this.f_check_start.substr(5,2)} `
      }
      if (this.f_check_end) {
        condition1 += ` AND f_plan_year <= ${this.f_check_end.substr(0,4)} `
        condition1 += ` AND f_plan_month <= ${this.f_check_end.substr(5,2)} `
      }
      if(this.f_start_time){
        condition2 += ` AND f_offsite_time >= '${this.f_start_time} 00:00:00'`
      }
      if (this.f_end_time){
        condition2 += ` AND f_offsite_time <= '${this.f_end_time} 23:59:59'`
      }
      if (this.$refs.paged.$refs.cri.model.f_checker_id){
        condition3 += ` AND f_checker_id= '${this.$refs.paged.$refs.cri.model.f_checker_id}' `
        condition2 += ` AND f_checker_id= '${this.$refs.paged.$refs.cri.model.f_checker_id}' `
      }
      if(this.$refs.paged.$refs.cri.model.f_checker_id==''||this.$refs.paged.$refs.cri.model.f_checker_id==null){
        condition3 += ` AND f_checker_id = '${Vue.user.id}' `
        condition2 += ` AND f_checker_id = '${Vue.user.id}' `
      }
      this.model.f_start_time=this.f_start_time
      this.model.f_end_time=this.f_end_time
      this.model.condition1=condition1
      this.model.condition2=condition2
      this.model.condition3=condition3

      this.searchData.condition1=condition1
      this.searchData.condition2=condition2
      this.searchData.condition3=condition3
      this.model.search(args.condition, args.model)
    },
    getRes(obj){
      this.model.f_filialeids = this.$login.convertToIn(obj.resids);
    }
  },
  ready() {
    this.getEmp()
    this.getrole()
    this.model.id = Vue.user.id
    this.model.f_subcompany = Vue.user.f_fengongsi
    this.model.f_filialeid = Vue.user.orgid
  },
  watch: {
    'model.condition'(val) {
      if(val){
        this.searchData.condition = val
      }
    },
    'model.f_checker'(val) {
      if(val){
        this.searchData.f_checker = val
      }
    },
    'f_start_time'(val) {
      if(val){
        this.searchData.f_start_time = val
      }
    },
    'f_end_time'(val) {
      if(val){
        this.searchData.f_end_time = val
      }
    },
    'checker'() {
      // //tag
      //tag
      this.model.chosenOrg = this.checker[0]
    },
    'model.rows.length'() {
      // 如果查询无结果，将合计置为0
      if(this.model.rows.length < 1 &&this. model.sums){
        this.model.sums.yrj = 0
        this.model.sums.no_plan_num = 0
        this.model.sums.wrj = 0
        this.model.sums.ruhu = 0
        this.model.sums.jujian = 0
        this.model.sums.daofang = 0
        this.model.sums.ruhu_total=0
        this.model.sums.daofang_total=0
        this.model.sums.jujian_total=0
        this.model.sums.total_sum=0
      }
    }
  },
  computed: {
    selected() {
      return this.$refs.paged.$refs.grid.selected
    }
  }
}
</script>
