<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 auto" partial>
          <div class="form-group form-input-group">
            <right-tree :userid.sync='$parent.$parent.userid' :source.sync='$parent.$parent.source' @re-res="$parent.$parent.getRes"></right-tree>
          </div>
          <div class="form-group form-input-group">
            <input  type="text" class="form-control" v-model="model.f_residential_arearea"
                    condition="f_residential_area like '%{}%'" placeholder="小区"
                    :size="model.f_residential_arearea ? model.f_residential_arearea.length*2 : 2"/>
          </div>
          <div class="form-group form-input-group" >
            <input  type="text" class="form-control" v-model="model.f_building"
                    condition="f_building like '%{}%'" placeholder="楼号"
                    :size="model.f_building ? model.f_building.length*2 : 2"/>
          </div>
          <div class="form-group form-input-group">
            <input  type="text" class="form-control" v-model="model.f_unit"
                    condition="f_unit like '%{}%'" placeholder="单元"
                    :size="model.f_unit ? model.f_unit.length*2 : 2"/>
          </div>
          <div class="form-group form-input-group">
            <input  type="text" class="form-control" v-model="model.f_floor"
                    condition="f_floor like '%{}%'" placeholder="楼层"
                    :size="model.f_floor ? model.f_floor.length*2 : 2"/>
          </div>
          <div class="form-group form-input-group">
            <input  type="text" class="form-control" v-model="model.f_room"
                    condition="f_room like '%{}%'" placeholder="门牌号"
                    :size="model.f_room ? model.f_room.length*2 : 2"/>
          </div>
          <div class="form-group form-input-group" >
            <input  type="text" class="form-control" v-model="model.f_address"
                    condition="f_address like '%{}%'" placeholder="详细地址"
                    :size="model.f_address ? model.f_address.length*2 : 4"/>
          </div>
          <div class="form-group form-input-group">
              <button class="btn btn-success width-80" @click="search()" >查询</button>
              <button class="btn btn-success width-80" @click="$parent.$parent.clear()">清空</button>
              <button class="btn btn-success width-80" @click="$parent.$parent.add()">新增</button>
              <button v-if="!$parent.$parent.$parent.ishujiao" class="btn btn-info width-80" @click="$parent.$parent.importAddress">导入地址</button>
              <button class="btn btn-success width-80" @click="$parent.$parent.addSpecial()">新增特殊</button>
              <button v-if="$parent.$parent.$parent.ishujiao" class="btn btn-danger width-80" @click="$parent.$parent.confirm()">确定</button>
          </div>
        </div>
      </criteria>
      <data-grid  :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th><nobr>序号</nobr></th>
            <th><nobr>id</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>
            <th><nobr>时间</nobr></th>
            <th><nobr>操作</nobr></th>
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td style="text-align:center"><nobr>{{$index+1}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.id}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_area}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_street}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_residential_area}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_building}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_unit}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_floor}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_room}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_address}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_create_date}}</nobr></td>
            <td style="text-align:center">
              <button type="button" name="button" class="btn btn-link"
                      @click.stop="$parent.$parent.$parent.delete(row)">删除</button>
            </td>
          </tr>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
  </div>
  <modal :show.sync="showFile" v-ref:modal backdrop="true">
    <header slot="modal-header" class="modal-header">
      <button type="button" class="close" @click="closeFile"><span>&times;</span></button>
      <h4 class="modal-title">选择文件</h4>
    </header>
    <article slot="modal-body" class="modal-body">
      <div class="form-group">
        <file-upload class="my-file-uploader" name="useraddressUploadFile" action="rs/file/uploadFile" tagname="确定" multiple v-ref:file></file-upload>
      </div>
    </article>
    <footer slot="modal-footer" class="modal-footer"></footer>
  </modal>
</template>

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

  export default {
    data () {
      return {
        model: new PagedList('rs/sql/address_singleTableOrderBy',20,{tablename: `'t_user_address'`,items: `'*'`,orderitem: `'f_create_date desc'`}),

        userid: this.$login.f.id,
        source:`tool.getFullTree(this.getRights().where(row.getType() == $organization$))`,

        //文件上传弹框
        showFile: false
      }
    },
    props:{
      f_filialeids:{
        type: String
      }
    },
    ready(){
      this.init().then(()=>{
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      async init(){
        await this.$refs.paged.$refs.cri.search()
      },
      selfsearch(args){
        //添加分公司查询条件
        args.condition =  `${args.condition} and f_filialeids = '${this.f_filialeids}'`
        console.log(args)
        //执行查询
        this.model.search(args.condition, args.model, args.condValue)
      },
      add(){
        this.$refs.paged.$refs.grid.selectStore.selected = null
        this.$dispatch('add',false)
      },
      addSpecial(){
        this.$refs.paged.$refs.grid.selectStore.selected = null
        this.$dispatch('add',true)
      },
      async delete(row){
        if(row.id){
          var id = row.id
          let service = await this.$resetpost('rs/sql/address_singleTable', {data: {items: 'f_addressid',tablename: 't_service',condition: `f_addressid='${id}'`}},{resolveMsg: null, rejectMsg: null})
          let userinfo = await this.$resetpost('rs/sql/address_singleTable', {data: {items: 'f_useraddress_id',tablename: 't_userinfo',condition: `f_useraddress_id='${id}'`}},{resolveMsg: null, rejectMsg: null})
          if(service.data.length || userinfo.data.length){
            this.$showAlert('该地址下已有工单或档案,无法删除!','warning',900)
          }else{
            this.$resetdelete('rs/entity/t_user_address', {id: id}, {resolveMsg: '删除成功', rejectMsg: '删除失败'}).then((res) => {
              this.$dispatch('refresh')
            })
          }
        }
      },
      //清空
      clear(){
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = ''
        })
      },
      confirm(){
        this.$dispatch('confirm')
      },
      getRes (obj) {
        console.log(obj,'选择到公司')
        this.f_filialeids = obj.resids
      },
      importAddress(){
        this.showFile = true
      },
      // 关闭文件上传对话框
      closeFile() {
        this.showFile = false
        // 将选的文件清空
        this.$refs.file.$el.querySelector('input').value = ''
      },
    },
    watch:{
      //分公司变化
      'f_filialeids'(){
        this.init()
      }
    },
    events: {
      onFileUpload: async function (file, res) {
        console.log("上传完毕"+JSON.stringify(res))
        let data = {
          filepath: res.f_downloadpath,
          f_filialeids: this.f_filialeids
        }
        console.log("filepath="+JSON.stringify(data))
        await this.$resetpost(`rs/logic/address_useraddressimport`,data, {resolveMsg: null, rejectMsg: '导入失败', silent: true}, 0).then((res)=>{
          this.$showAlert("成功导入"+res.data+'条地址', 'success', 2000)
          //关闭上传框
          this.closeFile()
          //重新查询
          this.init()
        })
      },
      'onFileError'(error) {
        this.$showAlert(error, 'warning', 2000)
      }
    }
  }
</script>
