<template>
  <div class="span" >
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearchsplit' v-ref:cri>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial >
          <div class="row">
            <div class="form-group col-sm-4">
              <label class="font_normal_body">组织机构</label>
              <res-select
                restype='organization'
                @res-select="$parent.$parent.getorgfen"
                is-mul="false"
                :initresid='$parent.$parent.curorgidfen'
              >
              </res-select>
            </div>

            <div class="span" style="float:right;">
              <button class="button_search button_spacing" @click="search()">查询</button>
              <button class="button_clear button_spacing" @click="$parent.$parent.clear()">清空</button>
              <button class="button_clear button_spacing" @click="$parent.$parent.addToTotal()" >添加至总表</button>
              <div style="float: right"  class="button_spacing"
                   :class="{'button_shrink_top':$parent.$parent.criteriaShowfen,'button_shrink_bottom':!$parent.$parent.criteriaShowfen}"
                   @click="$parent.$parent.hiddenfen()"></div>
            </div>
          </div>

          <div class="row" v-show="$parent.$parent.criteriaShowfen">
            <div class="form-group col-sm-4">
              <label class="font_normal_body">客户编号</label>
              <input  type="text" style="width:60%"  class="input_search" v-model="model.f_userinfo_code" placeholder='客户编号'
                      condition="f_userinfo_code = '{}'">
            </div>
            <div class="form-group col-sm-4">
              <label class="font_normal_body">客户名称</label>
              <input  type="text" style="width:60%"  class="input_search" v-model="model.f_user_name" placeholder='客户名称'
                      condition="f_user_name like '%{}%'">
            </div>
            <div class="form-group col-sm-4">
              <label class="font_normal_body">客户地址</label>
              <input  type="text" style="width:60%"  class="input_search" v-model="model.f_address" placeholder='客户地址'
                      condition="f_address like '%{}%'">
            </div>
          </div>
        </div>
      </criteria>
      <data-grid  :model="model" class="list_area table_sy"  partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>
              <input type="checkbox"  @click="$parent.$parent.$parent.isAll(model.pageIndex)">
            </th>
            <th><nobr>序号</nobr></th>
            <th><nobr>客户名称</nobr></th>
            <th><nobr>表编号</nobr></th>
            <th><nobr>表品牌</nobr></th>
            <th><nobr>表型号</nobr></th>
            <!-- <th>是否总表</th> -->
          </tr>
        </template>
        <template partial='body'>
          <td>
            <input type="checkbox" v-model="$parent.$parent.$parent.selectedDatas"
                   :value="row">
          </td>
          <td>{{$index+1}}</td>
          <td>{{row.f_user_name}}</td>
          <td>{{row.f_user_id}}</td>
          <td>{{row.f_meter_brand}}</td>
          <td>{{row.f_meter_style}}</td>
          <!-- <td style="text-align:center">{{row.f_totalsplit_type}}</td> -->
        </template>
      </data-grid>
    </criteria-paged>
    <modal :show.sync="shareratio" v-ref:modal backdrop="false">
      <header slot="modal-header" class="modal-header">
        <button type="button" class="close" @click="close"><span>&times;</span></button>
        <h4 class="modal-title">承担系数设置</h4>
      </header>
      <article slot="modal-body" class="modal-body">
        <div class="panel panel-info">
          <div class="panel-heading">
        		<h3 class="panel-title">总表信息</h3>
        	</div>
          <div class="panel-body">
            <table class="table">
              <thead>
                <tr>
                   <th>客户名称</th>
                   <th>表编号</th>
                   <th>表品牌</th>
                   <th>表型号</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                   <td class= "sharetd">{{data.f_user_name}}</td>
                   <td class= "sharetd">{{data.f_user_id}}</td>
                   <td class= "sharetd">{{data.f_meter_brand}}</td>
                   <td class= "sharetd">{{data.f_meter_style}}</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
        <div class="panel panel-info">
          <div class="panel-heading">
        		<h3 class="panel-title">分表信息</h3>
        	</div>
          <div class="panel-body">
            <table class="table pre-scrollable">
              <thead>
                <tr>
                   <th>客户名称</th>
                   <th>表编号</th>
                   <th>表品牌</th>
                   <th>表型号</th>
                   <th>承担比率</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="single in selectedDatas">
                   <td class= "sharetd">{{single.f_user_name}}</td>
                   <td class= "sharetd">{{single.f_user_id}}</td>
                   <td class= "sharetd">{{single.f_meter_brand}}</td>
                   <td class= "sharetd">{{single.f_meter_style}}</td>
                   <td class= "sharetd">
                     <div class="input-group">
                  		  <input type="number" class="form-control" v-model="single.f_share_ratio" value='0'/>
                  		  <span class="input-group-addon">%</span>
                  	 </div>
                   </td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </article>

      <footer slot="modal-footer" class="modal-footer">
        <button type="button" class="btn btn-default" @click='close()'>取消</button>
        <button type="button" class="btn btn-success" @click='save()'>确认</button>
      </footer>
    </modal>
  </div>
</template>

<script>
  /**
   *阶梯水价查询列表
   */
  import { PagedList } from 'vue-client'

  export default {
    data () {
      return {
        model: new PagedList('rs/sql/getUserAndMeter', 30, {items: '"f_address,f_userinfo_code,f_user_name,f_user_id,f_meter_brand,f_meter_style"'}),
        shareratio: false,
        selectedDatas: [],
        pages: [],
        criteriaShowfen: false,
        curorgidfen: [this.$login.f.orgid],
        f_filialeid: this.$login.convertToIn(this.$login.f.orgid)
      }
    },
    props: {
      data: ['data']
    },
    ready () {
      this.search()
    },
    methods: {
      getorgfen (val) {
        this.f_filialeid = this.$login.convertToIn(val)
      },
      clear () {
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
        })
      },
      hiddenfen () {
        this.criteriaShowfen = !this.criteriaShowfen
      },
      search () {
        this.$refs.paged.$refs.cri.search()
      },
      async selfSearchsplit (args) {
        if (this.f_filialeid) {
          args.condition = `f_filialeid in ${this.f_filialeid} and ${args.condition}`
        }
        console.log('-------------------------')
        console.log(args)
        console.log('-------------------------')
        this.model.search(args.condition, args.model)
      },
      isAll (page) {
        if (this.pages.includes(page)) { // 已添加,取消动作
          // 从ids中删除本页所有数据的ids
          this.addOrRemove(false)
          this.pages = this.pages.filter((item) => {
            return item !== page
          })
        } else { // 未添加
          this.pages.push(page) // 添加id，添加动作
          this.addOrRemove(true)
        }
      },
      addOrRemove (is) {
        let that = this
        console.log('看看有没有总分表', that.data, that.model.rows)
        if (is) { // 添加本页所有数据到ids中
          that.model.rows.forEach((row) => {
            if (!that.selectedDatas.includes(row)) {
              that.selectedDatas.push(row)
            }
          })
        } else { // 从ids中取消所有的数据
          that.model.rows.forEach((row) => {
            that.selectedDatas = that.selectedDatas.filter((item) => {
              return item.f_user_id !== row.f_user_id
            })
          })
        }
        console.log('this.selectedDatas', that.selectedDatas)
      },
      addToTotal () {
        if (!this.data) {
          console.log('总表没有选')
          this.$showAlert('请先选择总表！！！', 'warning', 2000)
        } else {
          this.selectedDatas = this.selectedDatas.filter((item) => {
            return item.f_user_id !== this.data.f_user_id
          })
          this.shareratio = true
        }
      },
      close () {
        this.shareratio = false
      },
      save () {
        console.log('确定了，，，', this.selectedDatas)
        this.$showMessage('确定建立它们之间的总分表关系吗？', ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            this.shareratio = false
            this.$LogicService.addSplit(this.data.f_user_id, this.selectedDatas).then((res) => {
              this.$dispatch('refresh')
            }).catch((error) => {
              this.$dispatch('refresh')
              throw error
            })
            this.selectedDatas = []
          } else if (res === 'cancel') {

          }
        })
      }
    }
  }
</script>
<style media="screen" scoped>
  .sharetd {
    text-align: center;
  }
</style>
