<template>
  <div class="flex">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
        <div novalidate class="form-inline" partial>
          <div class="form-group">
            <label>组织机构</label>
            <right-tree :userid.sync='$parent.$parent.userid' :source.sync='$parent.$parent.source'
                        @re-res="$parent.$parent.getRes"></right-tree>
          </div>
          <div class="form-group">
            <input type="text" class="form-control" v-model="model.f_userinfo_code" placeholder='客户编号'
                   condition="f_userinfo_code like '%{}%'">
          </div>
          <div class="form-group">
            <input type="text" class="form-control" v-model="model.f_user_name" placeholder='用户姓名'
                   condition="f_user_name like '%{}%'">
          </div>
          <div class="form-group">
            <input type="text" class="form-control" v-model="model.f_user_phone" placeholder='电话'
                   condition="f_user_phone like '%{}%'">
          </div>
          <div class="form-group">
            <input type="text" class="form-control" v-model="model.f_address" placeholder='用户地址'
                   condition="f_address like '%{}%'">
          </div>
          <div class="form-group">
            <v-select :value.sync="model.f_user_type" v-model="model.f_user_type"
                      :options='$parent.$parent.userType' placeholder='用户类型'
                      condition="f_user_type = '{}'"
                      close-on-select></v-select>
          </div>
          <div class="form-group">
            <v-select :value.sync="model.f_meter_classify" v-model="model.f_meter_classify"  :value-single="true"
                      :options='$parent.$parent.meter_classify' placeholder='气表类型'
                      condition="f_meter_classify = '{}'"
                      close-on-select></v-select>
          </div>
          <div class="form-group">
            <button class="btn btn-success" @click="$parent.$parent.search()" v-el:cx>查询</button>
            <button class="btn btn-success" @click="$parent.$parent.clear()" v-el:cx>清空</button>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <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">{{row.f_userinfo_code}}</td>
            <td style="text-align:center">{{row.f_user_name}}</td>
            <td style="text-align:center">{{row.f_user_phone}}</td>
            <td style="text-align:center">{{row.f_address}}</td>
            <td style="text-align:center">{{row.f_meter_classify}}</td>
            <td style="text-align:center">{{row.f_user_state}}</td>
            <td style="text-align:center">
              <button class="btn btn-link" @click="$parent.$parent.$parent.openEticket(row)">开票</button>
            </td>
          </tr>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>

    <open-hand-eticket v-if="isOpen" :show.sync="isOpen" :data="openData" ></open-hand-eticket>
  </div>
</template>

<script>
  import {PagedList} from 'vue-client'

  export default {
    title: '电子票开具',
    components: {

    },
    data () {
      return {
        model: new PagedList('rs/sql/sale_getHandUser', 20),
        isOpen: false,
        openData: null,
        userid: this.$login.f.id,
        source: `tool.getFullTree(this.getRights().where(row.getType() == $organization$))`,
        res: this.$login.f.f_fengongsi,
        resids: this.$login.f.f_orgids
      }
    },
    ready () {
      this.$refs.paged.$refs.cri.model.f_meter_classify = '机表'
      this.search()
    },
    methods: {
      search () {
        this.$refs.paged.$refs.cri.search()
      },
      selfSearch (args) {
        args.condition = `${args.condition} and f_filialeids like '${this.resids}%'`
        this.model.search(args.condition, args.model)
      },
      openEticket (row) {
        this.isOpen = true
        console.log('要开票了。。', row)
        this.openData = row
      },
      getRes (obj) {
        this.res = obj.res
        this.resids = obj.resids.replace(/\./g, '')
      },
      clear () {
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
        })
        this.$refs.paged.$refs.cri.model['f_meter_classify'] = '机表'
      }
    },

    computed: {
      meter_classify () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('气表类型')]
      },
      userType () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')]
      }
    }

  }
</script>
