<template>
  <div class="span">
    <validator name='v' style="overflow-y: auto">
      <form novalidate class="form-horizontal select-overspread" @valid="confirmEditcheck = true" @invalid="confirmEditcheck = false">
        <div class="row auto">
        <div style="margin-top: 2%">
          <img style="margin-top: -2px;margin-left: 2px" src="./../../../../../static/images/lefticon/矩形1183.png">
          <a style="font-size: 16px;font-weight: 500;">表计信息</a>&nbsp;<a style="color: #999999;text-decoration: none"></a>
        </div>
        <div class="col-sm-6" :class="[$v.f_meternumber.required ? 'has-error' : '']">
          <label class="font_normal_body">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;表号&nbsp;&nbsp;&nbsp;</label>
          <input type="text" v-model="newmeterinfo.f_meternumber" @blur="onbulrbunmber($event)" class="input_search" style="width:60%" v-validate:f_meternumber='{required: true }' placeholder="表号">
        </div>

        <div class="col-sm-6" :class="[$v.brand.required ? 'has-error' : '']">
          <label class="font_normal_body" title="参数:气表类型2">&nbsp;&nbsp;&nbsp;气表类型</label>
          <input type="text" v-show="false" v-model="$refs.brand.selectedItems" v-validate:brand='{required: true }'>
          <v-select style="width:60%" title="气表类型2"
                    placeholder='气表类型'
                    :value.sync="newmeterinfo.f_metertype"
                    v-model="newmeterinfo.f_metertype"
                    :options='metertype'
                    close-on-select
                    :value-single="true" v-ref:brand>  </v-select>
        </div>
        <div class="col-sm-6" :class="[$v.f_meter_brand.required ? 'has-error' : '']">
          <label class="font_normal_body">&nbsp;&nbsp;&nbsp;气表品牌</label>
          <input type="text" v-show="false" v-model="newmeterinfo.f_meter_brand"
                 v-validate:f_meter_brand='{required: true }'>
          <v-select
            placeholder='气表品牌'
            :value.sync="newmeterinfo.f_meter_brand"
            style="width:60%"
            :value-single="true"
            v-model="newmeterinfo.f_meter_brand"
            :options='meterbrand'
            close-on-select>
          </v-select>
        </div>
        <div class="col-sm-6" :class="[$v.f_meter_style.required ? 'has-error' : '']">
          <label class="font_normal_body" >&nbsp;&nbsp;&nbsp;气表型号</label>
          <input type="text" v-show="false" v-model="newmeterinfo.f_meter_style"
                 v-validate:f_meter_style='{required: true }'>
          <v-select style="width:60%"
                    placeholder='气表型号'
                    :value.sync="newmeterinfo.f_meter_style"
                    v-model="newmeterinfo.f_meter_style"
                    :options='meterstyle'
                    close-on-select
                    :value-single="true">  </v-select>
        </div>
        <div class="col-sm-12" >
          <label class="font_normal_body">&nbsp;&nbsp;&nbsp;设备地址</label>
          <input type="text" v-model="newmeterinfo.f_device_address" class="input_search" style="width:80%" placeholder="设备地址">
        </div>
        <div class="col-sm-6" >
          <label class="font_normal_body">&nbsp;&nbsp;&nbsp;是否领用</label>
          <v-select style="width:60%"
                    placeholder='是否领用'
                    :value.sync="newmeterinfo.f_is_receive"
                    v-model="newmeterinfo.f_is_receive"
                    :options='isReceive'
                    disabled
                    close-on-select
                    :value-single="true">  </v-select>
        </div>
        <div class="col-sm-6" >
          <label class="font_normal_body">&nbsp;&nbsp;&nbsp;气表年限</label>
          <input type="text" v-model="newmeterinfo.f_years" class="input_search" style="width:60%" placeholder="气表年限">
        </div>
        <div class="col-sm-6" >
          <label class="font_normal_body">&nbsp;&nbsp;&nbsp;生产日期</label>
          <datepicker  placeholder="生产日期" style="width: 60%"
                       v-model="newmeterinfo.f_manufacture_date"
                       :value.sync="newmeterinfo.f_manufacture_date"
                       :format="'yyyy-MM-dd HH:mm:ss'">
          </datepicker>
          <!--          <input type="text" v-model="newmeterinfo.f_manufacture_date" class="input_search" style="width:60%" placeholder="生产日期">-->
        </div>

        <div class="col-sm-6" >
          <label class="font_normal_body">&nbsp;&nbsp;&nbsp;批次号&nbsp;&nbsp;&nbsp;</label>
          <input type="text" v-model="newmeterinfo.f_batch_number" class="input_search" style="width:60%" placeholder="批次号">
        </div>
        <div class="col-sm-6" >
          <label class="font_normal_body">&nbsp;&nbsp;&nbsp;终端名称</label>
          <input type="text" v-model="newmeterinfo.f_terminal_id" class="input_search" style="width:60%" placeholder="终端名称">
        </div>

        <div class="col-sm-6" >
          <label class="font_normal_body">&nbsp;&nbsp;&nbsp;设备号&nbsp;&nbsp;&nbsp;</label>
          <input type="text" v-model="newmeterinfo.f_device_id" class="input_search" style="width:60%" placeholder="设备号">
        </div>
        <div class="col-sm-6" >
          <label class="font_normal_body">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;采购人</label>
          <input type="text" v-model="newmeterinfo.f_purchaser" class="input_search" style="width:60%" placeholder="采购人">
        </div>
        <div class="col-sm-6" >
          <label class="font_normal_body">&nbsp;&nbsp;&nbsp;采购时间</label>
          <datepicker  placeholder="采购时间" style="width: 60%"
                       v-model="newmeterinfo.f_purchase_date"
                       :value.sync="newmeterinfo.f_purchase_date"
                       :format="'yyyy-MM-dd HH:mm:ss'">
          </datepicker>
        </div>
        </div>
        <div class="auto row">
        <div style="margin-top: 2%">
          <img style="margin-top: -2px;margin-left: 2px" src="./../../../../../static/images/lefticon/矩形1183.png">
          <a style="font-size: 16px;font-weight: 500;">厂商信息</a>&nbsp;<a style="color: #999999;text-decoration: none"></a>
        </div>
        <div class="col-sm-6" >
          <label class="font_normal_body">&nbsp;&nbsp;&nbsp;气表厂家</label>
          <input type="text" v-model="newmeterinfo.f_manufacturers" class="input_search" style="width:60%" placeholder="气表厂家">
        </div>
        <div class="col-sm-6" >
          <label class="font_normal_body">&nbsp;&nbsp;&nbsp;厂商代码</label>
          <input type="text" v-model="newmeterinfo.f_vender" class="input_search" style="width:60%" placeholder="厂商代码">
        </div>
        </div>
        <div class="col-sm-12" style="text-align:right;height:auto;margin-top:6px;">
          <button class="button_search" type="button" @click="confirmEdit()" :disabled="!confirmEditcheck">确认</button>
          <button class="button_clear" type="button" @click="close()">取消</button>
        </div>
      </form>
    </validator>
  </div>
</template>


<script>
    let readyGen = async function (self) {
      await self.$LoadParams.loadParam()
      self.initQueryParam()
        // self.reflash()
    }
    export default {
      title: '表计信息修改',
      props: ['oldmeter', 'f_filialeid'],
      data () {
        return {
          row: {},
          meterbrand: [],
          newmeterinfo: {},
          confirmEditcheck: false,
          meterstyle: []
        }
      },
      ready () {
        this.newmeterinfo = Object.assign({}, {}, this.oldmeter)
        this.newmeterinfo.f_is_receive = this.newmeterinfo.f_is_receive !== '是' ? '否' : '是'
        readyGen(this)
      },
      watch: {
        'oldmeter' () {
          this.newmeterinfo = Object.assign({}, {}, this.oldmeter)
        },
        'newmeterinfo.f_meter_brand' () {
          if (this.newmeterinfo.f_meter_brand == null || this.newmeterinfo.f_meter_brand == '') return
          let gasmodels = []
          let gastype = []
          this.$GetSaleParam.getGasbrand().forEach((item) => {
            if (item.label == this.newmeterinfo.f_meter_brand) {
              gasmodels = item.value.gasmodel
            }
          })
          gasmodels.forEach((items) => {
            let temp = {}
            temp.label = items.label
            temp.value = items.value.f_meter_style + items.value.f_type
            gastype.push(temp)
          })
          this.meterstyle = gastype
        }
      },
      methods: {

        initQueryParam () {
          let arr = []
          this.$GetSaleParam.getGasbrand().forEach((item) => {
            let temp = {}
            temp.label = item.label
            temp.value = item.value.f_meter_brand
            arr.push(temp)
          })
          console.log(arr)
          this.meterbrand = [{label: '全部', value: ''}, ...arr]
        },
        async onbulrbunmber ($event) {
          if ($event.target.value === '') {
            return
          }
          let expiration = await this.$resetpost('rs/sql/singleTable_OrderBy', {data:
          {orderitem: 'id',
            items: 'f_meternumber',
            tablename: 't_meterinfo',
            condition: 'f_meternumber = \'' + $event.target.value + '\''}}, {resolveMsg: '', rejectMsg: ''})

          console.log(expiration)
          if (expiration.data.length !== 0) {
            this.$showMessage('表号已经存在')
          }
        },
        close () {
          this.$parent.meterinfos = []
          this.$dispatch('close')
        },
        confirmEdit () {
          this.newmeterinfo.f_diameter = ''
          this.newmeterinfo.f_orgid = this.$login.f.orgid
                // this.newmeterinfo.f_metertype='威星民用表'
          this.newmeterinfo.f_orgname = this.$login.f.orgs
          this.newmeterinfo.f_depid = this.$login.f.depids
          this.newmeterinfo.f_depname = this.$login.f.deps
          this.newmeterinfo.f_operator = this.$login.f.name
          this.newmeterinfo.f_operatorid = this.$login.f.id
          if (JSON.stringify(this.oldmeter) != '{}') {
            if (this.newmeterinfo.f_meternumber && this.newmeterinfo.f_meternumber !== '') {
              this.$resetpost('rs/entity/t_meterinfo', this.newmeterinfo, {resolveMsg: '修改成功。', rejectMsg: '修改失败!!!请核对表号是否重复!'})
            } else {
              this.$showMessage('表号不能为空!')
            }
          } else {
                    // 是新增
            this.newmeterinfo.f_state = '待安装'
            this.newmeterinfo.f_filialeid = this.f_filialeid ? this.f_filialeid : this.$login.f.orgid
            this.$resetpost('rs/entity/t_meterinfo', this.newmeterinfo, {resolveMsg: '添加成功。', rejectMsg: '添加失败!!!请核对表号是否重复'})
          }
          this.$dispatch('close')
        }
      },
      computed: {
        meterbrands () {
          return this.$GetSaleParam.getGasbrand()
        },
        iottype () {
          return this.$appdata.getParam('表通道类型')
        },
        metertype () {
          return this.$appdata.getParam('气表类型2')
        },
        isReceive () {
          return [{label: '否', value: '否'}, {label: '是', value: '是'}]
        }
      }
    }
</script>
