<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="{'form-group col-sm-3':$parent.$parent.$parent.isdetail,'form-group 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-6':$parent.$parent.$parent.isdetail,'col-sm-4':!$parent.$parent.$parent.isdetail}">
              <role-selector-safe
                role-name="安检员"
                role-lable="安&ensp;检&ensp;员"
                @re-res="$parent.$parent.getorg"
                :value.sync="model.f_checker_id"
                v-model="model.f_checker_id"
                condition="f_checker_name like '%{}%'">
              </role-selector-safe>
            </div>
            <div  :class="{'form-group col-sm-3':$parent.$parent.$parent.isdetail,'form-group 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="{'form-group col-sm-3':$parent.$parent.$parent.isdetail,'form-group col-sm-2':!$parent.$parent.$parent.isdetail}">
              <label class="font_normal_body">计划名称</label>
              <v-select class="select_list select"
                        placeholder='计划名称' style="width: 60%"
                        :search="true"
                        v-model="model.f_plan_name"
                        :value.sync="model.f_plan_name"
                        :options='$parent.$parent.planName'
                        condition="f_plan_id = '{}'"
                        :value-single="true"
                        close-on-select clear-button></v-select>

            </div>
            <!--<div  :class="{
            'form-group col-sm-3':$parent.$parent.$parent.isdetail,'form-group 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="form-group col-sm-2" v-if="!$parent.$parent.$parent.isdetail"></div>
            <div  :class="{'form-group col-sm-3':$parent.$parent.$parent.isdetail,'form-group col-sm-2':!$parent.$parent.$parent.isdetail}" style="float:right; padding-right: 25px">
              <div class="span" style="float:right;">
                <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="button_spacing" style="float: right;margin-left:10px"
                     :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                     @click="$parent.$parent.hidden()">
                </div>
<!--                <export-excel-safe   :data="$parent.$parent.exportParameter"
                                     :field="$parent.$parent.exportExcelField"
                                     sqlurl="rs/logic/SafeExportExcel" sql-name="getCheckPlanAreaList"
                                     template-name='安检汇总' :choose-col="true">
                </export-excel-safe>-->
                <button id="btnaaaa" style="float: right" class="button_search"
                        @click="$parent.$parent.exportexcel()"
                        v-el:cx>导出</button>
                <button id="btnaaa" style="float: right" class="button_search"
                        @click="$parent.$parent.getGroupName1(),search(),$parent.$parent.getexportExcelField(),$parent.$parent.$parent.isdetail=false,$parent.$parent.listflag=false"
                        v-el:cx>查询</button>
              </div>
            </div>
            <div  :class="{'form-group col-sm-3':$parent.$parent.$parent.isdetail,'form-group col-sm-2':!$parent.$parent.$parent.isdetail}" v-if="$parent.$parent.criteriaShow">
              <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.isdetail,'form-group col-sm-2':!$parent.$parent.$parent.isdetail}" v-if="$parent.$parent.criteriaShow">
              <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.isdetail,'form-group col-sm-2':!$parent.$parent.$parent.isdetail}" v-if="$parent.$parent.criteriaShow">
              <label class="font_normal_body">计划类型</label>
              <v-select class="select_list select"
                        placeholder='计划类型' style="width: 60%"
                        :search="true"
                        v-model="model.f_no_checkplan"
                        :value.sync="model.f_no_checkplan"
                        :options='$parent.$parent.f_no_checkplans'
                        condition="f_no_checkplan = '{}'"
                        :value-single="true"
                        close-on-select clear-button></v-select>

            </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 id="excelid" :model="model" partial='list' class="list_area table_sy excelid"  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>-->
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td style="text-align:center">{{$index + 1}}</td>
            <td style="text-align:center"  v-if="$parent.$parent.$parent.nameForSql.includes('f_plan_month')">{{row.f_plan_month}}</td>
            <td style="text-align:center"  v-if="$parent.$parent.$parent.nameForSql.includes('f_no_checkplan')">{{row.f_no_checkplan}}</td>
            <td style="text-align:center"  v-if="$parent.$parent.$parent.nameForSql.includes('f_residential_area')">{{row.f_residential_area}}</td>
            <td style="text-align:center"  v-if="$parent.$parent.$parent.nameForSql.includes('f_plan_name')">{{row.f_plan_name}}</td>
            <td style="text-align:center"  v-if="$parent.$parent.$parent.nameForSql && $parent.$parent.$parent.nameForSql.includes('f_checker_name')">{{row.f_checker_name}}</td>
            <td style="text-align:center" v-if="$parent.$parent.$parent.nameForSql.includes('f_user_number')">{{row.f_user_number}}</td>
            <td style="text-align:center">{{row.f_plan_total}}</td>
            <td style="text-align:center">{{row.f_weijian}}</td>
            <td style="text-align:center"><nobr>{{row.f_yijian}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_ruhu}}</nobr></td>
            <td style="text-align:center">{{row.f_buyu}}</td>
            <td style="text-align:center"><nobr>{{row.f_jujian}}</nobr></td>
            <td 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'></template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>
  /**
   *阶梯气价查询列表
   */
  import {PagedList} from 'vue-client'
  import { HttpResetClass } from 'vue-client'
  import XLSX from 'xlsx'
  export default {
    data () {
      return {
        model: new PagedList('rs/sql/getCheckPlanAreaList', 99999),
        // 公司下拉
        curorgid: [this.$login.f.orgid],
        row: {},
        f_orgid: '',
        nameForSql: [],
        groupNameForSql: '',
        listflag:false,
        nameflag:false,
        criteriaShow: false,
        f_check_start:'',
        f_check_end:'',
        planName:[{label: '全部', value: ''}],
        f_no_checkplans: [{label: '全部', value: ''},{label: '共享计划', value: '共享计划'},{label: '单独计划', value: '单独计划'}],
        exportExcelField: {
          'f_weijian':'未检',
          'f_yijian':'已检',
          'f_ruhu':'入户',
          'f_buyu':'到访不遇',
          'f_jujian':'拒检',
          'f_check_bv':'安检率'
        }
      }
    },
    props: {
      row: {},
      config: {},
      ispartial: false,
      f_filialeid: {
        type: String
      }
    },
    ready () {
      this.nameForSql = ['f_plan_month', 'f_plan_name','f_checker_name','f_no_checkplan']
      this.groupNameForSql = 'f_plan_month', 'f_plan_name','f_checker_name','f_no_checkplan'
      this.pl()
      // this.search()

    },
    methods: {
      exportexcel(){
        //tag
// 以上四行也可以直接一行搞定，如果不需要对表格数据进行修改的话
        const aa = document.getElementsByClassName("list_area table_sy excelid")[0]
        console.log(aa)
        let workbook = XLSX.utils.table_to_book(document.getElementsByClassName("list_area table_sy excelid")[0])
        try {
          XLSX.writeFile(workbook, '安检情况汇总导出.xlsx');
        } catch(e) {
          //tag;
        }
      },
      // 获取导出列
      getexportExcelField(){
        this.exportExcelField = {
          'f_weijian':'未检',
          'f_yijian':'已检',
          'f_ruhu':'入户',
          'f_buyu':'到访不遇',
          'f_jujian':'拒检',
          'f_check_bv':'安检率',
          'f_plan_total':'计划总数'
        }
        let forName = this.groupNameForSql.split(",")
        for (let i = 0; i < forName.length; i++) {
          let ff = forName[i]
          if (ff==='f_plan_month') {
            this.exportExcelField.f_plan_month='计划月份'
          }
          if (ff==='f_no_checkplan') {
            this.exportExcelField.f_checker_name='计划类型'
          }
          if (ff==='f_plan_name') {
            this.exportExcelField.f_plan_name='计划名称'
          }
          if (ff==='f_checker_name') {
            this.exportExcelField.f_checker_name='安检员'
          }if (ff==='f_residential_area') {
            this.exportExcelField.f_checker_name='小区名称'
          }if (ff==='f_user_number') {
            this.exportExcelField.f_checker_name='档案数量'
          }
        }
      },
       pl() {
         let getGasman = new HttpResetClass()
         getGasman.load('POST', `rs/sql/safe_singleTable_OrderBy`, {
          data: {
            items: "id,f_plan_name",
            tablename: "t_check_plan",
            condition: `f_filialeid = '${this.curorgid}'`,
            orderitem: "id"
          }
        }, {resolveMsg: null, rejectMsg: null}).then((plne) => {
          //tag
          //tag
          //tag
          for (let i = 0; i < plne.data.length; i++) {
            this.planName.push({label: plne.data[i].f_plan_name, value: plne.data[i].id})
          }
          //tag
        })
      },
      hiddenr() {
        this.$parent.isdetail = !this.$parent.isdetail
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
       },
      search () {
        this.$refs.paged.$refs.cri.search()
        this.$dispatch('search')
      },
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val.resids)
        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.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
        if (this.groupNameForSql=='undefined'){
          //tag
          this.$showMessage("请至少选择一项进行汇总")
          return
        }
        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()
      // },
      'nameForSql' () {
        var e = document.createEvent("MouseEvents");
        e.initEvent("click", true, true);
        document.getElementById("btnaaa").dispatchEvent(e);
      },

    },
    computed:{
      exportParameter(){
        // excel 导出条件拼接
        return {
            condition: this.$refs.paged.model.condition,
            groupName: this.groupNameForSql
          }
      }
    }
  }
</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;
  }
</style>
