<template>
  <div>
    <tabset v-ref:tabs :close="false" style="height: 450px;">
      <tab header='营业网点信息修正'>
        <outlets-essential-info :baseinfo="data.baseinfo" :old-address="oldAddress" @valid="essentialInfo = true"
                             @invalid="essentialInfo = false" v-ref:outletsessential></outlets-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)">
            保存
          </button>
          <button type="button" class="btn btn-default" @click="cancel()">
            取消
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    title: '综合信息',
    props: ['row', 'baseinfo'],
    data () {
      return {
        data: {
          baseinfo: {
            base: {
              f_outlets_name: '',
              f_outlets_phone: '',
              f_outlets_hours: '',
              f_outlets_address: '',
              f_outlets_lines: '',
              f_outlets_adcode: '',
              f_outlets_lng: '',
              f_outlets_lat: '',
              f_orgstr: ''
            }
          }
        },

        // 子组件验证
        essentialInfo: false
      }
    },
    ready () {
      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 () {
        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 = ''
        }
        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] : ''
        })
      }
    },
    computed: {
      meterbrands () {
        return this.$GetSaleParam.getGasbrand()
      },
      adjustables () {
        return this.$GetSaleParam.getAdjustable()
      }
    }
  }
</script>
