<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-horizontal  select-overspread auto" partial>
          <div class="row">
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">组织机构</label>
              <res-select restype='organization'
                          :is-mul="false"
                          :initresid="$parent.$parent.curorgid"
                          @res-select="$parent.$parent.getorgCur">
              </res-select>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">小&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;区</label>
              <input type="text" class="input_search" v-model="model.f_residential_arearea"
                     condition="a.f_residential_area like '%{}%'" placeholder="小区"
                     :size="model.f_residential_arearea ? model.f_residential_arearea.length*2 : 6"/>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">楼&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号</label>
              <input type="text" class="input_search" v-model="model.f_building"
                     condition="a.f_building like '%{}%'" placeholder="楼号"
                     :size="model.f_building ? model.f_building.length*2 : 6"/>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">单&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;元</label>
              <input type="text" class="input_search" v-model="model.f_unit"
                     condition="a.f_unit like '%{}%'" placeholder="单元"
                     :size="model.f_unit ? model.f_unit.length*2 : 6"/>
            </div>
            <div class="col-sm-2 form-group">
              <label class="font_normal_body">地&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;址</label>
              <input type="text" class="input_search" v-model="model.f_address"
                     condition="a.f_address like '%{}%'" placeholder="地址信息"
                     :size="model.f_address ? model.f_address.length*2 : 6"/>
            </div>
            <div class="span" style="float:right;">
              <button class="button_search button_spacing" @click="$parent.$parent.search()" v-el:cx>查询</button>
              <button class="button_new button_spacing" @click="$parent.$parent.clear()" v-el:cx>清空</button>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' style="overflow: auto" class="list_area table_sy" v-ref:grid>
        <template partial='head'>
          <tr>
            <th>
              <nobr>地址信息</nobr>
            </th>
            <th>
              <nobr>选择</nobr>
            </th>
          </tr>
        </template>
        <template partial='body'>
          <tr class="row" :class="[row.state?'bg-info':'']">
            <td style="text-align:center">
              <nobr>{{ row.f_adjustable_address }}</nobr>
            </td>
            <td style="text-align:center">
              <input type='checkbox' v-model="row.state" @change="$parent.$parent.$parent.confirmaddress(row)">
            </td>
          </tr>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

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

export default {
  data () {
    return {
      rows: [],
      criteriaShow: false,
      model: new PagedList('rs/sql/sale_getAddress', 50, {}),
      addflag: false,
      addtitle: '',
      userid: this.$login.f.id,
      source: `tool.getFullTree(this.getRights().where(row.getType() == $organization$))`,
      // 公司下拉
      curorgid: [this.$login.f.orgid],
      f_orgid: '',
      // 文件上传弹框
      showFile: false
    }
  },
  title: '地址列表',
  props: {
    f_filialeids: {
      type: String
    },
    company: {
      type: String,
      default: ''
    }
  },
  ready () {
    this.search()
  },
  methods: {
    getorgCur (val) {
      this.f_orgid = this.$login.convertToIn(val)
      this.f_filialeids = val[0]
    },
    hidden () {
      this.criteriaShow = !this.criteriaShow
    },
    // 添加方法
    add (val) {
      if (this.f_filialeids) {
        this.$refs.paged.$refs.grid.selectStore.selected = null
        this.$dispatch('add', val)
      } else {
        this.$showAlert('请先选择左上角的所在分公司', 'warning', 2000)
      }
    },
    // 修改
    modify (val) {
      this.$refs.paged.$refs.grid.selectStore.selected = null
      this.$dispatch('modify', val)
    },
    confirmaddress (row) {
      if (row.state !== true) {
        console.log(1111111)
        row.state = false
        var index = this.rows.indexOf(row)
        if (index !== -1) {
          this.rows.splice(index, 1)
        }
      } else {
        console.log(222)
        row.state = true
        this.rows.push(row)
      }
    },
    search () {
      this.$refs.paged.$refs.cri.search()
    },

    selfSearch (args) {
      if (!this.f_orgid) {
        this.getorgCur([this.$login.f.orgid])
      }
      if (this.f_orgid) {
        args.condition = `${args.condition} and a.f_orgid in ${this.f_orgid}`
      }
      this.model.search(args.condition, args.model)
    },

    // 清空
    clear () {
      this.$refs.paged.$refs.cri.model = {}
    },
    getRes (obj) {
      console.log(obj, '选择到公司')
      this.f_filialeids = obj.resids
    },
    getorg (val) {
      // this.f_orgid = this.$login.convertToIn(val)
      // this.f_filialeids = val[0]
    }
  },
  watch: {
    'company' (val) {
      this.f_orgid = val
      this.f_filialeids = val
    }
  }

}
</script>
<style scoped>
.bg-info {
  background-color: #5cb85c;
}
</style>
