<template>
  <div>
    <criteria-paged :model="model" v-ref:paged  @sort="sort">
      <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'"
                    :select-month="true"
                    :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'"
                    :select-month="true"
                    :show-rest-button="reset">
            </datepicker>
          </div>
          <div class="col-sm-2">
            <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="cp.f_entry_status = '{}'">
            </v-select>
          </div>
          <div class="col-sm-4">
            <role-selector
                    :left-width="'55%'"
                    :right-width="'55%'"
                    role-name="安检员"
                    role-lable="安&nbsp;检&nbsp;员"
                    @re-res="$parent.$parent.getRes"
                    :value.sync="model.f_checker_id"
                    v-model="model.f_checker_id"
                    condition="c.f_checker = '{}'">
            </role-selector>
          </div>
          <div class="col-sm-2">
            <div class="span" style="float: right;margin-top: 8px">
              <button type="button" class="button_search button_spacing" @click="search()">查询</button>
              <export-excel   :data="$parent.$parent.searchData"
                              :field="$parent.$parent.excelHeaders"
                              sqlurl="api/af-safecheck/logic/exportfile" sql-name="planCount" :template-name='$parent.$parent.excelTitle'></export-excel>
              <div class="button_spacing" style="float: right"
                   :class="{'button_shrink_left':$parent.$parent.$parent.showItem,'button_shrink_right':!$parent.$parent.$parent.showItem}"
                   @click="$parent.$parent.hiddenr()"></div>
            </div>

          </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><data-order field="checked" name="已完成"></data-order></th>
            <th>未完成</th>
            <th>入户率</th>
            <th><data-order field="rh" name="入户"></data-order></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.c}}</td>
          <td style="text-align: center">{{row.checked}}</td>
          <td style="text-align: center">{{row.unchecked}}</td>
          <td  class="text-center">
            <safe-fin-info :numfin="$parent.$parent.$parent.getnum(row.rh,row.c)"></safe-fin-info>
          </td>
          <td style="text-align: center">{{row.rh}}</td>
          <td style="text-align: center">{{row.jj}}</td>
          <td style="text-align: center">{{row.df}}</td>
        </template>
        <template partial="foot">
          <td style="text-align: center"><span><b>合计：</b></span></td>
          <td style="text-align: center">{{model.sums.c}}</td>
          <td style="text-align: center">{{model.sums.checked}}</td>
          <td style="text-align: center">{{model.sums.unchecked}}</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'


    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('api/af-safecheck/sql/planCount', 20, {
              f_plan_year: 'this.f_plan_year[0]',
              f_checker: 'this.chosenOrg.name',
              f_filialeids: 'this.f_filialeids ',
              orderitem: "'checked desc'"
            }, {c: '', checked: '', unchecked: ''})
            model.f_plan_year = year + ''
          model.f_filialeids = '('+this.$login.f.orgid+')'
            model.chosenOrg = {id: 0, name: ''}
            return {
              excelTitle:'安检员考核导出'+Util.toStandardTimeString().substring(11,19).replace(new RegExp( ':' , "g" ),""),
              excelHeaders:{
                  'f_checker': '安检员',
                  'c': '计划数',
                  'checked': '完成',
                  'unchecked': '未完成',
                  'rh': '入户',
                  'jj': '拒检',
                  'df': '到访不遇'
                },
              searchData:{
                  condition:"1=1",
                  f_plan_year:year[0],
                  f_checker:'',
                  f_filialeids:'('+this.$login.f.orgid+')',
                  orderitem: 'checked desc'
                }
              ,
                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: '',
              orderDefault: "'checked'",
              orderFields: {
                checked: 'no'
              },
            }
        },
        methods: {
          getnum(fin,sum){
            return sum?((fin/sum) * 100).toFixed(0):0
          },
            hiddenr() {
                this.$parent.showItem = !this.$parent.showItem
            },
          search(args) {
            if (this.f_check_start) {
              args.condition += ` AND f_plan_year >= ${this.f_check_start.substr(0,4)} `
              args.condition += ` AND f_plan_month >= ${this.f_check_start.substr(5,2)} `
            }
            if (this.f_check_end) {
              args.condition += ` AND f_plan_year <= ${this.f_check_end.substr(0,4)} `
              args.condition += ` AND f_plan_month <= ${this.f_check_end.substr(5,2)} `
            }
            this.$dispatch('checkstatus-changed', this.$refs.paged.$refs.cri.model.f_last_check_state[0])
            this.model.search(args.condition, args.model)
            console.log('this.model')
            console.log(this.model.rows.length)
          },
          getRes(obj){
            this.model.f_filialeids = this.$login.convertToIn(obj.resids);
            this.searchData.f_filialeids = this.$login.convertToIn(obj.resids);
            console.log(this.model.f_filialeids)
          },
          sort(field, rule) {
            // 将所有排序方式设为不排序，实现相互排斥
            for (let key in this.orderFields) {
              if (key === field) {
                this.orderFields[key] = rule
              } else {
                this.orderFields[key] = 'no'
              }
            }
            // 如果新规则不排序，还原为默认排序
            if (rule === 'no') {
              this.model.paramSource.orderitem = `'${this.orderDefault}'`
            } else {
              this.model.paramSource.orderitem = `'${field} ${rule}'`
            }
            this.$refs.paged.$refs.cri.search()
          }
        },
        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.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>
