<template>
  <div class="auto select-overspread" id="addBox">
    <validator name='v'>
      <ul class="nav nav-tabs" >
        <li role="presentation" class="active"><a href="#">{{model.f_adjustable_name}}信息</a></li>
      </ul>
      <criteria-paged :model="model" v-ref:paged>
        <criteria partial='criteria' v-ref:cri>
          <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
            <div class="row">
              <div class="col-sm-3 form-group">
                <label class="font_normal_body">客户编号</label>
                <input type="text" style="width:60%" class="input_search" v-model="model.f_userinfo_code"
                       placeholder="客户编号">
              </div>
              <div class="col-sm-3 form-group">
                <label class="font_normal_body">客户名称</label>
                <input type="text" style="width:60%" class="input_search" v-model="model.f_user_name"
                       placeholder="客户名称">
              </div>
              <div class="col-sm-3 form-group">
                <label class="font_normal_body">车牌号</label>
                <input type="text" style="width:60%" class="input_search" v-model="model.f_user_name"
                       placeholder="车牌号">
              </div>
              <div class="span" style="float:right;">
                <button class="button_search button_spacing" @click="$parent.$parent.search">查询</button>
                <button class="button_clear button_spacing" @click="$parent.$parent.clear()">清空</button>
              </div>
            </div>
          </div>
        </criteria>

        <data-grid :model="model" partial='list' class="list_area table_sy"  v-ref:grid>
          <template partial='head'>
            <tr>
              <th>
                <nobr>客户编号</nobr>
              </th>
              <th>
                <nobr>客户名称</nobr>
              </th>
              <th>
                <nobr>车牌号</nobr>
              </th>
              <th>
                <nobr>应收</nobr>
              </th>
              <th>
                <nobr>欠费</nobr>
              </th>
              <th>
                <nobr>气价</nobr>
              </th>
              <th>
                <nobr>操作</nobr>
              </th>

            </tr>
          </template>
          <template partial='body'>
            <td style="text-align: center;">
              <nobr>
                <span>{{row.f_userinfo_code}}</span></nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{row.f_user_name}}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{row.f_meternumber}}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{row.f_preamount}}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{row.f_arrears}}</nobr>
            </td>
            <td style="text-align: center;">
              <nobr>{{row.price}}</nobr>
            </td>
            <td style="text-align: center;">
              <button class="btn btn-link " @click="$parent.$parent.$parent.arrearsConfirm(row)"> 收费 </button>
            </td>
          </template>
          <template partial='foot'></template>
        </data-grid>
      </criteria-paged>

      <div style="text-align:right;height:auto;margin-top:30px;margin-right:50px;">
        <button class="button_clear " @click="cancel()" >取消</button>
      </div>
      <arrears-confirm :show.sync="limitShow" :arrears-data="arrearsData"
                 @cancel-show="cancelShow"></arrears-confirm>
    </validator>
  </div>
</template>
<script>

  import {HttpResetClass} from "vue-client";

  export default {
    data () {
      return {
        isshow: false,
        tempPosition: {
          position: { //经纬度信息
            lng: 0, //经度
            lat: 0  //纬度
          },
          formattedAddress: '' //地址信息
        },
        model: {
          f_adjustable_id: '',
          f_adjustable_name: '',
          f_adjustable_address: '',
          f_adjustable_type: '',
          f_adjustable_style: '',
          f_in_pipe: '',
          f_out_pipe: '',
          f_operate_date: '',
          f_open_date: '',
          f_manufacturer: '',
          f_remark: '',
          rows:[]
        },
        riser:[{}],
        delrow:[],
        haveid: false,
        havename: false,
        changedate: true,
        adjustabletype: this.$appdata.getParam('装置类型'),
        manufacturer: this.$appdata.getParam('生产厂家'),
        limitShow: false,
        arrearsData:{}
      }
    },
    props: ['data', 'f_filialeid'],
    watch: {
      'data' (val) {
        this.datatomodel()
      }
    },
    ready () {
      console.log(this.$login.toStandardTimeString())
      this.datatomodel()
    },
    methods: {
      search(){
        let param = {
          f_orgid:this.$login.f.orgid,
          f_adjustable_id: this.model.f_adjustable_id
        }
        this.$resetpost('rs/logic/gasGunArrearsCharge',param).then((res) => {
          this.model.rows = res.data
        })
        console.log(this.model)
      },
      cancelShow () {
        this.limitShow = false
      },
      arrearsConfirm (row) {
        this.limitShow = true
        console.log("有没有row",row)
        this.arrearsData = row
        console.log("有没有arrearsData",this.arrearsData)
      },
      async confirm () {
        if (this.haveid === true || this.havename === true) {
          return
        }
        this.model.f_adjustable_id = `${this.model.f_adjustable_id}`
        this.model.f_adjustable_state = '正常'
        this.model.f_adjustable_type = this.model.f_adjustable_type[0] ? this.model.f_adjustable_type[0] : ''
        this.model.f_manufacturer = this.model.f_manufacturer[0] ? this.model.f_manufacturer[0] : ''

        this.model.f_filialeid = this.f_filialeid

        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
        let http = new HttpResetClass()
        let res=await http.load('post', `rs/entity/t_adjustablebox`, this.model,{resolveMsg:'加气枪保存成功', rejectMsg: '保存失败!!!'})
        if(this.riser[0]){
          for(var i=0;i<this.riser.length;i++){
            var params={
              id:this.riser[i].id,
              f_table_name:`t_adjustablebox`,
              f_field_name:'id',
              f_parent_id:res.data.id,
              f_value:this.riser[i].f_value,
              // f_chinese_name:'立管编号',
              f_operator : this.$login.f.name,
              f_operatorid : this.$login.f.id,
              f_orgid : this.$login.f.orgid,
              f_orgname : this.$login.f.orgs,
              f_depid : this.$login.f.depids,
              f_depname : this.$login.f.deps
            }
            if(this.riser[i].f_value){
              await http.load('post', `rs/entity/t_attribute`, params, {resolveMsg: null, rejectMsg: null})
            }else{
              await this.$resetdelete('rs/entity/t_attribute', {id: this.riser[i].id}, {warnMsg:null,resolveMsg: null, rejectMsg: null})
            }
          }
        }
        if(this.delrow.length>0){
          for(var i=0;i<this.delrow.length;i++){
            await this.$resetdelete('rs/entity/t_attribute', {id: this.delrow[i]}, {warnMsg:null,resolveMsg: null, rejectMsg: null})
          }
        }
        this.$dispatch('success', '调压箱保存成功', this.model, res)
        this.riser=[{}]
        this.delrow=[]

      },
      delriser (index,row) {
        if (this.riser.length > 1) {
          this.riser.splice(index, 1)
          if(row.id){
            this.delrow.push(row.id)
          }
        }
      },
      addriser () {
        this.riser.push({})
      },
      async datatomodel () {
        this.delrow=[]
        this.$nextTick(() => {
          if (JSON.stringify(this.data) === '{}') {
            Object.assign(this.$data.model, this.$options.data().model)
            delete this.model.id
            this.model.f_in_pipe = 'DN'
            this.model.f_out_pipe = 'DN'
            this.changedate = true
            this.model.f_operate_date = this.$login.toStandardTimeString()
            this.model.f_open_date = this.$login.toStandardTimeString()
          } else {
            this.model = Object.assign(this.model, {}, this.data)
            this.model.f_adjustable_type = this.model.f_adjustable_type ? [this.model.f_adjustable_type] : ''
            this.model.f_manufacturer = this.model.f_manufacturer ? [this.model.f_manufacturer] : ''
            // this.model.f_adjustable_style = this.model.f_adjustable_style ? [this.model.f_adjustable_style] : ''
            this.model.f_operate_date ? this.changedate = false : this.changedate = true
          }
        })
        if(this.data.id){
          // var data=await this.$resetpost('rs/sql/saleSingleTable', {data: {tablename: 't_attribute', condition: `f_table_name = 't_adjustablebox' and f_parent_id = '${this.data.id}' and f_chinese_name ='立管编号'`}}, {resolveMsg: null, rejectMsg: '获取加气枪失败'})
          var data=await this.$resetpost('rs/sql/saleSingleTable', {data: {tablename: 't_attribute', condition: `f_table_name = 't_adjustablebox' and f_parent_id = '${this.data.id}'`}}, {resolveMsg: null, rejectMsg: '获取加气枪失败'})
          this.riser=data.data
        }

        if (this.riser.length===0){
          this.riser=[{}]
        }
      },
      cancel () {
        this.$dispatch('close')
      },
      modify () {
        this.model = Object.assign({}, this.gasmessage)
        this.model.f_meter_type = this.model.f_meter_type[0]
        this.getArrParam()
        this.transfer()
        this.data = {}
      },
      changeid () {
        this.$resetpost('rs/sql/saleSingleTable', {data: {tablename: 't_adjustablebox', condition: `f_adjustable_id = '${this.model.f_adjustable_id}' and f_orgid = '${this.$login.f.orgid}'`}}, {resolveMsg: null, rejectMsg: null})
          .then((res) => {
            res.data.length > 0 ? this.haveid = true : this.haveid = false
          })
      },
      changename () {
        this.$resetpost('rs/sql/saleSingleTable', {data: {tablename: 't_adjustablebox', condition: `f_adjustable_name = '${this.model.f_adjustable_name}' and f_orgid = '${this.$login.f.orgid}'`}}, {resolveMsg: null, rejectMsg: null})
          .then((res) => {
            res.data.length > 0 ? this.havename = true : this.havename = false
          })
      },
      openModal () {
        this.isshow = true
      },
      closeModal () {
        this.isshow = false;
      },
      getmylocation (res) {
        this.tempPosition = res
      },
      confirmLocation () {
        this.model.f_adjustable_address = this.tempPosition.formattedAddress
        this.model.f_adjustable_lng = this.tempPosition.position.lng
        this.model.f_adjustable_lat = this.tempPosition.position.lat
        this.isshow = false;
      }
    }
  }
</script>
<style lang="less">
  #addBox{
    #addboxmap {
      height: 300px;
    }
    .glyphicon:hover {
      color: red;
    }
    .glyphicon {
      color: blue;
      cursor: pointer;
    }
  }
</style>
