<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 container-fluid auto" partial>

          <div class="row">

            <div :class="$parent.$parent.style" v-show="!$parent.$parent.select">
              <label class="font_normal_body">组织机构</label>
              <res-select
                restype='organization'
                          @res-select="$parent.$parent.getorg"
                          is-mul="false"
                          :initresid='$parent.$parent.curorgid'
              >
              </res-select>
            </div>
            <div :class="$parent.$parent.style" v-show="!$parent.$parent.select">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;小区&nbsp;&nbsp;&nbsp;</label>
              <input  type="text" class="input_search" style="width:60%" v-model="model.f_residential_arearea" style="width:60%"
                      condition="f_residential_area like '%{}%'" placeholder="小区"
                      :size="model.f_residential_arearea ? model.f_residential_arearea.length*2 : 8"/>
            </div>
            <div :class="$parent.$parent.style" v-show="!$parent.$parent.select">
              <label class="font_normal_body" v-show="!$parent.$parent.select">&nbsp;&nbsp;&nbsp;楼号&nbsp;&nbsp;&nbsp;</label>
              <input  type="text" class="input_search" style="width:60%" v-model="model.f_building" style="width:60%"
                      condition="f_building like '%{}%'" placeholder="楼号"
                      :size="model.f_building ? model.f_building.length*2 : 6"/>
            </div>
            <div :class="$parent.$parent.style" v-show="!$parent.$parent.select">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;单元&nbsp;&nbsp;&nbsp;</label>
              <input  type="text" class="input_search" style="width:60%" v-model="model.f_unit" style="width:60%"
                      condition="f_unit like '%{}%'" placeholder="单元"
                      :size="model.f_unit ? model.f_unit.length*2 : 6"/>
            </div>
            <div :class="$parent.$parent.style" v-show="!$parent.$parent.select">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;楼层&nbsp;&nbsp;&nbsp;</label>
              <input  type="text" class="input_search" style="width:60%" v-model="model.f_floor" style="width:60%"
                      condition="f_floor like '%{}%'" placeholder="楼层"
                      :size="model.f_floor ? model.f_floor.length*2 : 6"/>
            </div>
            <div :class="$parent.$parent.style" v-show="!$parent.$parent.select">
              <label class="font_normal_body">&nbsp;&nbsp;&nbsp;门牌号</label>
              <input  type="text" class="input_search" style="width:60%" v-model="model.f_room" style="width:60%"
                      condition="f_room like '%{}%'" placeholder="门牌号"
                      :size="model.f_room ? model.f_room.length*2 : 6"/>
            </div>
            <div  :class="$parent.$parent.style"  v-show="!$parent.$parent.select">
              <label class="font_normal_body">地址ID</label>
              <input  type="text" class="input_search" style="width:60%" v-model="model.id" style="width:60%"
                      condition="id = '{}'" placeholder="地址ID"
                      />
            </div>
            <div :class="$parent.$parent.style" v-show="!$parent.$parent.select">
              <label class="font_normal_body">详细地址</label>
              <input  type="text" class="input_search" style="width:60%" v-model="model.f_address" style="width:60%"
                      condition="f_address like '%{}%'" placeholder="详细地址"
                      :size="model.f_address ? model.f_address.length*2 : 8"/>
            </div>
            <div :class="$parent.$parent.style" v-show="$parent.$parent.select">
              <label class="font_normal_body">地址信息</label>
              <input  type="text" class="input_search" style="width:60%" v-model="model.f_address" style="width:60%"
                      condition="f_address like '%{}%'" placeholder="地址信息"
                      :size="model.f_address ? model.f_address.length*2 : 8"/>
            </div>
            <div class="span" style="float:right;">
              <button class="button_search button_spacing" @click="search()" >查询</button>
              <button class="button_new button_spacing"  style="width:70px" @click="$parent.$parent.add('民用')">添加民用</button>
              <button class="button_new button_spacing" style="width:100px" @click="$parent.$parent.add('非民用')">添加特殊地址</button>
              <button class="button_clear button_spacing" @click="$parent.$parent.clear()">清空</button>
              <button class="button_export button_spacing"  @click="$parent.$parent.importAddress">导入地址</button>
              <!--<div style="float: right"  class="button_spacing" :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"    @click="$parent.$parent.hidden()"></div>-->
            </div>
          </div>
          <!--<div class="row" v-show="$parent.$parent.criteriaShow">-->
            <!---->
          <!--</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 v-show="!$parent.$parent.$parent.select"><nobr>序号</nobr></th>
            <th ><nobr>地址信息</nobr></th>
            <th v-show="$parent.$parent.$parent.select"><nobr>选择</nobr></th>
            <th v-show="!$parent.$parent.$parent.select"><nobr>区/县</nobr></th>
            <th v-show="!$parent.$parent.$parent.select"><nobr>片区</nobr></th>
            <th v-show="!$parent.$parent.$parent.select"><nobr>街道</nobr></th>
            <th v-show="!$parent.$parent.$parent.select"><nobr>小区(单位)</nobr></th>
            <th v-show="!$parent.$parent.$parent.select"><nobr>楼号</nobr></th>
            <th v-show="!$parent.$parent.$parent.select"><nobr>单元</nobr></th>
            <th v-show="!$parent.$parent.$parent.select"><nobr>楼层</nobr></th>
            <th v-show="!$parent.$parent.$parent.select"><nobr>门牌号</nobr></th>
            <th v-show="!$parent.$parent.$parent.select"><nobr>详细地址</nobr></th>
            <th v-show="!$parent.$parent.$parent.select"><nobr>时间</nobr></th>
            <th v-show="!$parent.$parent.$parent.select"><nobr>操作</nobr></th>
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td style="text-align:center" v-show="!$parent.$parent.$parent.select"><nobr>{{$index + 1}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_pcd}}{{row.f_slice_area}}{{row.f_street}}{{row.f_residential_area}}{{row.f_building}}{{row.f_building_suffix}}{{row.f_unit}}{{row.f_unit_suffix}}{{row.f_floor}}{{row.f_floor_suffix}}{{row.f_room}}{{row.f_room_suffix}}</nobr></td>
            <td style="text-align:center" v-show="$parent.$parent.$parent.select">
              <!-- <button type="button" name="button" class="btn btn-link"
                       @click.stop="$parent.$parent.$parent.modify(row)">修改</button>-->
              <button type="button" name="button" class="button_export"
                      @click.stop="$parent.$parent.$parent.confirmaddress(row)">确认选择</button>
            </td>
            <td v-show="!$parent.$parent.$parent.select" style="text-align:center"><nobr>{{row.f_pcd}}</nobr></td>
            <td v-show="!$parent.$parent.$parent.select" style="text-align:center"><nobr>{{row.f_slice_area}}</nobr></td>
            <td v-show="!$parent.$parent.$parent.select" style="text-align:center"><nobr>{{row.f_street}}</nobr></td>
            <td v-show="!$parent.$parent.$parent.select" style="text-align:center"><nobr>{{row.f_residential_area}}</nobr></td>
            <td v-show="!$parent.$parent.$parent.select" style="text-align:center"><nobr>{{row.f_building}}{{row.f_building_suffix}}</nobr></td>
            <td v-show="!$parent.$parent.$parent.select" style="text-align:center"><nobr>{{row.f_unit}}{{row.f_unit_suffix}}</nobr></td>
            <td v-show="!$parent.$parent.$parent.select" style="text-align:center"><nobr>{{row.f_floor}}{{row.f_floor_suffix}}</nobr></td>
            <td v-show="!$parent.$parent.$parent.select" style="text-align:center"><nobr>{{row.f_room}}{{row.f_room_suffix}}</nobr></td>
            <td v-show="!$parent.$parent.$parent.select" style="text-align:center"><nobr>{{row.f_address}}</nobr></td>
            <td v-show="!$parent.$parent.$parent.select" style="text-align:center"><nobr>{{row.f_create_date}}</nobr></td>
            <td v-show="!$parent.$parent.$parent.select" style="text-align:center">
             <!-- <button type="button" name="button" class="btn btn-link"
                      @click.stop="$parent.$parent.$parent.modify(row)">修改</button>-->
              <button  v-show="!$parent.$parent.$parent.select" 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 {
        criteriaShow: false,
        model: new PagedList('rs/sql/address_getAddress',20,{tablename: `'t_user_address'`,items: `'*'`,orderitem: `'id desc'`}),
        addflag: false,
        addtitle:'',

        // 公司下拉
        curorgid: [this.$login.f.orgid],
        f_orgid: '',

        //文件上传弹框
        showFile: false
      }
    },
    title: '地址列表',
    props:{
      f_filialeids:{
        type: String
      },
      style: {
        type: String,
        default: 'col-sm-2 '
      },
      select: {
        type: Boolean,
        default: false
      },

      company:{
        type: String,
        default: ''
      }
    },
    ready(){
      this.search()
    },
    methods: {
      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)
        }
      },
      importAddress(){
        this.showFile = true
      },
      // 关闭文件上传对话框
      closeFile() {
        this.showFile = false
        // 将选的文件清空
        this.$refs.file.$el.querySelector('input').value = ''
      },
      //修改
      modify(val){
        this.$refs.paged.$refs.grid.selectStore.selected = null
        this.$dispatch('modify',val)
      },
      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 userfiles = await this.$resetpost('rs/sql/address_singleTable', {data: {items: 'f_useraddress_id',tablename: 't_userfiles',condition: `f_useraddress_id='${id}' and f_table_state = '正常'`}},{resolveMsg: null, rejectMsg: null})
          if(service.data.length || userfiles.data.length){
            this.$showAlert('该地址下已有工单或档案,无法删除!','warning',1000)
          }else{
            this.$resetdelete('rs/entity/t_user_address', {id: id}, {resolveMsg: '删除成功', rejectMsg: '删除失败'}).then((res) => {
              this.$dispatch('cancel')
            })
          }
        }
      },
      confirmaddress (row) {
        this.$refs.paged.$refs.cri.search()
        this.$dispatch('confirmaddress',row)
      },
      search () {
        this.$refs.paged.$refs.cri.search()
      },

      selfSearch (args) {

        if (!this.f_orgid) {
          this.getorg([this.$login.f.orgid])
        }
        if (this.select) {
          args.condition =  `${args.condition} and a.f_userinfo_id is null`
        }

        args.condition =  `${args.condition} and a.f_filialeid = '${this.f_filialeids}'`
        this.model.search(args.condition, args.model)
      },

      //清空
      clear(){
        this.$refs.paged.$refs.cri.model = {}
      },
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val)
        this.f_filialeids = val[0]
      }
    },
    events: {
      onFileUpload: async function (file, res) {
        console.log("上传完毕"+JSON.stringify(res))
        let data = {
          filepath: res.f_downloadpath,
          user:this.$login.f
        }
        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)
      }
    },
    watch : {
      'company' (val) {
        this.f_orgid = val
        this.f_filialeids = val
      }
    }

  }
</script>
