<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-form ref="form" :model="form" label-width="250px" class="form-page">
        <el-form-item label="会员编号">
          <el-input v-model="form.userName" @change="getData"></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="结算年">
          <el-date-picker
              v-model="selectYear"
              format="yyyy"
              value-format="yyyy"
              type="year"
              :editable="false"
              @change="handleMonth"
              :picker-options="pickerOptions0"
              placeholder="选择结算年">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结算月">
          <el-select v-model="selectMonth">
            <el-option v-for="(item, key) in allMonth" :key="key" :label="item.title" :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="可用PV" v-show="unusedPV!==null">
          <el-input :value="unusedPV" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="补扣PV" v-show="deductPv!==null">
          <el-input :value="deductPv" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="备注" v-if="false">
          <el-input type="textarea" :rows="2" placeholder="备注" v-model="form.remark">
          </el-input>
        </el-form-item>
        <el-form-item label="备注前台显示" v-if="false">
          <el-switch v-model="form.isShow" active-value="1" inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :loading="submitButtonStat">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import store from '@/utils/vuexStore'
import network from '@/utils/network'
import tool from '@/utils/tool'

export default {
  name: 'reconsume_deduct-audit-add',
  mounted () {
  },
  data () {
    return {
      form: {
        userName: null,
        calcYearMonth: null,
        //remark: null,
        //isShow: '1',
      },
      balance: {
        unusedPV: 0,
        unusedMonth: 0,
      },
      userInfo: {
        REAL_NAME: null,
      },
      unusedPV: null,
      deductPv: null,
      loading: true,
      submitButtonStat: false,
      selectYear: null,
      selectMonth: null,
      allMonth: [
        {title: '一月', value: 1, disabled: false},
        {title: '二月', value: 2, disabled: false},
        {title: '三月', value: 3, disabled: false},
        {title: '四月', value: 4, disabled: false},
        {title: '五月', value: 5, disabled: false},
        {title: '六月', value: 6, disabled: false},
        {title: '七月', value: 7, disabled: false},
        {title: '八月', value: 8, disabled: false},
        {title: '九月', value: 9, disabled: false},
        {title: '十月', value: 10, disabled: false},
        {title: '十一月', value: 11, disabled: false},
        {title: '十二月', value: 12, disabled: false},
      ],
      pickerOptions0: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        }
      },
    }
  },
  methods: {
    onSubmit () {
      this.form.calcYearMonth = this.selectYear + tool.dataLeftCompleting(2, '0', this.selectMonth)
      this.submitButtonStat = true
      let path = 'reconsume/deduct-audit-add'
      network.postData(path, this.form).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.submitButtonStat = false
        this.$router.go(-1)
      }).catch(response => {
        this.submitButtonStat = false
      })
    },
    getData(){
      if(this.form.userName !== null && this.form.userName !== ''){
        this.loading = true
        network.getData('reconsume/deduct-audit-add', {userName: this.form.userName}).then(response => {
          this.balance = response.balance
          this.deductPv = response.deductPv
          this.unusedPV = response.balance.unusedPV
          this.loading = false
          network.getData('user/full-info', {userName: this.form.userName}).then(response => {
            this.userInfo = response
            this.handleMonth()
            this.loading = false
          }).catch(response => {
            this.loading = false
          })
        })
      } else {
        this.loading = true
        network.getData('reconsume/deduct-audit-add').then(response => {
          this.selectYear = response.year
          this.loading = false
        })
      }
    },
    handleMonth(){
      if(this.form.userName === '' || this.form.userName === null){
        this.$message.error('请先填写会员编号')
      }
      network.getData(`reconsume/cant-deduct-month`, {userName: this.form.userName, year: this.selectYear}).then((data)=>{
        let cantMonth = []
        for(let i in data){
          cantMonth.push(Number.parseInt(data[i].CALC_MONTH.substr(4, 2)))
        }
        //console.log(cantMonth)
        for(let i in this.allMonth){
          if(tool.isInArray(cantMonth, this.allMonth[i].value)){
            this.allMonth[i].disabled = true
          }
        }
        //console.log(this.allMonth)
      })
    }
  }
}
</script>

<style scoped>

</style>
