<template>
  <div id="unit" class="flex-row">
    <div class="flex" style="flex: 1; width:65%">
      <address-list @select-changed="selected" v-ref:addresslist :f_filialeids.sync="f_filialeids"></address-list>
    </div>
    <div style="width:35%" v-show="useraddressShow">
      <user-address v-ref:useraddress :f_filialeids.sync="f_filialeids" :row="row"></user-address>
    </div>
  </div>
</template>

<script>

  import {HttpResetClass} from 'vue-client'

  let loadStreetAndResGen = async function (self, model, row) {
    self.street = []
    self.residential = []
    self.area = []
    let loc = self.f_filialeids.split('.')
    // 获取区县
    let condition = `f_type='区' and f_locationId = ${loc[loc.length-2]?loc[loc.length-2]:''}`
    let res = await self.$resetpost('rs/sql/address_singleTableOrderBy', {data: {items: 'id,f_name',tablename: 't_address',orderitem: 'f_name' ,condition: condition}},{resolveMsg: null, rejectMsg: '获取区县失败!'})
    res.data.forEach((row)=>{
      self.area.push({label:row.f_name, value:row.id})
  })
    if (row.f_area) {
      self.area.forEach((item) => {
        if (item.label === row.f_area) {
        model.f_area = item.value
        // 用于区分变化
        row.f_area_id = item.value
      }
    })
    }
    if (model.f_area) {
      let streetCondition = `f_type='街道' and f_parentId = ${model.f_area?model.f_area:''}`
      console.log('我是修正是发的获取街道请求')
      let getStreet = await
      self.$resetpost('rs/sql/address_singleTableOrderBy', {
        data: {
          items: 'id,f_name',
          tablename: 't_address',
          orderitem: 'f_name',
          condition: streetCondition
        }
      }, {resolveMsg: null, rejectMsg: '获取街道失败!'})
      getStreet.data.forEach((row) => {
        self.street.push({label: row.f_name, value: row.id})
    })
      self.street.forEach((item) => {
        if(item.label === row.f_street){
        model.f_street = item.value
        row.f_street_id = item.value
      }
    })
    } else {
      model.f_street = ''
    }

    if (model.f_street) {
      let resCondition = `f_type='小区' and f_parentId = ${model.f_street}`
      console.log('我是修正是发的获取小区请求')
      let getResCondition = await self.$resetpost('rs/sql/address_singleTableOrderBy', {data: {items: 'id,f_name',tablename: 't_address',orderitem: 'f_name' ,condition: resCondition}},{resolveMsg: null, rejectMsg: '获取小区失败!'})
      getResCondition.data.forEach((row)=>{
        self.residential.push({label:row.f_name, value:row.id})
    })
      self.residential.forEach((item) => {
        if (item.label === row.f_residential_area) {
        model.f_residential_area = item.value
        row.f_residential_area_id = item.value
      }
    })
    } else {
      model.f_residential_area = ''
    }
  }

  export default {
    title: '地址管理',
    data () {
      return {
        //分公司id串
        f_filialeids: this.$login.f.f_orgids,
        area: [],
        street: [],
        residential: [],
        row: {},
        useraddressShow: false
      }
    },
    props:{
      //控制显示确定按钮
      ishujiao:{
        type: Boolean,
        default: false
      }
    },
    methods:{
      selected(obj){
        this.useraddressShow = true
        this.row = obj.val
        //选择状态变为true
        this.$refs.useraddress.revise = true
        //true则是特殊地址
        if(this.row.f_special==1){
          this.$refs.useraddress.special = true
        }else{
          this.$refs.useraddress.special = false
        }
        this.$refs.useraddress.model.id = this.row.id
        this.$refs.useraddress.model.f_residential_area_id = this.row.f_residential_area_id

        loadStreetAndResGen(this, this.$refs.useraddress.model, this.row)
        this.$refs.useraddress.model.f_building = this.row.f_building
        this.$refs.useraddress.model.f_unit = this.row.f_unit
        this.$refs.useraddress.model.f_floor = this.row.f_floor
        this.$refs.useraddress.model.f_room = this.row.f_room

        this.$refs.useraddress.model.f_address = this.row.f_address.replace(''+(this.row.f_area?this.row.f_area:'')+(this.row.f_street?this.row.f_street:''),'')
        //下拉数据清空
        this.$refs.useraddress.area = this.area
        this.$refs.useraddress.street = this.street
        this.$refs.useraddress.residentialArea = this.residential
      }
    },
    events:{
      'add'(val){
        this.row = {}
        this.useraddressShow = true
        //true为新增特殊地址
        if(val){
          this.$refs.useraddress.special = true
        }else{
          this.$refs.useraddress.special = false
        }
        //选择状态变为false
        this.$refs.useraddress.revise = false
        //获取区/县数据
        this.$refs.useraddress.init()
        //清空输入数据
        this.$refs.useraddress.partclear1()
        this.$refs.useraddress.partclear2()
      },
      'cancel'(){
        this.useraddressShow = false
      },
      'refresh'(){
        //重新查询
        this.$refs.addresslist.$refs.paged.$refs.cri.search()
      },
      //点了确定
      'confirm'(){
        if(this.$refs.useraddress.revise){
          //看看地址是否上传工单
          this.$resetpost('rs/sql/address_singleTable',{data: {items: 'id',tablename: 't_service',condition: `f_addressid='${this.row.id}'`}}, {resolveMsg: null, rejectMsg: null}).then((rs)=>{
            if(rs.data.length>0){
            this.$showAlert('该地址已下发通气单','warning',900)
          }else{
            this.$dispatch('re-ref',this.row)
            console.log("返回事件",this.row)
          }
        })
        }
      }
    }
  }
</script>
<style>
</style>
