<template>
  <div id="unit" class="flex" >
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='search' v-ref:cri>
        <div novalidate class="form-inline" partial>
          <div class="form-group">
            <v-select :value.sync="model.f_adjustable_id" :options='$parent.$parent.adjustables' placeholder='调压箱' close-on-select
                      v-model='model.f_adjustable_id'   condition="u.f_adjustable_id = '{}'" :search='true'></v-select>
          </div>
          <div class="form-group">
            <button class="btn btn-success width-80" @click="search()" v-el:cx>查询</button>
            <!--<export-excel :data="{condition: $parent.model.condition}"-->
                          <!--sqlurl="rs/logic/exportfile" :bean="'调压箱汇总查询导出'"></export-excel>-->
            <export-excel :data="$parent.$parent.getCondition"
                          :field="$parent.$parent.getfield"
                          sqlurl="rs/logic/exportfile" sql-name="boxSummarize" template-name='调压箱汇总查询导出'  :choose-col="true"></export-excel>
          </div>
        </div>
      </criteria>
      <data-grid  :model="model" partial='list' v-ref:grid :classname="$parent.classname" @dblclick="$parent.dblclick">
        <template partial='head'>
          <tr>
            <th v-for="item in $parent.$parent.$parent.headData"><nobr>{{item}}</nobr></th>
          </tr>
        </template>
        <template partial='body'>
          <td v-for="item in $parent.$parent.$parent.bodyData"><nobr>{{row[item]}}</nobr></td>
        </template>
        <template partial='foot'></template>
      </data-grid>
      <colligate-details :showinfo="showinfo" :businesstype="businesstype" :data="data" @close-info="showinfo=false"></colligate-details>
      <modal :show.sync="printshow" v-ref:modal backdrop="false">
        <header slot="modal-header" class="modal-header text-center">
          <h4 class="modal-title">打印列选择</h4>
          <input type="checkbox" class="" id="f_generations" v-model="all">
          <label for="f_generations" class="font-size">全选</label>
        </header>
        <article slot="modal-body">
          <div class="auto col-sm-11 col-md-offset-1"  style="margin-top: 10px;">
            <div class="col-sm-3" v-for="f in fields">
              <input type="checkbox" class="" :id="'water-owe-details-'+$index" v-model="modelval" :value="$key">
              <label :for="'water-owe-details-'+$index" class="font-size">{{f}}</label>
            </div>
          </div>
        </article>
        <footer slot="modal-footer" class="modal-footer">
          <center>
            <button v-show="printshow" type="button" class="btn btn-default" @click='close()'>取消</button>
            <button v-show="printshow" type="button" class="btn btn-success" @click='print()'>打印</button>
            <print-table :model='model' v-show="false" v-ref:print
                         :top='40' :left='60' :right='40' :bottom='60'
                         :thead="thead" :tfoot='tfoot' :attach="yangsen"
                         :fields="modelval" :printpage="true">
            </print-table>
          </center>
        </footer>
      </modal>
    </criteria-paged>
  </div>
</template>
<script>

  import { PagedList } from 'vue-client'
  import Vue from 'vue'
  let loadParamGem = async function (self) {
    console.log('获取登录信息，', self.$login, self.$appdata, self.$appdata.getParam('用户类型'), Vue.$login.f.name)
    // self.headers.username = Vue.$login.f.name
    // self.headers.username = Vue.$login.guid()
    // await self.search()
    await self.$MagLoadParams.loadParam(self.$login.f)
    self.initAdjustables()
  }
  export default {
    title: '调压箱汇总信息',
    data () {
      return {
        showinfo: false,
        businesstype: '调压箱汇总信息',
        data: {},
        sliceArea: [],
        adjustables: [],
        model: new PagedList('rs/sql/boxSummarize', 150),
        criteriaShow: true,
        classname: 'table table-bordered table table-hover',
        headData: ["序号","片区/管理站","调压箱名称","小区名称","用户数量"],
        bodyData: ["idnumber","f_slice_area","f_adjustable_name","f_residential_area","users"],

        filialeNameStr: null,
        filialeCodeStr: this.$login.f.f_orgids,
        userid: this.$login.f.id,
        source:`tool.getFullTree(this.getRights().where(row.getType() != $zone$).where(row.getType() != $role$))`,

        modelval: [],
        printshow: false,
        all: false,
        fields: {},
        thead: '',
        tfoot: '',
        //合计数据
        sumsmodel: {}
      }
    },
    props: [],
    ready () {
      loadParamGem(this)
      this.model.search('1=1')
    },
    methods: {
      search () {
        this.model.search()
      },
      getPargram () {
        let startDate = this.$refs.paged.$refs.cri.model.startDate
        let endDate = this.$refs.paged.$refs.cri.model.endDate
        return {startDate: startDate, endDate: endDate}
      },
      dblclick(obj) {
        this.data = obj
        this.showinfo = true
      },
      //   async selfSearch (args) {
      // let f_orgstr = this.filialeCodeStr
      // console.log('xxxx',f_orgstr)
      // args.condition =  `${args.condition} and f_orgstr like '${f_orgstr}%'`
      // console.log('我的阐述',JSON.stringify(args.condition))
      // await this.model.search(args.condition, args.model)
      //
      // this.sumsmodel = this.$refs.paged.$refs.grid.model.sums
      //   console.log('合计字段',this.sumsmodel)
      // },
      clear(){
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
      })

      },
      show(){
        this.criteriaShow = true
      },
      hidden(){
        this.criteriaShow = false
      },
      getRes(obj) {
        this.filialeNameStr = obj.res
        this.filialeCodeStr = obj.resids
      },
      stamp () {
        this.all = false
        //默认选择要打印的列
        this.modelval = defaultPrint.config
        this.fields = this.getfield
        console.log('所有打印字段',this.fields)
        this.printshow = true
        this.put()
      },
      put () {
        // 对Modelval进行排序
        this.sortModelval()
        this.thead = `<tr><th colspan=${this.modelval.length}>日汇总表</th></tr><tr>`
        for (let key of this.modelval) {
          this.thead += '<th>' + this.fields[key] + '</th>'
        }
        this.thead += '</tr>'
      },
      print () {
        this.$refs.print.PrintAsFile()
        this.printshow = false
      },
      close () {
        this.printshow = false
        this.all = false
      },
      // 对选择的列进行排序
      sortModelval () {
        let sortModel = []
        Object.keys(this.fields).forEach((key) => {
          if (this.modelval.includes(key)) {
          sortModel.push(key)
        }
      })
        this.modelval = sortModel
        console.log('选择的打印的字段', this.modelval)
      },
      // getPargram () {
      //   let condition = `id >= \'${this.$refs.paged.$refs.cri.model.f_adjustable_name}\'`
      //   return {condition: condition}
      // }
      initAdjustables () {
        console.log('初始化调压箱', this.$MagGetSaleParam)
        let arr = []
        let filter =  this.$login.f.f_orgids
        this.$MagGetSaleParam.getAdjustable(filter).forEach((item) => {
          let temp = {}
          temp.label = `[${item.value.f_adjustable_id}]-${item.label}`
          temp.value = `${item.value.id}`
          arr.push(temp)
        })
        this.adjustables = [{label: '全部', value: ''}, ...arr]
      },
    },
    watch: {
      'model.rows' (val) {
        val.forEach((item, index) => {
          if (item.f_adjustable_name === '合计' && item.users === 0) {
          val.splice(index, 1)
        }
      })
      },
      'all' (val) {
        if (val) {
          this.modelval = this.bodyData
        } else {
          this.modelval = defaultPrint.config
          this.put()
        }
      },
      'modelval.length' () {
        this.put()
      }
    },
    computed: {
      getCondition(){
        this.$refs.paged.$refs.cri.condition = this.$refs.paged.$refs.cri.condition ? this.$refs.paged.$refs.cri.condition : '1=1'
        return {condition: `${this.$refs.paged.$refs.cri.condition}  and u.f_orgstr like '${this.filialeCodeStr}%'`}
      },
      getfield(){
        let data = {}
        this.bodyData.forEach((value, index)=>{
          data[this.bodyData[index]] = this.headData[index]
        })

        //合计字段打印
        this.tfoot = `<tr><th colspan=${this.modelval.length}>全表汇总信息: `
        if(this.sumsmodel){
          Object.keys(this.sumsmodel).forEach((key) => {
            this.tfoot += this.fields[key] + '合计: ' + `<font color="blue">${this.sumsmodel[key]} </font>`
        })
        }else{
          this.tfoot += '暂无'
        }
        this.tfoot += '</th></tr>'
        return data
      },
      boxname () {
        let arr = []
        let filter = this.$login.f.f_orgids
        console.log("filter", filter)
        this.$appdata.getAdjustable(filter).forEach((item) => {
          console.log("item1111", item)
          let temp = {}
          temp.label = `[${item.value.f_adjustable_id}]-${item.label}`
        temp.value = item.value
        arr.push(temp)
      })
        return [{label: '全部', value: ''}, ...arr]

      }
      // boxname () {
      //   let arr = []
      //   this.$appdata.getAdjustable().forEach((item) => {
      //     let temp = {}
      //     temp.label = `[${item.value.f_adjustable_id}]-${item.label}`
      //   temp.value = `${item.value.id}`
      //   arr.push(temp)
      // })
      //   console.log(AppData.getAdjustable())
      //   return [{label: '全部', value: ''}, ...arr]
      // }
    }
  }
</script>
