<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 :class="{'form-group col-sm-6':$parent.$parent.$parent.showItem,'form-group col-sm-4':!$parent.$parent.$parent.showItem}" style="margin: 0">
              <role-selector-safe
                role-name="安检员"
                role-lable="安&ensp;检&ensp;员"
                @re-res="$parent.$parent.getRes"
                :value.sync="model.f_checker_id"
                v-model="model.f_checker_id">
              </role-selector-safe>
            </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>
            <v-select
              :value.sync="model.f_checker_state"
              :options='$parent.$parent.checkertates'
              placeholder='安检员状态'
              close-on-select
              style="width:60% "
              class="select select_list"
              v-model='model.f_checker_state'
             >
            </v-select>
           </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_check_start"
                placeholder='起始月份' style="width:60%"
                :disabled-days-of-week="[]"
                :format="'yyyy-MM'"
                :select-month="true"
                :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_check_end"
                placeholder='结束月份' style="width:60%"
                :disabled-days-of-week="[]"
                :format="'yyyy-MM'"
                :select-month="true"
                :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>
              <v-select :value.sync="model.f_last_check_state" :options='$parent.$parent.checkStates' placeholder='上次安检状态' close-on-select
                        style="width:60% "   class="select select_list"  v-model='model.f_last_check_state'   condition="i.f_last_check_state = '{}'">
              </v-select>
            </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_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 :class="{'form-group col-sm-3':$parent.$parent.$parent.showItem,'form-group col-sm-2':!$parent.$parent.$parent.showItem}" v-if="$parent.$parent.criteriaShow">-->
              <!--<label class="font_normal_body">安检起始</label>-->
              <!--<datepicker-->
                <!--:value.sync="$parent.$parent.f_start_time"-->
                <!--placeholder='起始时间' style="width:60%"-->
                <!--:disabled-days-of-week="[]"-->
                <!--:format="'yyyy-MM-dd'"-->
                <!--: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}" v-if="$parent.$parent.criteriaShow">-->
              <!--<label class="font_normal_body">安检结束</label>-->
              <!--<datepicker-->
                <!--:value.sync="$parent.$parent.f_end_time"-->
                <!--placeholder='结束时间' style="width:60%"-->
                <!--:disabled-days-of-week="[]"-->
                <!--:format="'yyyy-MM-dd'"-->
                <!--: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}">
              <export-excel-safe :data="$parent.$parent.searchData"
                                 :field="$parent.$parent.excelHeaders"
                                 :choose-col="true"
                                 sqlurl="rs/logic/SafeExportExcel" sql-name="planCount" template-name='安检计划明细'></export-excel-safe>
              <button type="button" class="button_search button_spacing" @click="search()">查询</button>
              <!--<div class="button_spacing"-->
                   <!--:class="{'button_shrink_top':$parent.criteriaShow,'button_shrink_bottom':!$parent.criteriaShow}"-->
                   <!--@click="$parent.$parent.hidden()">-->
               <!--</div>-->
              <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>总入户量</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.f_checker}}</td>
          <td style="text-align: center">{{row.ruhu_total}}</td>
          <td style="text-align: center">{{row.daofang_total}}</td>
          <td style="text-align: center">{{row.jujian_total}}</td>
          <td style="text-align: center">{{row.total_sum}}</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.no_plan_num}}</td>
          <td style="text-align: center">{{row.noruhu}}</td>
          <td style="text-align: center">{{row.nodaofang}}</td>
          <td style="text-align: center">{{row.nojujian}}</td>
        </template>
        <template partial="foot">
          <td style="text-align: center"><span><b>合计：</b></span></td>
          <td style="text-align: center">{{model.sums.ruhu_total}}</td>
          <td style="text-align: center">{{model.sums.daofang_total}}</td>
          <td style="text-align: center">{{model.sums.jujian_total}}</td>
          <td style="text-align: center">{{model.sums.total_sum}}</td>
          <td style="text-align: center">{{model.sums.plannum}}</td>
          <td style="text-align: center">{{model.sums.ruhu}}</td>
          <td style="text-align: center">{{model.sums.daofang}}</td>
          <td style="text-align: center">{{model.sums.jujian}}</td>
          <td style="text-align: center">{{model.sums.no_plan_num}}</td>
          <td style="text-align: center">{{model.sums.noruhu}}</td>
          <td style="text-align: center">{{model.sums.nodaofang}}</td>
          <td style="text-align: center">{{model.sums.nojujian}}</td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>
<script>
  import {PagedList} from 'vue-client'


  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/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'
      }, {plannum: '', ruhu: '', jujian: '', daofang: '',no_plan_num:'',noruhu:'',nodaofang:'',nojujian:'',ruhu_total:'',daofang_total:'',jujian_total:'',total_sum:''})
      model.f_filialeids = '('+this.$login.f.orgid+')'
      return {
        checkertates: [{label: '在职', value: '在职'}, {label: '禁用', value: '禁用'}, {label: '离职', value: '离职'}],
        excelHeaders:{
          'f_checker': '安检员',
          'plannum': '计划总数',
          // 'yrj': '计划中已入户',
          // 'wrj': '计划中未入户',
          'ruhu': '入户',
          'jujian': '拒检',
          'daofang': '到访不遇',
          'no_plan_num': '无计划总数',
          'noruhu': '无计划入户',
          'nojujian': '无计划拒检',
          'nodaofang': '无计划到访不遇'
        },
        searchData:{
          condition:"1=1",
          f_plan_year:year[0],
          f_checker:'',
          f_filialeids:'('+this.$login.f.orgid+')',
          f_start_time:'',
          f_end_time:''
        },
        checker: this.$login.f,
        checkers: [],
        checkersname: [],
        checkStates: this.$appdata.getParam('安检状态'),
        f: this.$login.f,
        criteriaShow: false,
        model: model,
        c_year_list: options,
        f_check_start:'',
        f_check_end: '',
        f_start_time:'',
        f_end_time:''
      }
    },
    methods: {
      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 = '${this.$refs.paged.$refs.cri.model.f_checker_id}' `
          condition2 += ` AND f_checker_name = '${this.$refs.paged.$refs.cri.model.f_checker_id}' `
        }
        if (this.$refs.paged.$refs.cri.model.f_checker_state !=''){
          condition3 += ` AND tu.state= '${this.$refs.paged.$refs.cri.model.f_checker_state}' `
          condition2 += ` AND tu.state= '${this.$refs.paged.$refs.cri.model.f_checker_state}' `
        }else{
          condition3 += ` AND tu.state= '在职' `
          condition2 += ` AND tu.state= '在职' `
        }
        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);
        //tag
      }
    },
    ready() {
      this.model.id = this.$login.f.id
      this.model.f_subcompany = this.$login.f.f_fengongsi
      this.model.f_filialeid = this.$login.f.orgid
    },

    watch: {
      'model.condition'(val) {
        if(val){
          this.searchData.condition = val
        }
      },
      'model.condition1'(val) {
        if(val){
          this.searchData.condition1 = val
        }
      },
      'model.condition2'(val) {
        if(val){
          this.searchData.condition2 = val
        }
      },
      'model.condition3'(val) {
        if(val){
          this.searchData.condition3 = 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() {
        // //tag
        // this.$parent.showItem=!this.$parent.showItem
        return this.$refs.paged.$refs.grid.selected
      }
    }
  }
</script>
