<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-tabs v-model="tabActiveName">
        <el-tab-pane label="手动添加" name="first" v-if="permission.hasPermission(`finance/change-balance`)">
          <el-form ref="form" :model="form" label-width="250px" class="form-page">
            <el-form-item label="会员编号">
              <el-input v-model="form.userName" @change="handleChange"></el-input>
            </el-form-item>
            <el-form-item label="会员姓名" v-show="userInfo.REAL_NAME!==null">
              <el-input :value="userInfo.REAL_NAME" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="会员级别" v-show="userInfo.REAL_NAME!==null">
              <el-input :value="userInfo.DEC_LV_NAME" :disabled="true"></el-input>
            </el-form-item>
            <!--<el-form-item label="报单中心级别" v-show="userInfo.DEC_ROLE_NAME!=='无'">-->
              <!--<el-input :value="userInfo.DEC_ROLE_NAME" :disabled="true"></el-input>-->
            <!--</el-form-item>-->

            <el-form-item :label="item.label" v-show="userInfo.REAL_NAME!==null" v-for="(item,key) in type" :key="key">
              <el-input :value="userInfo[item.name.toUpperCase()]" :disabled="true"></el-input>
            </el-form-item>

            <el-form-item label="账户类型">
              <el-select v-model="form.type" placeholder="请选择账户类型">
                <el-option v-for="(item,key) in type" :label="item.label" :value="key" :key="key"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="调整金额">
              <el-input v-model="form.amount"></el-input>
            </el-form-item>
            <el-form-item label="交易类型">
              <el-select v-model="form.dealType" placeholder="请选择交易类型">
                <el-option v-for="(item,key) in dealTypes" :label="item.TYPE_NAME" :value="item.ID"
                           :key="item.ID"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="form.remark"></el-input>
            </el-form-item>
            <el-form-item label="备注前台显示">
              <el-switch v-model="form.isShow" active-value="1" inactive-value="0">
              </el-switch>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :loading="submitButtonStat" :disabled="getCodeStat" @click="onSubmit">提交</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <!--<el-tab-pane label="批量导入" name="second"-->
                     <!--v-if="permission.hasPermission(`finance/import-change-balance-to-excel-table`)&&permission.hasPermission(`finance/import-change-balance`)">-->
          <!--<el-form ref="form" :model="form" label-width="250px" class="form-page">-->
            <!--<el-form-item label="Excel文件总行数">-->
              <!--<el-input v-model="excelForm.rowCount"></el-input>-->
            <!--</el-form-item>-->
            <!--<el-form-item label="">-->
              <!--<el-tag type="info">表格中账户类型要求英文：bonus(会员账户余额)、reconsume_points(复消积分)</el-tag>-->
            <!--</el-form-item>-->
            <!--<el-form-item>-->
              <!--<leo-excel-uploader-->
                  <!--:request-upload-route="`file/upload-excel`"-->
                  <!--:request-import-to-excel-table-route="`finance/import-change-balance-to-excel-table`"-->
                  <!--:request-import-excel-table-to-data-route="`finance/import-change-balance`"-->
                  <!--:import-row-count="excelForm.rowCount"-->
                  <!--excel-option="changeBalance"-->
                  <!--upload-btn-title="Excel导入"-->
                  <!--style="float: left;"-->
              <!--&gt;</leo-excel-uploader>-->
              <!--<el-button type="primary" @click="handleExcel" style="float: left; margin-left: 10px;">下载模板</el-button>-->
            <!--</el-form-item>-->
          <!--</el-form>-->
        <!--</el-tab-pane>-->
      </el-tabs>
    </div>
  </div>
</template>

<script>
  import Vue from 'vue'
  import store from '@/utils/vuexStore'
  import network from '@/utils/network'
  import tool from '@/utils/tool'
  import baseInfo from '@/utils/baseInfo'
  import LeoExcelUploader from '@/components/ExcelUploader'
  import {CDN_BASE_URL} from '@/utils/config'
  import permission from '@/utils/permission'

  export default {
    name: 'finance_change-balance-opt',
    components: {LeoExcelUploader},
    mounted() {
      network.getData('finance/change-balance-type').then(response => {
        this.type = response.type
        this.dealTypes = response.dealTypes
        this.loading = false
        if (permission.hasPermission(`finance/import-change-balance-to-excel-table`) && permission.hasPermission(`finance/import-change-balance`)) {
          this.tabActiveName = 'second'
        }
        if (permission.hasPermission(`finance/change-balance`)) {
          this.tabActiveName = 'first'
        }
      })
        network.postData('finance/mult-point', {opType: 1}).then(response => {
            this.form.balanceCode = response.balanceCode
            this.getCodeStat = false
        })
    },
    data() {
      return {
        form: {
          userName: null,
          type: null,
          amount: 0,
          remark: '',
          isShow: '1',
          dealType: null,
          balanceCode: '',
        },
        userInfo: {
          REAL_NAME: null,
          DEC_ROLE_NAME: '',
          BONUS: 0,
          BONUS_FREEZE: 0,
          DEC_LV: 0,
          DEC_DEC_LV: 0,
        },
        baseDecLevels: baseInfo.decLevels(),
        type: null,
        dealTypes: null,
        loading: true,
        submitButtonStat: false,
        getCodeStat: true,
        tool: tool,
        tabActiveName: 'first',
        excelForm: {
          rowCount: 0,
        },
        permission: permission,
      }
    },
    methods: {
      handleChange() {
        this.loading = true
        network.getData('user/full-info', {userName: this.form.userName}).then(response => {
          this.userInfo = response
          this.loading = false
        }).catch(response => {
          this.loading = false
        })
      },
      onSubmit() {
            if (this.form.type === 'bonus') {
              if ((Number.parseFloat(this.userInfo['BONUS']) + Number.parseFloat(this.form.amount)) < 0) {
                this.$confirm(`会员账户余额不足，是否继续提交`, '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                  this._handleSubmit()
                }).catch(() => {
                  this.submitButtonStat = false
                  return false
                })
              } else {
                this._handleSubmit()
              }
            } else {
              this._handleSubmit()
            }
      },
      _handleSubmit() {
        this.submitButtonStat = true
        let path = 'finance/change-balance'
        network.postData(path, this.form).then(response => {
          this.submitButtonStat = false
          this.$message({
            message: response,
            type: 'success'
          })
          this.$router.go(-1)
        }).catch(response => {
          this.submitButtonStat = false
        })
      },
      handleExcel() {
        window.open(CDN_BASE_URL + `/files/bonus_change_balance.xlsx`)
      },
    }
  }
</script>

<style scoped>

</style>