<template>
  <div  class="flex">
    <criteria-paged :model="model" v-ref:paged :pager="true">
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row">
            <div  :class="{'col-sm-6':$parent.$parent.$parent.isdetail,'col-sm-4':!$parent.$parent.$parent.isdetail}">
              <role-selector-safe-dep
                role-name="安检员"
                role-lable="安&nbsp;检&nbsp;员&nbsp;"
                @re-res="$parent.$parent.getRes"
                @alluser="$parent.$parent.getchecker"
                :value.sync="model.f_checker_id"
                v-model="model.f_checker_id"
                :value-multiple="true"
                condition=" f_checker_name in {}">
              </role-selector-safe-dep>
            </div>
            <div :class="{'col-sm-3':$parent.$parent.$parent.isdetail,'col-sm-2':!$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">小区名称</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_residential_area" placeholder='小区名称'
                     condition="f_residential_area like '%{}%'" v-next-el="cx" v-el:qjmc>
            </div>
            <div :class="{'col-sm-3':$parent.$parent.$parent.isdetail,'col-sm-2':!$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">计划名称</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_plan_name" placeholder='计划名称'
                     condition="f_plan_name like '%{}%'" v-next-el="cx" v-el:qjmc>
            </div>
<!--            <div :class="{'col-sm-3':$parent.$parent.$parent.isdetail,'col-sm-2':!$parent.$parent.$parent.isdetail}">-->
<!--              <label class="font_normal_body">安&ensp;检&ensp;员</label>-->
<!--              <input type="text" class="input_search" style="width:60%" v-model="model.f_checker_name" placeholder='安检员'-->
<!--                     condition="f_checker_name like '%{}%'" v-next-el="cx" v-el:qjmc>-->
<!--            </div>-->
            <div :class="{'col-sm-3':$parent.$parent.$parent.isdetail,'col-sm-2':!$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">安检起始</label>
              <datepicker
                :value.sync="$parent.$parent.f_start_time"
                v-model="$parent.$parent.f_start_time"
                placeholder='安检起始' style="width:60%"
                :show-rest-button="reset">
              </datepicker>
            </div>
            <div :class="{'col-sm-3':$parent.$parent.$parent.isdetail,'col-sm-2':!$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">安检截止</label>
              <datepicker
                :value.sync="$parent.$parent.f_end_time"
                placeholder='安检截止' style="width:60%"
                v-model="$parent.$parent.f_end_time"
                :show-rest-button="reset">
              </datepicker>
            </div>
            <div :class="{'col-sm-3':$parent.$parent.$parent.isdetail,'col-sm-2':!$parent.$parent.$parent.isdetail}">
              <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-3':$parent.$parent.$parent.isdetail,'col-sm-2':!$parent.$parent.$parent.isdetail}">
              <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-3':$parent.$parent.$parent.isdetail,'col-sm-2':!$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">所属区域</label>
              <v-select :value.sync="model.f_street" v-model='model.f_street'
                        :value-single="true" style="width: 60%"
                        class="select_list select"
                        :options='$parent.$parent.streets' placeholder='所属区域'
                        close-on-select
                        condition="f_street = '{}'"></v-select>
            </div>
            <div :class="{'col-sm-3':$parent.$parent.$parent.isdetail,'col-sm-2':!$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">用户类型</label>
              <v-select :value.sync="model.f_user_type" v-model='model.f_user_type'
                        :value-single="true" style="width: 60%"
                        class="select_list select"
                        :options='$parent.$parent.usertypes' placeholder='用户类型'
                        condition="f_user_type = '{}'"
                        close-on-select></v-select>
            </div>
            <div :class="{'col-sm-3':$parent.$parent.$parent.isdetail,'col-sm-2':!$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">班&emsp;&emsp;组</label>
              <v-select
                class="select select_list"
                :value.sync="model.depmentid"
                v-model="model.depmentid"
                :options='$parent.$parent.depments'
                condition="f_checker_name like '{}%'"
                placeholder='请选择'
                :search="false"
                close-on-select
                value-single
              >
              </v-select>
            </div>
            <div style="float: right;margin-left:10px" class="button_spacing"
                 :class="{'button_shrink_left':!$parent.$parent.$parent.isdetail,'button_shrink_right':$parent.$parent.$parent.isdetail}"
                 @click="$parent.$parent.hiddenr()"></div>
            <div class="span" style="float:right;">
              <button class="button_search" @click="$parent.$parent.getGroupName1(),search() ,$parent.$parent.$parent.isdetail=false,$parent.$parent.listflag=false" v-el:cx>查询</button>
            </div>

          </div>
        </div>

      </criteria>
      <data-grid :model="model" partial='list'  class="list_area table_sy"  v-ref:grid>
        <template partial='head'>
          <tr>
            <th>
              <nobr>序号</nobr>
            </th>
            <th v-if="$parent.$parent.$parent.nameForSql.includes('f_plan_month')"><nobr>计划月份</nobr></th>
            <th v-if="$parent.$parent.$parent.nameForSql.includes('f_no_checkplan')"><nobr>计划类型</nobr></th>
            <th v-if="$parent.$parent.$parent.nameForSql.includes('f_residential_area')"><nobr>小区名称</nobr></th>
            <th v-if="$parent.$parent.$parent.nameForSql.includes('f_plan_name')"><nobr>计划名称</nobr></th>
            <th v-if="$parent.$parent.$parent.nameForSql.includes('f_checker_name')"><nobr>安检员</nobr></th>
            <th v-if="$parent.$parent.$parent.nameForSql.includes('f_user_number')"><nobr>档案数量</nobr></th>
            <th>
              <nobr>计划总数</nobr>
            </th>
            <th>
              <nobr>计划中未检</nobr>
            </th>
            <th>
              <nobr>计划中已检</nobr>
            </th>
            <th>
              <nobr>计划中已入户</nobr>
            </th>
            <th>
              <nobr>入户率</nobr>
            </th>
            <th>
              <nobr>安检率</nobr>
            </th>
            <th>
              <nobr>入户</nobr>
            </th>
            <th>
              <nobr>到访不遇</nobr>
            </th>
            <th>
              <nobr>复访不遇</nobr>
            </th>
            <th>
              <nobr>拒检</nobr>
            </th>
            <!--<th>-->
              <!--<nobr>查看</nobr>-->
            <!--</th>-->
          </tr>
        </template>
        <template partial='body'>
          <tr >
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center">{{$index + 1}}</td>
            <td style="text-align:center" :class="{}" v-if="$parent.$parent.$parent.nameForSql.includes('f_plan_month')">{{row.f_plan_month.indexOf('*&')!=-1?'':row.f_plan_month}}</td>
            <td style="text-align:center" :class="{}" v-if="$parent.$parent.$parent.nameForSql.includes('f_no_checkplan')">{{row.f_no_checkplan.indexOf('*&')!=-1?'':row.f_no_checkplan}}</td>
            <td style="text-align:center" :class="{}" v-if="$parent.$parent.$parent.nameForSql.includes('f_residential_area')">{{row.f_residential_area.indexOf('*&')!=-1?'':row.f_residential_area}}</td>
            <td style="text-align:center" :class="{}" v-if="$parent.$parent.$parent.nameForSql.includes('f_plan_name')">{{row.f_plan_name.indexOf('*&')!=-1?'':row.f_plan_name}}</td>
            <td style="text-align:center" :class="{}" v-if="$parent.$parent.$parent.nameForSql.includes('f_checker_name')">{{row.f_checker_name.indexOf('*&')!=-1?'':row.f_checker_name}}</td>
            <td style="text-align:center" :class="{'defectfront':row.f_plan_total==row.f_user_number}" v-if="$parent.$parent.$parent.nameForSql.includes('f_user_number')">{{row.f_user_number}}</td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center">{{row.f_plan_total}}</td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center">{{row.f_weijian}}</td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center">{{row.f_yijian}}</td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center">{{row.f_jhruhu}}</td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center"><nobr>{{row.f_check_bv}}</nobr></td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center">{{row.anjianlv}}</td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center"><nobr>{{row.f_ruhu}}</nobr></td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center"><nobr>{{row.f_buyu}}</nobr></td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center"><nobr>{{row.f_fufang}}</nobr></td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center">{{row.f_jujian}}</td>
          </tr >
        </template>
        <template partial="foot">
          <tr>
          <td  style="text-align:center"><strong>合计：</strong></td>
          <td  style="text-align:center" :colspan="$parent.$parent.$parent.nameForSql.length"></td>
          <td  style="text-align:center"><strong>{{model.sums.f_plan_total}}</strong></td>
            <td  style="text-align:center"><strong>{{model.sums.f_weijian}}</strong></td>
            <td  style="text-align:center"><strong>{{model.sums.f_yijian}}</strong></td>
            <td  style="text-align:center"><strong>{{model.sums.f_jhruhu}}</strong></td>
            <td  style="text-align:center">{{model.sums.f_plan_total == 0 ? 0 : ((model.sums.f_jhruhu/model.sums.f_plan_total).toFixed(4) * 100).toFixed(2) }}%</td>
            <td  style="text-align:center">{{model.sums.f_plan_total == 0 ? 0 :((model.sums.f_yijian/model.sums.f_plan_total).toFixed(4) * 100).toFixed(2) }}%</td>
          <td  style="text-align:center"><nobr><strong>{{model.sums.f_ruhu}}</strong></nobr></td>
          <td  style="text-align:center"><nobr><strong>{{model.sums.f_buyu}}</strong></nobr></td>
          <td  style="text-align:center"><nobr><strong>{{model.sums.f_fufang}}</strong></nobr></td>
          <td  style="text-align:center"><strong>{{model.sums.f_jujian}}</strong></td>
          </tr >
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>
  /**
   *阶梯气价查询列表
   */
  import {PagedList} from 'vue-client'
  import * as Util from '../../Util'
  import Vue from 'vue'
  import HttpResetClass from "vue-client/src/plugins/HttpResetClass";
  export default {
    data () {
      return {
        checker_name:'',
        depments:[{label:'居民安检班组',value:'居民安检'},{label:'非居民安检班组',value:'非居民安检'}],
        model: new PagedList('rs/sql/getCheckPlanAreaList', 100,{
        }, {f_plan_total:'',f_yijian:'',f_jhruhu:'',f_ruhu:'',f_buyu:'',f_fufang:'',f_jujian:'',f_weijian:''}),
        // 公司下拉
        curorgid: [this.$login.f.orgid],
        row: {},
        f_orgid: '',
        nameForSql: [],
        groupNameForSql: '',
        listflag:false,
        nameflag:false,
        f_check_start:'',
        f_checkxin_start:'',
        f_start_time:'',
        f_end_time:'',
        f_check_end:'',
        f_checkxin_end:'',
        streets:[],
        usertypes:[{label:"全部",value:""},{label:"民用",value:"民用"},{label:"非民用",value:"非民用"}],
        f_user_type:""
      }
    },
    props: {
      row: {},
      config: {},
      ispartial: false,
      f_filialeid: {
        type: String
      }
    },
    ready () {
      this.nameForSql = ['f_checker_name','f_plan_name']
      this.groupNameForSql = 'f_checker_name,f_plan_name'
      this.search()
      this.initstreets()
    },
    methods: {
      getchecker(val){
        this.$refs.paged.$refs.criteria.model.f_checker_id = ''
        if(val && val.length > 0){
          this.checker_name = ''
          val.forEach(user => {
            this.checker_name += `'${user.name}',`
          })
          this.checker_name = this.checker_name.slice(0,this.checker_name.length-1)
        }else{
          this.checker_name = ''
        }
      },
      getRes(){

      },
      async initstreets(){
        this.streetslist = []
        let HttpReset = new HttpResetClass()
        let req = await HttpReset.load('POST', 'rs/sql/address_singleTableOrderBy', {
          data: {
            items: '*',
            tablename: 't_street',
            orderitem: 'id',
            condition: `f_orgid = '${this.$login.f.orgid}'`
          }
        }, {resolveMsg: null, rejectMsg: '获取地址失败!'})
        let redata = []
        redata.push({
          label: '全部',
          value: '',
        })
        req.data.forEach((row, n) => {
          redata[n+1] = {
            label: row.f_street,
            value: row.f_street,
            data:row,
            id:row.id
          }
        })
        this.streets=redata
      },
      hiddenr() {
        this.$parent.isdetail = !this.$parent.isdetail
      },
      search () {
        this.$refs.paged.$refs.cri.search()
        this.$dispatch('search')

      },
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val)
        this.f_filialeid = val[0]
      },
      selfSearch (args) {
        if (!this.f_orgid) {
          this.getorg([this.$login.f.orgid])
          args.condition = `f_filialeid  in ('${this.$login.f.orgid}') and ${args.condition}`
        }
        if (this.f_orgid) {
          args.condition = `f_filialeid  in ${this.f_orgid} and ${args.condition}`
        }
        if(this.checker_name){
          args.condition += ` and  f_checker_name in (${this.checker_name}) `
        }
        if(this.f_checkxin_start){
          args.condition += `  and f_start_time>='${this.f_checkxin_start}'`
        }
        if(this.f_checkxin_end){
          args.condition += ` and f_end_time<='${this.f_checkxin_end}' `
        }
        if(this.f_check_start){
          args.condition += ` and f_year>=${new Date(this.f_check_start).getFullYear()} and f_month>=${new Date(this.f_check_start).getMonth()+1}`
        }
        if(this.f_check_end){
          args.condition += ` and f_year<=${new Date(this.f_check_end).getFullYear()} and f_month<=${new Date(this.f_check_end).getMonth()+1}`
        }
        this.$refs.paged.$refs.grid.$el.scrollTop = 0
        this.model.params.f_filialeids='('+this.$login.f.orgid+')'
        this.model.params.f_start_time= this.f_start_time==''  ? '' : this.f_start_time+" 00:00:00"
        this.model.params.f_end_time= this.f_end_time=='' ? '': this.f_end_time+" 23:59:59"
        this.model.paramSource = {groupName: `'${this.groupNameForSql}'`}
        this.model.search(args.condition, args.model)
      },
      // 拼接groupName1
      getGroupName1 () {
        var temp=['f_plan_month','f_no_checkplan','f_residential_area','f_plan_name','f_checker_name','f_user_number']
        var newtemp=[]

        for (var i=0;i<temp.length;i++){

          for (var j=0;j<this.nameForSql.length;j++){
            //tag
            if(temp[i]==this.nameForSql[j]){
              newtemp.push(this.nameForSql[j])
            }
          }
        }
        let str = ''

        for (var i=0;i < newtemp.length - 1; i++) {
          str += newtemp[i] + ','
        }
        str += newtemp[i]
        this.groupNameForSql = str.trim()
      }
    },
    watch: {
      'model.rows'(){
        //tag
        if(this.listflag){
          return;
        }
        this.listflag=true
        //tag
        var temp=['f_plan_month','f_no_checkplan','f_residential_area','f_plan_name','f_checker_name','f_user_number']
        var newtemp=[]
        for (var i=0;i<temp.length;i++){

          for (var j=0;j<this.nameForSql.length;j++){
            //tag
            if(temp[i]==this.nameForSql[j]){
              newtemp.push(this.nameForSql[j])
            }
          }
        }
        if((newtemp.includes('f_user_number')&&newtemp.length==2)||(!newtemp.includes('f_user_number')&&newtemp.length==1)){
          return
        }
        var temp=JSON.parse(JSON.stringify(this.model.rows))

        //遍历查询结果 为指定列增加样式
        for (var i=0;i<newtemp.length-1;i++){
          if(newtemp[i]=='f_user_number'){
            return;
          }
          //tag
          var ss=''
          for (var j=0;j<temp.length;j++){
            if(j>0&&i>0){
              var flag=false
              for(var t=i-1;t>-1;t--){
                //tag
                //tag
                //tag
                //tag
                if(temp[j-1][newtemp[t]]!=temp[j][newtemp[t]]&&temp[j][newtemp[t]].indexOf('*&')==-1){
                  flag=true
                  break
                }
              }
               //tag
              if(flag){
                ss=temp[j][newtemp[i]]
                continue
              }
            }
            if(temp[j][newtemp[i]]==ss){
              temp[j][newtemp[i]]=temp[j][newtemp[i]]+'*&'
            }else {
              ss=temp[j][newtemp[i]]
            }
        }
      }
      //tag
      this.model.rows=JSON.parse(JSON.stringify(temp))
      },
      'model.rows.length'() {
        // 如果查询无结果，将合计置为0
        if(this.model.rows.length < 1){
          this.model.sums.f_plan_total = 0
          this.model.sums.f_jhruhu = 0
          this.model.sums.f_ruhu = 0
          this.model.sums.f_buyu = 0
          this.model.sums.f_jujian = 0
          this.model.sums.f_yijian = 0
          this.model.sums.f_weijian = 0
        }
      }
    }
  }
</script>

<style scoped>

  .td-0{
    border: 1px solid #000000;
  }

  .td-1{
      border: none;
      border-top: 1px solid #000000;
      border-left: 1px solid #000000;
      border-right: 1px solid #000000;
    }

    .td-2{
      border: none;
      border-left: 1px solid #000000;
      border-right: 1px solid #000000;
    }
    .td-4{
      border: none;
      border-top: 1px solid #000000;
      border-left: 1px solid #000000;
      border-right: 1px solid #000000;
      border-bottom: 1px solid #000000;
    }
    .td-3{
      border: none;
      border-left: 1px solid #000000;
      border-right: 1px solid #000000;
      border-bottom: 1px solid #000000;
    }
  .defectfront{
    background-color: chartreuse;
  }
</style>
