<template>
  <div>
    <tabset v-ref:tabs :close="false" style="height: 450px;">
      <tab header='信息修正'>
        <knowledge-essential-info :baseinfo="data.baseinfo" :old-address="oldAddress" @valid="essentialInfo = true"
                             @invalid="essentialInfo = false" v-ref:knowledgeessential></knowledge-essential-info>
      </tab>
    </tabset>
    <div class="form-horizontal auto">
      <div class="form-group">
        <div class="col-sm-offset-9 col-sm-3">
          <button type="button" class="btn btn-success" @click="confirm()"
                  :disabled="!(essentialInfo && meterInfo && addressInfo)">
            保存
          </button>
          <button type="button" class="btn btn-default" @click="cancel()">
            取消
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import Knowledge from './Knowledge'
let getFileGen = async function (self, data) {
  let result = await self.$resetpost('rs/logic/getFilesDetail', data, {
    resolveMsg: null
  })
  self.data = result.data
  // 更新地址
  // 修改附件临时id为表档案id
  self.blodid = self.data.baseinfo.base.f_userinfo_id
  console.log('获取档案信息', self.data)
  self.checkout()
  console.log('附件id', self.blodid)
}
  export default {
    components: {Knowledge},
    title: '综合信息',
    props: ['row', 'areainfo'],
    data () {
      return {
        data: {
          baseinfo: {
            base: {
              f_title: '',
              f_record_type: ''
            }
          },
          addressinfo: {
            f_area: '',
            f_street: '',
            f_residential_area: ''
          },
          meterinfo: [{
            f_user_type: '',
            f_gasproperties: ''
          }],
          devicesinfo: []
        },

        // 子组件验证
        essentialInfo: false,
        meterInfo: false,
        addressInfo: false,

        // 地址相关属性
        params: {
          areas: [],
          f_street: [],
          f_residential_area: []
        },

        blodid: '' // 附件的临时id
      }
    },
    ready () {
      this.blodid = '临时id' + Date.now()
      console.log('ready，，，', this.row, this.areainfo)
      if (this.row && !this.areainfo) { // 修改用户
        this.areaGen(this.row)
      } else if (!this.row && this.areainfo) { // 小区批量建档
        // 将小区信息转化为综合信息的格式
        this.areainfoInUser(this.areainfo)
      }
    },
    watch: {
      'row' (val) {
        console.log('观察row', val)
        if (val) {
          this.areaGen(val)
        } else {
          this.areainfoInUser(this.areainfo)
        }
      },
      'areainfo' (val) {
        console.log('小区观察。。', val)
        if (val) {
          this.$refs.meter.clearMeter()
          this.blodid = '临时id' + Date.now()
          this.areainfoInUser(val)
        }
      }
    },
    methods: {
      confirm () {
        this.data.addressinfo.f_address = this.$refs.useraddress.address
        console.log('档案保存之前，。。。', this.data)
        this.$FileService.fileSave(this.data).then((res) => {
          this.$dispatch('success')
        }).catch((error) => {
          if (error.status === 603) {
            this.$error('重复提交')
            return
          }
          this.$dispatch('error', '档案保存', this.data, error)
        })
      },
      areaGen (data) {
        data.f_user_id = data.f_user_id + ''
        getFileGen(this, data)
      },
      cancel () {
        this.$dispatch('clean', '取消档案修正', 'user-general-info', this.row)
      },
      checkout () {
        // 对收到的综合信息进行校验（主要目的是为了防止下拉框没有默认值）
        // 对综合用户基本信息进行校验

        // 将oldAddress 置为空，
        this.oldAddress = {}
        this.data.baseinfo.base.f_user_state = this.data.baseinfo.base.f_user_state ? [this.data.baseinfo.base.f_user_state] : ['正常']
        this.data.baseinfo.base.f_credentials = this.data.baseinfo.base.f_credentials ? [this.data.baseinfo.base.f_credentials] : ['身份证']
        this.data.baseinfo.base.f_cost_type = this.data.baseinfo.base.f_cost_type ? [this.data.baseinfo.base.f_cost_type] : ['现金缴费']
        this.data.baseinfo.base.f_paper_type = this.data.baseinfo.base.f_paper_type ? [this.data.baseinfo.base.f_paper_type] : ['国税发票']
        this.data.baseinfo.bank.f_bank_name = this.data.baseinfo.bank.f_bank_name ? [this.data.baseinfo.bank.f_bank_name] : ['中国银行']

        if (this.data.baseinfo.base.f_slice_area) {
          this.data.baseinfo.base.f_slice_area = [this.data.baseinfo.base.f_slice_area]
        } else {
          this.data.baseinfo.base.f_slice_area = ''
        }

        // 地址信息
        // loadStreetAndResGen(this)

        // 对表具信息进行校验
        this.data.meterinfo.forEach((row) => {
          if (row.gasbrand) {
            this.meterbrands.forEach((item) => {
              if (item.label === row.gasbrand) {
                row.gasbrand = [item.value]
              }
            })
          } else {
            row.gasbrand = ''
          }
          if (row.gasmodel && (row.gasbrand instanceof Array)) {
            row.gasbrand[0].gasmodel.forEach((item) => {
              if (item.label === row.gasmodel + row.gasmodeltype) {
                row.gasmodel = [item.value]
              }
            })
          } else {
            row.gasmodel = ''
          }
          if (row.f_fire_state === '1') {
            row.f_fire_state1 = true
          } else if (row.f_fire_state === '0') {
            row.f_fire_state1 = false
          }
          row.f_install_date = row.f_install_date ? row.f_install_date : ''
          row.f_gas_date = row.f_gas_date ? row.f_gas_date : ''
          row.f_changetube_date = row.f_changetube_date ? row.f_changetube_date : ''
          row.f_aroundmeter = row.f_aroundmeter ? [row.f_aroundmeter] : ''
          row.f_inputtor = row.f_inputtor ? [row.f_inputtor] : ''
          row.f_position = row.f_position ? [row.f_position] : ''

          row.f_user_type = row.f_user_type ? [row.f_user_type] : ''
          row.f_gasproperties = row.f_gasproperties ? [row.f_gasproperties] : ''
          row.pricetype = row.pricetype ? [row.pricetype] : ''

          if (row.pricename) {
            let names = this.getPricenames(row.f_user_type,
              row.f_gasproperties,
              row.pricetype)
            if (names.length > 0) {
              names.forEach((item) => {
                if (item.label === row.pricename) {
                  row.pricename = [item.value]
                }
              })
            } else {
              trow.pricename = ''
            }
          } else {
            row.pricename = ''
          }

          if (row.f_adjustable) {
            this.adjustables.forEach((item) => {
              if (item.label === row.f_adjustable) {
                row.f_adjustable = [item.value]
              }
            })
          } else {
            row.f_adjustable = ''
          }
        })
        // console.log('5555555555', this.data.meterinfo)
        // 对设备进行校验
        this.data.devicesinfo.forEach((row) => {
          row.f_devices_type = row.f_devices_type ? [row.f_devices_type] : ''
        })
      },
      getPricenames (f_user_type, f_gasproperties, f_price_type) {
        let rs = []
        if (f_user_type.length === 1 && f_gasproperties.length === 1 && f_price_type.length === 1) {
          rs = this.$GetSaleParam.getPrice({
            f_user_type: f_user_type[0],
            f_gasproperties: f_gasproperties[0],
            f_price_type: f_price_type[0],
            filter: this.$login.f.f_fengongsi
          })
        }
        return rs
      },
      // 将小区信息转化为综合信息的格式
      areainfoInUser (areainfo) {
        // this.nextTick(() => {
        //
        // })
        this.data = {
          baseinfo: {
            base: {
              // 附件表临时id
              blodid: this.blodid,
              // 用户姓名
              f_title: baseinfo.f_title,
              // 用户电话
              f_user_phone: areainfo.f_user_phone,
              // 备用电话
              f_rent_phone: areainfo.f_rent_phone,
              f_credentials: areainfo.f_credentials,
              f_cost_type: areainfo.f_cost_type
            },
            bank: {}
          },
          addressinfo: {
            // 控制是否可编辑 小区建档可编辑，地址建档不可编辑
            isEdit: areainfo.isEdit,
            // 区/县
            f_area: areainfo.f_area,
            f_slice_area: areainfo.f_slice_area,
            // 街道
            f_street: areainfo.f_street,
            // 小区
            f_residential_area: areainfo.f_residential_area,
            // 详细地址
            f_address: areainfo.f_address,
            // 单元,楼号,楼层,门牌号
            f_building: areainfo.f_building,
            f_building_suffix: areainfo.f_building_suffix ? areainfo.f_building_suffix : '栋',
            f_unit: areainfo.f_unit,
            f_unit_suffix: areainfo.f_unit_suffix ? areainfo.f_unit_suffix : '单元',
            f_floor: areainfo.f_floor,
            f_floor_suffix: areainfo.f_floor_suffix ? areainfo.f_floor_suffix : '层',
            f_room: areainfo.f_room,
            f_room_suffix: areainfo.f_room_suffix ? areainfo.f_room_suffix : '室',
            // 小区id,有值代表是小区建档
            f_area_id: areainfo.f_area_id
          },
          meterinfo: [{
            // 表号
            f_meternumber: areainfo.f_meternumber,
            f_metertitles: areainfo.f_metertitles,
            // 气表品牌
            gasbrand: areainfo.f_meter_brand,
            // 气表型号
            gasmodel: areainfo.f_meter_style,
            // 气表类型
            gasmodeltype: areainfo.f_type,
            pricetype: areainfo.f_price_type, // 气价类型
            pricename: areainfo.f_price_name, // 气价名称
            f_user_type: areainfo.f_user_type ? areainfo.f_user_type : '民用', // 用户类型
            f_gasproperties: areainfo.f_gasproperties, // 用气性质
            f_inputtor: areainfo.f_inputtor,
            // 安装位置
            f_position: areainfo.f_position,
            // 左右表
            f_aroundmeter: areainfo.f_aroundmeter ? areainfo.f_aroundmeter : '左表',
            f_meter_base: areainfo.f_meter_base ? areainfo.f_meter_base : 0,
            // 通气人
            f_gas_person: areainfo.f_gas_person,
            f_adjustable: areainfo.f_adjustable_name // 调压箱
          }],
          devicesinfo: []
        }
        // 若有id，则表示该地址是更新操作
        if (areainfo.id) {
          this.data.addressinfo.id = areainfo.id
        }
        // 改地址是 只有表没有户信息
        if (areainfo.meter) {
          Object.assign(this.data.meterinfo[0], this.data.meterinfo[0], areainfo.meter)
        }
        if (areainfo.user) {
          Object.assign(this.data.baseinfo.base, this.data.baseinfo.base, areainfo.user)
        }

        this.checkout()
      }
    },
    computed: {
      meterbrands () {
        return this.$GetSaleParam.getGasbrand()
      },
      adjustables () {
        return this.$GetSaleParam.getAdjustable()
      }
    }
  }
</script>
