<template>
  <div class="auto" style="overflow:auto;">
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <div class="row auto">

          <div class="col-sm-4 form-group"
               :class="[$v.f_using_base_old.required || $v.f_using_base_old.dctest ? 'has-error' : 'has-success']">
            <label class=" font_normal_body">&emsp;表底数</label>
            <input class="input_search" style="width:60%" type="number"
                   v-model="model.f_using_base_old"
                   placeholder="请输入表底数"
                   v-validate:f_using_base_old='{required: true, dctest: [-1, ">" ] }'
            >
          </div>
        </div>
        <div class="row" style="height: 30%">
          <div class="col-sm-4 form-group">
            <label  class="font_normal_body">拆表原因</label>
            <v-select  v-model="model.f_reason"
                      placeholder='拆表原因'
                      :value.sync="model.f_reason"
                      :options='reason'
                       :value-single="true"
                      :search="false"
                      v-ref:reason
                      close-on-select></v-select>
          </div>
          <div class="col-sm-8 form-group" v-if="model.f_reason[0] === '其他'">
            <label class="font_normal_body">其他原因</label>
            <input class="input_search" style="width:80%" v-model="model.f_othereason" placeholder="请填写原因"/>
          </div>
        </div>
        <div style="text-align:right;height:auto;">
          <button class="button_search btn-gn" type="button" @click="confirm()" :disabled="!$v.valid"
          >确认
          </button>
          <button class="button_clear btn-gn" type="button" @click="clean()">取消
          </button>
        </div>
        <!--<div style="height: auto;width: 100%">-->
        <!--<div style="height: 10%">-->
        <!--<img style="margin-top: -5px" src="../../../../../../static/images/lefticon/矩形1183.png">-->
        <!--<a style="font-size: 16px;font-weight: 500;color: #333;text-decoration: none;">附件信息</a>&nbsp;<a style="color: #999999"></a>-->
        <!--</div>-->
        <!--<upload :blodid="row.f_userinfo_id" isremark="true" fusetype="换表"></upload>-->
        <!--</div>-->
      </form>
    </validator>
  </div>
</template>
<script>
/**
 *换表管理
 *组合界面，加载机表，物联网表，气量卡表，金额卡表旧表信息，以及控制显示换新表信息
 */
export default {
  title: '换表登记',
  data () {
    return {
      resid: [],  // 存放新增的f_files表中id
      model: {
        metermessage: {
          f_meter_type: '',
          gasbrand: '',
          gasmodel: '',
          f_meternumber: '',
          f_meter_base: 0,
          f_metertitles: '',
          f_oldmeter_use_reference: 0 // 旧表使用量参考值
        },
        // 旧表底数
        f_using_base_old: '',
        // 换表补气量
        f_remanent_gas: 0,
        // 换表补气单价
        f_remanent_price: 0,
        f_remanent_money: 0,
        f_type: '换新表',
        f_comments: '',
        f_othereason: '',
        f_payment: '现金缴费',
        f_print: '普通收据',
        f_state: '有效',
        f_changemeter_date: this.$login.toStandardTimeString(),
        f_changemeter_fee: this.$appdata.getSingleValue('换表金额') ? this.$appdata.getSingleValue('换表金额') : 0
      },
      meterinfo: false,

      // 控制气量和金额的转换显示
      changeType: '',
      config: {
        showupload: true, //
        dispatch: false // 是否派工
      },
      // 控制气量金额的编辑
      isGas: false,
      reason: this.$appdata.getParam('拆表原因') ? this.$appdata.getParam('拆表原因') : [],
      paytype: this.$appdata.getParam('付款方式'),
      printstyle: this.$appdata.getParam('打印格式'),
      gasproperties: [],
      newinfo: {
        f_user_type: '',
        f_gasproperties: '',
        pricetype: '',
        pricename: '',
        f_perform_date: this.$login.toStandardDateString() + ' 00:00:00' // 操作时间
      },
      usertypes: this.$appdata.getParam('用户类型') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')] : [],
      pricetypes: this.$appdata.getParam('气价类型') ? [{label: '全部', value: ''}, ...this.$appdata.getParam('气价类型')] : []
    }
  },
  props: ['row'],
  ready () {

  },
  events: {},
  methods: {

    // 返回
    clean () {
      this.$dispatch('success', '换表', this.row)
    },
    async confirm () {
      if (this.row.f_meter_type.includes('机表')) {
        this.$showMessage(`将生成抄表记录，是否确认`, ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            let data = {
              downDate: this.$login.toStandardTimeString(),
              startDate: this.$login.toStandardDateString(),
              endDate: this.$login.toStandardDateString(),
              condition: `f_userfiles_id = '${this.row.f_userfiles_id}'`,
              f_operator: this.$login.f.name,
              f_operatorid: this.$login.f.id,
              f_meternumber: this.row.f_meternumber,
              f_userinfo_id: this.row.f_userinfo_id,
              f_userfiles_id: this.row.f_userfiles_id,
              f_meterbase: this.model.f_using_base_old,
              f_meter_brand: this.row.f_meter_brand,
              f_price_id: this.row.f_price_id
            }
            this.$resetpost('rs/logic/changeMeterHand', {data: data}, {
              resolveMsg: '生成欠费成功',
              rejectMsg: '生成欠费失败'
            }).then((res) => {
              console.log('成功')
              this.saveobj()
            })
          } else {

          }
        })
      } else if (this.row.f_meter_type.includes('卡表')) {
        let totalGas = (this.row.f_total_gas - 0) + (this.row.f_initial_base - 0)
        if (this.model.f_using_base_old > totalGas) {
          this.$showAlert('该表可能存在超用，请核实输入后进行拆表操作！！', 'warning', 5000)
          this.$dispatch('button-specifies', {name: '超用收费', value: 'over-use-charge'}, this.row)
          return
        }
        this.saveobj()
      } else {
        // 物联网表拆表
        this.$showMessage(`将生成抄表记录，是否确认`, ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            let data = {
              downDate: this.$login.toStandardTimeString(),
              startDate: this.$login.toStandardDateString(),
              endDate: this.$login.toStandardDateString(),
              condition: `f_userfiles_id = '${this.row.f_userfiles_id}'`,
              f_operator: this.$login.f.name,
              f_operatorid: this.$login.f.id,
              f_meternumber: this.row.f_meternumber,
              f_userinfo_id: this.row.f_userinfo_id,
              f_userfiles_id: this.row.f_userfiles_id,
              f_meterbase: this.model.f_using_base_old,
              f_meter_brand: this.row.f_meter_brand,
              f_meter_type: this.row.f_meter_type,
              f_meter_classify: this.row.f_meter_type,
              f_price_id: this.row.f_price_id
            }
            // 划价
            this.$resetpost('rs/logic/changeMeterWebHand', {data: data}, {resolveMsg: null, rejectMsg: null }).then((res) => {
              console.log('划价成功')
                // 保存拆表记录
              this.saveobj()
            })
          } else {
            // 取消
          }
        })
      }
    },
    async saveobj () {
      let res = await this.$resetpost('rs/sql/singleTable_OrderBy', {
        data: {
          orderitem: 'f_userfiles_id',
          items: '*',
          tablename: 't_userfiles',
          condition: 'f_userfiles_id = ' + this.row.f_userfiles_id
        }
      })
      console.log('=============', res.data[0])
      let data = {
        f_userfiles_id: {
          f_userfiles_id: this.row.f_userfiles_id,
          f_table_state: '拆表',
          version: res.data[0].version
        },
        f_userfile_id: this.row.f_userfiles_id,
        f_user_id: this.row.f_user_id,
        f_userinfo_id: this.row.f_userinfo_id,
        f_user_name: this.row.f_user_name,
        f_operat_type: '拆表',
        f_operate_type: '拆表',
        f_describe: `${this.$login.f.name}对客户${this.row.f_user_name}进行拆表操作`,
        f_state: '有效',
        f_comments: this.model.f_reason,
        f_othereason: this.model.f_othereason,
        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,
        f_zoneid: this.$login.f.zoneid,
        f_zones: this.$login.f.zones
      }
      this.$resetpost('rs/entity/t_disable', data, {resolveMsg: '拆表成功', rejectMsg: '拆表失败'})
      this.$dispatch('success')
    }

  },

  watch: {}
}
</script>
