<template>
  <!--<div class="auto">-->
    <validator name='v'  @valid="onValid()"  @invalid="onInvalid()">
        <form novalidate class="form-horizontal">
          <div class="row auto" >

            <div class="col-sm-4 form-group" :class="[$v.metertype.required ? 'has-error' : '']">
              <label for="f_meter_type" class="font_normal_body">新表类型</label>
              <input type="text" class="input_search" style="width: 60%" v-show="false" v-model="$refs.metertype.selectedItems" v-validate:metertype='{required: true }'>
              <v-select :value.sync="metermessage.f_meter_type"
                        v-model="metermessage.f_meter_type"
                        :options='meterType' placeholder='气表类型'
                        close-on-select v-ref:metertype style="width: 60%"></v-select>
            </div>

            <div class="col-sm-4 form-group" :class="[$v.brand.required ? 'has-error' : '']">
              <label for="gasbrand" class="font_normal_body">新表品牌</label>
              <input type="text" class="input_search" style="width:60%" v-show="false" v-model="$refs.brand.selectedItems" v-validate:brand='{required: true }'>
              <v-select :value.sync="metermessage.gasbrand" :options='getMeterBrands(metermessage.f_meter_type)' @change='brandChange' placeholder='气表品牌' close-on-select
                        search v-model='metermessage.gasbrand' v-ref:brand></v-select>
            </div>
            <div class="col-sm-4 form-group" :class="[$v.model.required ? 'has-error' : '']">
              <label for="gasmodel" class="font_normal_body">新表型号</label>
              <input type="text" class="input_search" style="width:60%" v-show="false" v-model="$refs.model.selectedItems"  v-validate:model='{required: true }'>
              <v-select :value.sync="metermessage.gasmodel" :options='metermessage.gasbrand[0] ? metermessage.gasbrand[0].gasmodel : [] ' placeholder='气表型号' close-on-select
                        v-model='metermessage.gasmodel' :disabled= 'metermessage.gasbrand.length === 0 ' @change='modelChange' v-ref:model></v-select>
            </div>

          </div>
          <div class="row auto" >
            <div class="col-sm-4 form-group" :class="[$v.meternumber.required ? 'has-error' : '']">
              <label for="f_meternumber" class="font_normal_body">新&ensp;表&ensp;号</label>
              <input type="text" class="input_search" style="width:60%" class="input_search"
                     @blur="meternumberValidate()"  v-model="metermessage.f_meternumber" v-validate:meternumber='{required: true }' placeholder='表号' >
            </div>
            <div class="col-sm-4 form-group" :class="[$v.f_metertitles.required ? 'has-error' : '']">
              <label for="f_metertitles" class="font_normal_body">新表封号</label>
              <input type="text" class="input_search" style="width:60%" class="input_search"  v-model="metermessage.f_metertitles" placeholder='表封号'>
            </div>
            <div class="col-sm-4 form-group" :class="[$v.f_meter_base.required ? 'has-error' : '']" title="参数名称：【初始表底数最大值】">
              <label for="f_meter_base" class="font_normal_body">新表底数</label>
              <input type="text" class="input_search" style="width:60%" class="input_search" v-validate:f_meter_base='{required: true }' @blur='meterbase()' v-model="metermessage.f_meter_base" placeholder='表底数' >
            </div>
          </div>

        </form>
    </validator>
  <!--</div>-->
</template>

<script>
  import {HttpResetClass} from "vue-client";

  /**
   *公共组件
   *获取气表信息
   */
  export default {
    data () {
      return {
        isdecimal: '否',
        imgsrc: '/images/mainicon/biaopan.png',
        imgmeter: '/images/mainicon/addmeter.png',
        f_meter_base_max: this.$appdata.getSingleValue('初始表底数最大值') ? this.$appdata.getSingleValue('初始表底数最大值') : 10000,
        meterType: this.$appdata.getParam('气表类型')
      }
    },
    props: ['f_userfiles_id', 'metermessage'],
    methods: {
      onValid () {
        this.$dispatch('valid')
      },
      onInvalid () {
        this.$dispatch('invalid')
      },
      brandChange (val) {
        this.isdecimal = val.length > 0 ? val[0].f_isdecimal : '否'
        this.meternumberValidate()
      },
      modelChange (val) {
        this.$dispatch('gasmodel', this.metermessage.gasbrand)
      },
      resetValidate () {
        this.$resetValidation()
      },
      meterbase () {
        if (this.isdecimal === '是') {
          this.metermessage.f_meter_base = (this.metermessage.f_meter_base - 0).toFixed(2)
        } else {
          this.metermessage.f_meter_base = (this.metermessage.f_meter_base - 0).toFixed(0)
        }
        this.f_meter_base_max -= 0
        if (this.metermessage.f_meter_base > this.f_meter_base_max) {
          this.$showAlert(`新表底数可录入的最大值为: ${this.f_meter_base_max}, 请核实后重新录入!!!`, 'warning', 3000)
          this.metermessage.f_meter_base = ''
        }
      },
      getMeterBrands (meterType) {
        let res = []
        if (meterType.length > 0) {
          this.meterbrands.forEach((item) => {
            if (item.value.f_meter_type === meterType[0]) {
              res.push(item)
            }
          })
        }
        return res
      },
      async meternumberValidate () {
        let http1 = new HttpResetClass()
        let res1 = await http1.load('POST', '/rs/sql/getBrandAndType', {data: {condition: ` mi.f_meternumber = '${this.metermessage.f_meternumber}'`}}, {resolveMsg: null, rejectMsg: '获取默认气表品牌型号失败！！'})
        if (res1.data.length > 0) {


          // 已经选择了气表 则不再替换
          if (this.metermessage.f_meter_type <= 0 ){
            this.metermessage.f_meter_type = [res1.data[0].f_meter_classify]
          }
            // 替换表号自带的气表品牌和型号
          let meterbrands = this.meterbrands
          for (let row of meterbrands) {
            if (row.label == res1.data[0].f_meter_brand) {
              // 已经选择了气表品牌则不再替换
              if (this.metermessage.gasbrand.length <= 0) this.metermessage.gasbrand = [row.value]
              // 替换气表型号
              if (res1.data[0].f_meter_style) {
                for (let model of row.value.gasmodel) {
                  if (model.label == res1.data[0].f_meter_style) {
                    // 已经选择了气表型号则不再替换
                    if (this.metermessage.gasmodel.length <= 0) this.metermessage.gasmodel = [model.value]
                    continue
                  }
                }
              }
              continue
            }
          }
        }
        if (this.metermessage.f_meternumber && this.metermessage.gasbrand.length > 0) {
          // 表号位数验证
          let meternumberLen = this.metermessage.gasbrand[0].f_meternumber_len
          if (meternumberLen) {
            if (this.metermessage.f_meternumber.length != meternumberLen) {
              this.metermessage.f_meternumber = ''
              this.$showAlert(`表号位数不正确, 当前气表品牌表号位数是: ${meternumberLen}`, 'warning', 3000)
              return
            }
          }
          // 表计信息验证
          if (this.metermessage.gasbrand[0].f_meterverification == '是') {
            if (res1.data.length <= 0) {
              this.$showAlert(`当前表号: ${this.metermessage.f_meternumber}在表计信息中无记录, 请核实!!!`, 'warning', 3000)
              this.metermessage.f_meternumber = ''
              return
            }
          }
          if (this.metermessage.gasbrand[0].f_meter_type === '物联网表') {
            let param = {
              f_userfiles_id: this.f_userfiles_id,
              f_meternumber: this.metermessage.f_meternumber,
              f_gasbrand_id: this.metermessage.gasbrand[0].id
            }

            let res = await this.$resetpost('rs/logic/meternumberValidate', param, {
              resolveMsg: null,
              rejectMsg: '表号验证失败！！'
            })
            console.log('标号验证返回：', res)
            if (res.data) {
              this.metermessage.f_meternumber = ''
              this.$showAlert('表号已存在！！', 'warning', 3000)
            }
          }
        }
      }
    },
    computed: {
      usertypes () {
        return this.$appdata.getParam('用户类型')
      },
      positions () {
        return this.$appdata.getParam('安装位置')
      },
      meterbrands () {
        return this.$GetSaleParam.getGasbrand()
      }
    }
  }
</script>
