<template>
  <div  class="flex">
    <criteria-paged :model="model" v-ref:paged :pager="false">
      <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-3':$parent.$parent.$parent.isdetail,'col-sm-2':!$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">组织机构</label>
              <res-select restype='organization'
                          class="select select_list"
                          style="width: 60%"
                          @res-select="$parent.$parent.getorg"
                          :initresid='$parent.$parent.curorgid'>
              </res-select>
            </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_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="$parent.$parent.f_user_type" v-model='$parent.$parent.f_user_type'
                        :value-single="true" style="width: 60%"
                        class="select_list select"
                        :options='$parent.$parent.usertypes' placeholder='用户类型'
                        close-on-select></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 class="row">
            <div class="col-sm-12">
              <label class="control-label" style="color: #c7254e">汇 总 项</label>
              <input type="checkbox" id="f_plan_month" value="f_plan_month"  v-model="$parent.$parent.nameForSql"/>
              <label for="f_plan_month">计划月份</label>
              <input type="checkbox" id="f_no_checkplan" value="f_no_checkplan" v-model="$parent.$parent.nameForSql"/>
              <label for="f_no_checkplan">计划类型</label>
              <input type="checkbox" id="f_residential_area" value="f_residential_area" v-model="$parent.$parent.nameForSql"/>
              <label for="f_residential_area">小区名称</label>
              <input type="checkbox" id="f_plan_name" value="f_plan_name" v-model="$parent.$parent.nameForSql"/>
              <label for="f_plan_name">计划名称</label>
              <input type="checkbox" id="f_checker_name" value="f_checker_name" v-model="$parent.$parent.nameForSql"/>
              <label for="f_checker_name">安检员</label>
              <input type="checkbox" id="f_user_number" value="f_user_number" v-model="$parent.$parent.nameForSql"/>
              <label for="f_checker_name">档案数量</label>
            </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="{'defectfront':row.f_plan_total==row.f_user_number,'td-0':row.f_plan_month==undefined,'td-1':model.rows.length!=($index+1)&&row.f_plan_month.indexOf('*&')==-1,'td-2':model.rows.length!=($index+1)&&row.f_plan_month.indexOf('*&')!=-1,'td-3':model.rows.length==($index+1)&&row.f_plan_month.indexOf('*&')!=-1,'td-4':model.rows.length==($index+1)&&row.f_plan_month.indexOf('*&')==-1}" 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="{'defectfront':row.f_plan_total==row.f_user_number,'td-0':row.f_no_checkplan==undefined,'td-1':model.rows.length!=($index+1)&&row.f_no_checkplan.indexOf('*&')==-1,'td-2':model.rows.length!=($index+1)&&row.f_no_checkplan.indexOf('*&')!=-1,'td-3':model.rows.length==($index+1)&&row.f_no_checkplan.indexOf('*&')!=-1,'td-4':model.rows.length==($index+1)&&row.f_no_checkplan.indexOf('*&')==-1}" 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="{'defectfront':row.f_plan_total==row.f_user_number,'td-0':row.f_residential_area==undefined,'td-1':model.rows.length!=($index+1)&&row.f_residential_area.indexOf('*&')==-1,'td-2':model.rows.length!=($index+1)&&row.f_residential_area.indexOf('*&')!=-1,'td-3':model.rows.length==($index+1)&&row.f_residential_area.indexOf('*&')!=-1,'td-4':model.rows.length==($index+1)&&row.f_residential_area.indexOf('*&')==-1}" 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="{'defectfront':row.f_plan_total==row.f_user_number,'td-0':row.f_plan_name==undefined,'td-1':model.rows.length!=($index+1)&&row.f_plan_name.indexOf('*&')==-1,'td-2':model.rows.length!=($index+1)&&row.f_plan_name.indexOf('*&')!=-1,'td-3':model.rows.length==($index+1)&&row.f_plan_name.indexOf('*&')!=-1,'td-4':model.rows.length==($index+1)&&row.f_plan_name.indexOf('*&')==-1}" 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="{'defectfront':row.f_plan_total==row.f_user_number,'td-0':row.f_checker_name==undefined,'td-1':model.rows.length!=($index+1)&&row.f_checker_name.indexOf('*&')==-1,'td-2':model.rows.length!=($index+1)&&row.f_checker_name.indexOf('*&')!=-1,'td-3':model.rows.length==($index+1)&&row.f_checker_name.indexOf('*&')!=-1,'td-4':model.rows.length==($index+1)&&row.f_checker_name.indexOf('*&')==-1}" 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+row.f_buyu+row.f_jujian+row.f_jiguangjianlou+row.f_liganbaoya+row.f_shiwaiguanfa}}</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"><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_jiguangjianlou}}</nobr></td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center"><nobr>{{row.f_liganbaoya}}</nobr></td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center"><nobr>{{row.f_shiwaiguanfa}}</nobr></td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center">{{row.f_jujian}}</td>
            <td :class="{'defectfront':row.f_plan_total==row.f_user_number}" style="text-align:center"><nobr>{{row.f_check_bv}}</nobr></td>
            <!--<td style="text-align: center;">-->
              <!--<button class="btn btn-link" @click.stop="$parent.$parent.$parent.showhistory(row)">查看</button>-->
            <!--</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+model.sums.f_buyu+model.sums.f_jujian+model.sums.f_jiguangjianlou+model.sums.f_liganbaoya+model.sums.f_shiwaiguanfa}}</strong></td>
          <!--<td  style="text-align:center"><strong>{{model.sums.f_yijian}}</strong></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_jiguangjianlou}}</strong></nobr></td>
            <td  style="text-align:center"><nobr><strong>{{model.sums.f_liganbaoya}}</strong></nobr></td>
            <td  style="text-align:center"><nobr><strong>{{model.sums.f_shiwaiguanfa}}</strong></nobr></td>
          <td  style="text-align:center"><strong>{{model.sums.f_jujian}}</strong></td>
          <td  style="text-align:center"><strong>{{model.sums.f_plan_total>0?(model.sums.f_ruhu/model.sums.f_plan_total*100).toFixed(2)+'%':0}}</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 {
        model: new PagedList('rs/sql/getCheckPlanAreaList', 99999,{},{f_plan_total:'',f_weijian:'',f_yijian:'',f_ruhu:'',f_buyu:'',f_jiguangjianlou:'',f_liganbaoya:'',f_shiwaiguanfa:'',f_jujian:''}),
        // 公司下拉
        curorgid: [this.$login.f.orgid],
        row: {},
        f_orgid: '',
        nameForSql: [],
        groupNameForSql: '',
        listflag:false,
        nameflag:false,
        f_check_start:'',
        f_check_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_plan_month', 'f_residential_area','f_plan_name']
      this.groupNameForSql = 'f_plan_month,f_residential_area,f_plan_name'
      this.f_check_start=Util.toStartDateYearString()
      // this.search()
      this.initstreets()
    },
    methods: {
      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 = []
        req.data.forEach((row, n) => {
          redata[n] = {
            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()
      },
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val)
        this.f_filialeid = val[0]
      },
      selfSearch (args) {
        let startyear=''
        let startmon=''
        let endyear=''
        let endmon=''
        let datetime
        if (!this.f_orgid) {
          this.getorg([this.$login.f.orgid])
        }
        if (this.f_orgid) {
          args.condition = `f_filialeid  in ${this.f_orgid} and ${args.condition}`
        }
        if(this.f_check_start){
          datetime=new Date(this.f_check_start)
          startyear=datetime.getFullYear()
          startmon=datetime.getMonth()+1
        }
        if(this.f_check_end){
          datetime=new Date(this.f_check_end)
          endyear=datetime.getFullYear()
          endmon=datetime.getMonth()+1
        }
        this.$refs.paged.$refs.grid.$el.scrollTop = 0
        this.model.params.startyear=startyear
        this.model.params.startmon=startmon
        this.model.params.endyear=endyear
        this.model.params.endmon=endmon
        this.model.params.f_user_type=this.f_user_type
        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: {
      // 'nameForSql' () {
      //
      //   this.getGroupName1()
      // },
      '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_weijian = 0
          this.model.sums.f_yijian = 0
          this.model.sums.f_ruhu = 0
          this.model.sums.f_buyu = 0
          this.model.sums.f_jiguangjianlou= 0
          this.model.sums.f_liganbaoya = 0
          this.model.sums.f_shiwaiguanfa = 0
          this.model.sums.f_jujian = 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>
