<template>
  <div class="auto" style="height:80%">
    <validator name='v'>
      <p class="bg-info text-center" style="padding: 8px;">{{usertype ? '特殊地址管理' : '民用地址管理'}}</p>
      <div class="auto select-overspread form-horizontal">
        <div class="form-group" v-if="!usertype&&!model.id">
          <input type="radio" id="true" value="one" v-model="onedata">
          <label for="one">单户</label>
          <input type="radio" id="false" value="more" v-model="onedata">
          <label for="more">多户</label>
        </div>
        <!--单个录入-->
        <div v-if="onedata=='one'" class="row">
          <div class="col-sm-6 form-group "
               :class="[$v.f_pcd_id.required ? 'has-error' : 'has-success']">
            <label class="font_normal_body">省&ensp;市&ensp;区</label>
            <input type="text" style="width:41%" v-show="false" v-model="model.f_pcd_id"
            >
            <v-select :value.sync="model.f_pcd_id" :value-single="true"
                      :options='pcdslist' placeholder='请选择'
                      close-on-select search="true"  @change="pcdChange" :disabled="!usertype">
            </v-select>
          </div>

          <div class="col-sm-6 form-group " v-if="!usertype"
               :class="[$v.f_street_id.required ? 'has-error' : 'has-success']">
            <label class="font_normal_body">街道名称</label>
            <input type="text" style="width:41%" v-show="false" v-model="model.f_street_id"
            >
            <v-select :value.sync="model.f_street_id" :value-single="true"
                      :options='streetslist' placeholder='请选择'
                      close-on-select search="true" :disabled="!usertype">
            </v-select>
          </div>
          <div class="col-sm-6 form-group " v-if="usertype"
               :class="[$v.f_street_id.required ? 'has-error' : 'has-success']">
            <label class="font_normal_body">街道名称</label>
            <input type="text" style="width:41%" v-show="false" v-model="model.f_street_id"
            >
            <v-select :value.sync="model.f_street_id" :value-single="true"
                      :options='streetslist' placeholder='请选择'
                      @change="streetChange"
                      close-on-select search="true" :disabled="!usertype">
            </v-select>
          </div>
          <div  class="col-sm-6 form-group " v-if="!usertype"
                :class="[$v.f_residential_area_id1.required ? 'has-error' : 'has-success']">
            <label class="font_normal_body">小区名称</label>
            <input type="text" style="width:41%" v-show="false" v-model="model.f_residential_area_id"
                   v-validate:f_residential_area_id1='{required: true }'>
            <v-select :value.sync="model.f_residential_area_id" :value-single="true"
                      :options='areaslist' placeholder='请选择'
                      @change="areaChange"
                      close-on-select search="true">
            </v-select>
          </div>

          <div  class="col-sm-6 form-group " v-if="usertype">
            <label class="font_normal_body">小区名称</label>
            <v-select :value.sync="model.f_residential_area_id" :value-single="true"
                      :options='areaslist' placeholder='请选择'
                      @change="areaChange"
                      close-on-select search="true">
            </v-select>
          </div>

          <div class="col-sm-6 form-group " :class="[$v.slice1.required ? 'has-error' : 'has-success']">
            <label class="font_normal_body">片&emsp;&emsp;区</label>
            <input type="text" style="width:60%" class="input_search" v-show="false" v-model="$refs.slice.selectedItems"
                   v-validate:slice1='{required: true }'>
            <v-select :value.sync="model.slice_area" v-model="model.slice_area"
                      :options='sliceArea' placeholder='片区/管理站' filer-key="name"
                      close-on-select v-ref:slice>
            </v-select>
          </div>
          <div v-if="!usertype" class="col-sm-6 form-group "
               :class="[$v.f_building.required ? 'has-error' : 'has-success']">
            <label for="f_address" class="font_normal_body">楼&emsp;&emsp;号</label>
            <input type="text" style="width:40%" v-model="model.f_building" class="input_search"
                   v-validate:f_building='{required: true }'
                   placeholder="楼号"/>
            <input type="text" style="width:20%" v-model="model.f_building_suffix" class="input_search"
            />

          </div>
          <div v-if="!usertype" class="col-sm-6 form-group "
               :class="[$v.f_unit.required ? 'has-error' : 'has-success']">
            <label for="f_address" class="font_normal_body">单&emsp;&emsp;元</label>
            <input type="text" style="width:40%" v-model="model.f_unit" class="input_search" placeholder="单元"  v-validate:f_unit='{required: true }'/>
            <input type="text" style="width:20%" v-model="model.f_unit_suffix" class="input_search"
            />
          </div>
          <div v-if="!usertype" class="col-sm-6 form-group "
               :class="[$v.f_floor.required ? 'has-error' : 'has-success']">
            <label for="f_address" class="font_normal_body">楼&emsp;&emsp;层</label>
            <input type="text" style="width:40%" v-model="model.f_floor" class="input_search"  v-validate:f_floor='{required: true }'
                   placeholder="楼层" />
            <input type="text" style="width:20%" v-model="model.f_floor_suffix" class="input_search"
            />
          </div>
          <div v-if="!usertype" class="col-sm-6 form-group "
               :class="[$v.f_room.required ? 'has-error' : 'has-success']">
            <label for="f_address" class="font_normal_body">门&ensp;牌&ensp;号</label>
            <input type="text" style="width:40%" v-model="model.f_room" class="input_search" placeholder="门牌号"
                   v-validate:f_room='{required: true }'/>
            <input type="text" style="width:20%" v-model="model.f_room_suffix" class="input_search"
            />
          </div>
          <div class="col-sm-6 form-group">
            <label class="font_normal_body">地址状态</label>
            <v-select :value.sync="model.f_address_state" :value-single="true"
                      class="select_list select"
                      condition="f_address_state = '{}'"
                      :options='addresstate' placeholder='地址状态'
                      close-on-select>
            </v-select>
          </div>
          <div v-if="usertype" class="col-sm-12 form-group "
               :class="[$v.f_address.required ? 'has-error' : 'has-success']">
            <label for="f_address" class="font_normal_body">详细地址</label>
            <input type="text" style="width:80%" v-model="model.f_address" class="input_search" placeholder="详细地址"
                   v-validate:f_address='{required: true }'/>
          </div>
          <div class="col-sm-12 form-group">
            <label  class="font_normal_body " >备&emsp;&emsp;注</label>
            <textarea class="input_textarea" rows="3" style="margin-top: 25px; width:80%;height: auto;" v-model="model.f_comments"></textarea>
          </div>
          <div style="text-align:right;margin-top:40px;margin-right:50px;" class="col-sm-12">
            <button class="button_search button_spacing" @click="confirm()" :disabled='!$v.valid'>保存</button>
            <button class="button_clear button_spacing" @click="cancel()">取消</button>
          </div>

        </div>
        <!--批量录入-->
        <div v-if="onedata=='more'" class="row">
          <div class="col-sm-6 form-group "
               :class="[$v.f_pcd_id.required ? 'has-error' : 'has-success']">
            <label class="font_normal_body">省&ensp;市&ensp;区</label>
            <input type="text" style="width:60%" v-show="false" v-model="model.f_pcd_id"
            >
            <v-select :value.sync="model.f_pcd_id" :value-single="true"
                      :options='pcdslist' placeholder='请选择'
                      close-on-select search="true" @change="pcdChange" :disabled="!usertype">
            </v-select>
          </div>
          <div class="col-sm-6 form-group "
               :class="[$v.f_street_id.required ? 'has-error' : 'has-success']">
            <label class="font_normal_body">街&emsp;&emsp;道</label>
            <input type="text" style="width:60%" v-show="false" v-model="model.f_street_id"
            >
            <v-select :value.sync="model.f_street_id" :value-single="true"
                      :options='streetslist' placeholder='请选择'
                      close-on-select search="true" :disabled="!usertype">
            </v-select>
          </div>
          <div class="col-sm-6 form-group "
               :class="[$v.f_residential_area_id.required ? 'has-error' : 'has-success']">
            <label class="font_normal_body">小区名称</label>
            <input type="text" style="width:60%" v-show="false" v-model="model.f_residential_area_id"
                   v-validate:f_residential_area_id='{required: true }'>
            <v-select :value.sync="model.f_residential_area_id" :value-single="true"
                      @change="areaChange"
                      :options='areaslist' placeholder='请选择'
                      close-on-select search="true">
            </v-select>
          </div>
          <div class="col-sm-6 form-group " :class="[$v.slice.required ? 'has-error' : 'has-success']">
            <label class="font_normal_body">片&emsp;&emsp;区</label>
            <input type="text" style="width:60%" class="input_search" v-show="false" v-model="$refs.slice.selectedItems"
                   v-validate:slice='{required: true }'>
            <v-select :value.sync="model.slice_area" v-model="model.slice_area"
                      :options='sliceArea' placeholder='片区/管理站' filer-key="name"
                      close-on-select v-ref:slice>
            </v-select>
          </div>

          <div class="col-sm-6 form-group "
               :class="[$v.f_building_start.integernum || $v.f_building_start.dctest ?'has-error' : 'has-success']">
            <label for="f_address" class="font_normal_body">起始楼号</label>
            <input type="text" style="width:15%" placeholder="前缀" v-model="model.f_building_prefix" class="input_search"/>
            <input type="text" style="width:25%"
                   v-validate:f_building_start='{integernum: true ,dctest: [model.f_building_end, "<=" ]}'
                   v-model="model.f_building_start" class="input_search" placeholder="楼号"/>
            <input type="text" style="width:17%" v-model="model.f_building_suffix" class="input_search"/>
          </div>
          <div class="col-sm-6 form-group "
               :class="[$v.f_building_end.integernum ||$v.f_building_end.dctest ? 'has-error' : 'has-success']">
            <label for="f_address" class="font_normal_body">截止楼号</label>
            <input type="text" style="width:15%" placeholder="前缀" v-model="model.f_building_prefix" class="input_search"/>
            <input type="text" style="width:25%"
                   v-validate:f_building_end='{integernum: true ,dctest: [model.f_building_start, ">=" ]}'
                   v-model="model.f_building_end" class="input_search" placeholder="楼号"/>
            <input type="text" style="width:17%" v-model="model.f_building_suffix" class="input_search"
            />
          </div>
          <div class="col-sm-6 form-group  "
               :class="[$v.f_unit_start.integernum || $v.f_unit_start.dctest ? 'has-error' : 'has-success']">
            <label for="f_address" class="font_normal_body">起始单元</label>
            <input type="text" style="width:15%" placeholder="前缀" v-model="model.f_unit_prefix" class="input_search" />
            <input type="text" style="width:25%"
                   v-validate:f_unit_start='{integernum: true ,dctest: [model.f_unit_end, "<=" ]}'
                   v-model="model.f_unit_start" class="input_search" placeholder="单元"/>
            <input type="text" style="width:17%" v-model="model.f_unit_suffix" class="input_search" />
          </div>
          <div class="col-sm-6 form-group  "
               :class="[$v.f_unit_end.integernum ||$v.f_unit_end.dctest ? 'has-error' : 'has-success']">
            <label for="f_address" class="font_normal_body">截止单元</label>
            <input type="text" style="width:15%" placeholder="前缀" v-model="model.f_unit_prefix" class="input_search" />
            <input type="text" style="width:25%"
                   v-validate:f_unit_end='{integernum: true ,dctest: [model.f_unit_start, ">="]}'
                   v-model="model.f_unit_end" class="input_search" placeholder="单元"/>
            <input type="text" style="width:17%" v-model="model.f_unit_suffix" class="input_search"/>
          </div>
          <div class="col-sm-6 form-group  "
               :class="[$v.f_floor_start.integernum || $v.f_floor_start.dctest  ? 'has-error' : 'has-success']">
            <label for="f_address" class="font_normal_body">起始楼层</label>
            <input type="text" style="width:15%" placeholder="前缀" v-model="model.f_floor_prefix" class="input_search"/>
            <input type="text" style="width:25%"
                   v-validate:f_floor_start='{integernum: true,dctest: [model.f_floor_end, "<=" ] }'
                   v-model="model.f_floor_start" class="input_search" placeholder="楼层"/>
            <input type="text" style="width:17%" v-model="model.f_floor_suffix" class="input_search"/>
          </div>
          <div class="col-sm-6 form-group  "
               :class="[$v.f_floor_end.integernum ||$v.f_floor_end.dctest ? 'has-error' : 'has-success']">
            <label for="f_address" class="font_normal_body">截止楼层</label>
            <input type="text" style="width:15%" placeholder="前缀" v-model="model.f_floor_prefix" class="input_search"/>
            <input type="text" style="width:25%" v-model="model.f_floor_end"
                   v-validate:f_floor_end='{integernum: true,dctest: [model.f_floor_start, ">=" ] }'
                   class="input_search" placeholder="楼层"/>
            <input type="text" style="width:17%" v-model="model.f_floor_suffix" class="input_search"/>
          </div>
          <div class="col-sm-6 form-group "
               :class="[$v.f_room_start.integernum || $v.f_room_start.dctest ? 'has-error' : 'has-success']">
            <label for="f_address" class="font_normal_body">起始门牌</label>
            <input type="text" style="width:15%" placeholder="前缀" v-model="model.f_room_prefix" class="input_search"/>
            <input type="text" style="width:25%" v-model="model.f_room_start"
                   class="input_search" placeholder="门牌号"
                   v-validate:f_room_start='{integernum: true ,dctest: [model.f_room_end, "<=" ]}'
            />
            <input type="text" style="width:17%" v-model="model.f_room_suffix" class="input_search"/>
          </div>
          <div class="col-sm-6 form-group "
               :class="[$v.f_room_end.integernum ||$v.f_room_end.dctest ? 'has-error' : 'has-success']">
            <label for="f_address" class="font_normal_body">截止门牌</label>
            <input type="text" style="width:15%" placeholder="前缀" v-model="model.f_room_prefix" class="input_search"/>
            <input type="text" style="width:25%" v-model="model.f_room_end" class="input_search" placeholder="门牌号"
                   v-validate:f_room_end='{integernum: true ,dctest: [model.f_room_start, ">=" ]}'/>
            <input type="text" style="width:17%" v-model="model.f_room_suffix" class="input_search"/>
          </div>
          <div class="col-sm-6 form-group">
            <label class="font_normal_body">地址状态</label>
            <v-select :value.sync="model.f_address_state" :value-single="true"
                      class="select_list select"
                      condition="f_address_state = '{}'"
                      :options='addresstate' placeholder='地址状态'
                      close-on-select>
            </v-select>
          </div>
          <div class="col-sm-10 form-group ">
            <p class="navbar-text" style="margin-left: 10%">说明: 层数输入1,门牌号输入001。产生的最终门牌号显示1001</p>
          </div>
          <div  class="col-sm-12 form-group">
            <label  class="font_normal_body " style="margin-top:-20px">备&emsp;&emsp;注</label>
            <textarea class="input_textarea" rows="3" style="width:80%;height: auto;" v-model="model.f_comments"></textarea>
          </div>
          <div class="row">
            <div style="text-align:right;margin-top:20px;margin-right:50px;" class="col-sm-12">
              <button class="button_search button_spacing" @click="confirmall()" :disabled='!$v.valid'>保存</button>
              <button class="button_clear button_spacing" @click="cancel()">取消</button>
            </div>
          </div>
        </div>
      </div>
    </validator>
  </div>
</template>

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

  let getAreaConfig = async function (self) {
    // 获取气价里面的配置
    await self.$getConfig(self, 'UserAddress')
    console.log('原地址配置', self.config)
    console.log('获取地址配置', self.config)
    Object.assign(self.model, self.config)
    self.initdata()
  }

  export default {
    title: '用户地址添加',
    data () {
      return {
        // 初始化省市区数据
        pcdslist: [],
        // 初始化街道数据
        streetslist: [],
        // 初始化街道数据
        areaslist: [],
        config: {
          f_building_suffix: '号楼',
          f_unit_suffix: '单元',
          f_floor_suffix: '层',
          f_room_suffix: '室'
        },
        model: {
          f_pcd_id: '',
          f_street_id: '',
          f_comments: '',
          f_residential_area_id: '',
          f_pcd: '',
          f_street: '',
          f_residential_area: '',
          f_slice_area: '',
          f_building: '',
          f_building_start: '',
          f_building_end: '',
          f_building_suffix: '',
          f_unit: '',
          f_unit_start: '',
          f_unit_end: '',
          f_unit_suffix: '',
          f_floor: '',
          f_floor_start: '',
          f_floor_end: '',
          f_floor_suffix: '',
          f_room: '',
          f_room_start: '',
          f_room_end: '',
          f_room_suffix: '',
          // 详细地址
          f_address: '',
          // 单位名称
          f_company: '',
          // 单位地址
          f_company_address: ''
        },
        // 判读是否为单个数据录入
        onedata: 'one',
        // 选中所有地址
        addresslist: [],
        usertype: false,

        // 公司信息
        curorgid: [this.$login.f.orgid],

        f_orgid: '',

        sliceArea: []
      }
    },
    props: ['f_filialeids', 'row', 'operation', 'usertype'],
    ready () {
      getAreaConfig(this)
    },
    methods: {
      async initdata () {
        this.model.f_address_state = '已通气'
        this.initAreas(this.f_filialeids)
        // 添加特殊地址选省市区

        this.initpcds(` f_orgid = '${this.f_filialeids}'`)
        this.initstreets(` f_orgid = '${this.f_filialeids}' `)
        this.initareas(` f_orgid = '${this.f_filialeids}'`)
      },

      // 初始化片区
      async initAreas (val) {
        if (val) {
          let getAllArea = await this.$resetpost('/rs/search', {
            source: 'this.getParentByType($organization$).getAllChildrens().where(row.getType() == $zone$)',
            userid: this.$login.f.id
          }, {resolveMsg: null, rejectMsg: '获取片区出错！！！'})

          let arr = getAllArea.data.filter((res) => {
            return res.parentid == val
          })
          console.log('过滤之后的片区', arr)
          this.sliceArea = []
          arr.forEach((res) => {
            this.sliceArea.push({label: res.name, value: {name: res.name, code: res.number}})
          })
        }
      },
      // 初始化省市区,添加街道
      async initpcds (pconditon) {
        this.pcdslist = []
        let HttpReset = new HttpResetClass()
        let req = await HttpReset.load('POST', 'rs/sql/address_singleTableOrderBy?pageNo=1&pageSize=9999999', {
          data: {
            items: '*',
            tablename: 't_pcd',
            orderitem: 'id',
            condition: pconditon
          }
        }, {resolveMsg: null, rejectMsg: '获取地址失败!'})
        let redata = []
        req.data.forEach((row, n) => {
          redata[n] = {
            label: row.f_pcd,
            value: row.id,
            data: row,
            id: row.id
          }
        })
        this.pcdslist = redata
      },
      // 初始化街道 添加小区
      async initstreets (pconditon) {
        this.streetslist = []
        let HttpReset = new HttpResetClass()
        let req = await HttpReset.load('POST', 'rs/sql/address_singleTableOrderBy', {
          data: {
            items: '*',
            tablename: 't_street',
            orderitem: 'id',
            condition: pconditon
          }
        }, {resolveMsg: null, rejectMsg: '获取地址失败!'})
        let redata = []
        req.data.forEach((row, n) => {
          redata[n] = {
            label: row.f_street,
            value: row.id,
            data: row,
            id: row.id
          }
        })
        this.streetslist = redata
      },
      // 初始化小区添加小区
      async initareas (pconditon) {
        // if(this.usertype){
        //   pconditon=pconditon+` and  f_special='1' `
        // }
        this.areaslist = []
        let HttpReset = new HttpResetClass()
        let req = await HttpReset.load('POST', 'rs/sql/address_singleTableOrderBy?pageNo=1&pageSize=9999999', {
          data: {
            items: '*',
            tablename: 't_area',
            orderitem: 'id',
            condition: pconditon
          }
        }, {resolveMsg: null, rejectMsg: '获取地址失败!'})
        let redata = []
        req.data.forEach((row) => {
          redata.push({
            label: row.f_residential_area,
            value: row.id,
            data: row,
            id: row.id
          })
        })
        this.areaslist = redata
      },
      // 省/市/区变化
      async pcdChange (val) {
        // 只有添加特殊地址时才级联
        if (!this.usertype) {
          return
        }
        console.log('省/市/区变化', val)
        if (val) {
          // 那就把[小区，街道]重新组织一下
          await this.initstreets(`  f_pcd_id ='${val}' `)
          if (this.model.f_street_id) {
            if (this.findbyid(this.streetslist, this.model.f_street_id)) {
              let pcd_id = this.findbyid(this.streetslist, this.model.f_street_id).f_pcd_id
              if (pcd_id != val) {
                this.model.f_street_id = ''
              }
            } else {
              this.model.f_street_id = ''
            }

            // 那就把[小区，街道]重新组织一下
            if (this.model.f_street_id) {
              await this.initareas(` f_street_id ='${this.model.f_street_id}' `)
              if (this.model.f_residential_area_id) {
                if (this.findbyid(this.areaslist, this.model.f_residential_area_id)) {
                  let pcd_id = this.findbyid(this.areaslist, this.model.f_residential_area_id).f_pcd_id
                  if (pcd_id != val) {
                    this.model.f_residential_area_id = ''
                  }
                } else {
                  this.model.f_residential_area_id = ''
                }
              }
            }
          }
        }
        this.$resetValidation()
      },
      // 小区变化
      areaChange (val) {
        console.log('小区变化', val)
        // 选择小区后级联出省市区和街道
        let selectArea // 选中的小区数据
        for (let row of this.areaslist) {
          if (val == row.value) {
            selectArea = row.data
            break
          }
        }
        if (selectArea) {
          this.model.f_street_id = selectArea.f_street_id
          this.model.f_pcd_id = selectArea.f_pcd_id
          this.initstreets(` f_filialeid = '${this.f_filialeids}' and id ='${selectArea.f_street_id}' `)
          this.initpcds(` f_filialeid = '${this.f_filialeids}' and id ='${selectArea.f_pcd_id}' `)
          this.model.slice_area = [{name: selectArea.f_slice_area, code: selectArea.f_area_code}]
          // 拼接地址
          this.model.f_address = `${selectArea.f_street}${selectArea.f_residential_area}`
        }
        this.$nextTick(() => {
          this.$resetValidation()
        })
      },
      // 街道变化
      async streetChange (val) {
        console.log('街道变化', val)
        if (this.streetslist[0]) {
          var street = ''
          this.streetslist.forEach((item) => {
            if (item.id === val) {
              street = item.label
            }
          })
          this.model.f_address = street
        }
        if (val) {
          // 那就把[小区]重新组织一下
          await this.initareas(` f_street_id ='${val}' `)
          if (this.model.f_residential_area_id) {
            if (this.findbyid(this.areaslist, this.model.f_residential_area_id)) {
              let street_id = this.findbyid(this.areaslist, this.model.f_residential_area_id).f_street_id
              if (street_id != val) {
                this.model.f_residential_area_id = ''
              }
            } else {
              this.model.f_residential_area_id = ''
            }
          }
        }
        this.$resetValidation()
      },

      getorg (val) {
        this.f_orgid = val[0]
      },

      // 处理批量地址信息
      dealaddlist () {
        try {
          var resultlist = []
          for (var i = parseInt(this.model.f_building_start); i <= parseInt(this.model.f_building_end); i++) {
            for (var j = parseInt(this.model.f_unit_start); j <= parseInt(this.model.f_unit_end); j++) {
              for (var m = parseInt(this.model.f_floor_start); m <= parseInt(this.model.f_floor_end); m++) {
                for (var n = parseInt(this.model.f_room_start); n <= parseInt(this.model.f_room_end); n++) {
                  // 预备一个空的json
                  console.log('批量', this.model)
                  let data = Object.assign({}, this.model)
                  data.f_create_person = this.$login.f.name
                  data.f_building = data.f_building_prefix + i
                  data.f_unit = data.f_unit_prefix + j
                  data.f_floor = data.f_floor_prefix + m
                  // 目前房号都按两位处理 如： 101室 201室 110室
                  if ((n + '').length == 1) {
                    data.f_room = data.f_room_prefix + m + '0' + n
                  } else {
                    data.f_room = data.f_room_prefix + m + '' + n
                  }
                  data.f_filialeid = this.f_filialeids
                  data.f_operator = this.$login.f.name
                  data.f_operatorid = this.$login.f.id
                  data.f_orgid = this.$login.f.orgid
                  data.f_orgname = this.$login.f.orgs
                  data.f_depid = this.$login.f.depids
                  data.f_depname = this.$login.f.deps
                  data.f_pcd = this.findbyid(this.pcdslist, this.model.f_pcd_id).f_pcd
                  data.f_street = this.findbyid(this.streetslist, this.model.f_street_id).f_street
                  data.f_residential_area = this.findbyid(this.areaslist, this.model.f_residential_area_id).f_residential_area
                  data.f_address = data.f_street +
                  data.f_residential_area +
                    data.f_building + data.f_building_suffix +
                  data.f_unit + data.f_unit_suffix +
                  data.f_floor + data.f_floor_suffix +
                  data.f_room + data.f_room_suffix
                  if (this.model.slice_area) {
                    if (this.model.slice_area.length > 0) {
                      data.f_slice_area = this.model.slice_area[0].name
                      data.f_area_code = this.model.slice_area[0].code
                    }
                  }
                  resultlist.push(data)
                }
              }
            }
          }
          this.addresslist = resultlist
        } catch (e) {
          console.log(e)
          this.$showAlert('输出数据有误，请检查', 'warning', 2000)
        }
      },
      // 保存多个
      async confirmall () {
        this.dealaddlist()
        if (this.addresslist.length > 0) {
          let msg = {
            resolveMsg: '地址保存成功',
            rejectMsg: '地址保存失败'
          }
          this.$showMessage('确定要批量添加' + this.addresslist.length + '户地址吗？，如有已经存在的，将会重复添加！！！', ['confirm', 'cancel']).then((res) => {
            if (res === 'confirm') {
              this.$resetpost('rs/logic/address_adduserlist', {data: {addlist: this.addresslist}}, msg).then((req) => {
                this.cleardara()
                this.$dispatch('confirm')
              })
            }
          })
        }
      },
      // 保存
      async confirm () {
        if (this.onedata == 'one') {
          await this.saveonedata()
        }
        this.cleardara()
        this.$dispatch('confirm')
      },
      // 保存一户信息
      async saveonedata () {
        this.model.f_create_person = this.$login.f.name
        this.model.f_filialeid = this.f_filialeids
        this.model.f_operator = this.$login.f.name
        this.model.f_operatorid = this.$login.f.id
        this.model.f_orgid = this.$login.f.orgid
        this.model.f_orgname = this.$login.f.orgs
        this.model.f_depid = this.$login.f.depids
        this.model.f_depname = this.$login.f.deps
        if (this.model.slice_area) {
          if (this.model.slice_area.length > 0) {
            this.model.f_slice_area = this.model.slice_area[0].name
            this.model.f_area_code = this.model.slice_area[0].code
          }
        }
        if (this.usertype) {
          // 非民用
          this.model.f_special = '1'
          this.model.f_building_suffix = ''
          this.model.f_unit_suffix = ''
          this.model.f_floor_suffix = ''
          this.model.f_room_suffix = ''
          this.model.f_pcd = this.findbyid(this.pcdslist, this.model.f_pcd_id).f_pcd
          this.model.f_street = this.findbyid(this.streetslist, this.model.f_street_id).f_street
          this.model.f_residential_area = this.findbyid(this.areaslist, this.model.f_residential_area_id) ? this.findbyid(this.areaslist, this.model.f_residential_area_id).f_residential_area : ''
        } else {
          // 民用
          this.model.f_pcd = this.findbyid(this.pcdslist, this.model.f_pcd_id).f_pcd
          this.model.f_street = this.findbyid(this.streetslist, this.model.f_street_id).f_street
          this.model.f_residential_area = this.findbyid(this.areaslist, this.model.f_residential_area_id).f_residential_area
          this.model.f_address = this.model.f_street + this.model.f_residential_area + (this.model.f_building ? this.model.f_building + this.model.f_building_suffix : '') + (this.model.f_unit ? this.model.f_unit + this.model.f_unit_suffix : '') + (this.model.f_floor ? this.model.f_floor + this.model.f_floor_suffix : '') + this.model.f_room + this.model.f_room_suffix
        }
        await this.$resetpost('rs/logic/address_updateuseraddress', this.model)
      },
      cancel () {
        this.cleardara()
        this.$dispatch('cancel')
      },
      cleardara () {
        this.model = {
          f_pcd_id: '',
          f_street_id: '',
          f_residential_area_id: '',
          f_pcd: '',
          f_comments: '',
          f_street: '',
          f_residential_area: '',
          f_slice_area: '',
          f_building: '',
          f_building_start: '',
          f_building_end: '',
          f_building_prefix: '',
          f_building_suffix: this.config.f_building_suffix,
          f_unit: '',
          f_unit_start: '',
          f_unit_end: '',
          f_unit_prefix: '',
          f_unit_suffix: this.config.f_unit_suffix,
          f_floor: '',
          f_floor_start: '',
          f_floor_end: '',
          f_floor_prefix: '',
          f_floor_suffix: this.config.f_floor_suffix,
          f_room: '',
          f_room_start: '',
          f_room_end: '',
          f_room_prefix: '',
          f_room_suffix: this.config.f_room_suffix,
          // 详细地址
          f_address: '',
          // 单位名称
          f_company: '',
          // 单位地址
          f_company_address: ''
        }
        // this.pcdslist = []
        // this.streetslist = []
        // this.areaslist = []
      },

      // 根据名字找数据
      findbyid (list, name) {
        var result
        list.forEach((row, n) => {
          if (name == row.value) {
            result = row.data
          }
        })
        return result
      }
    },
    watch: {
      // 分公司变化
      'f_filialeids' () {
        if (this.model.f_filialeid) {
          if (this.model.f_filialeid != this.f_filialeids) {
            this.$dispatch('cancel')
          }
        }
        this.cleardara()
        this.initdata()
      },
      'row' () {
        this.onedata = 'one'
      }
    },
    computed: {
      // 地址状态下拉框
      addresstate () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('地址状态')]
      }
    }
  }
</script>
