<template>
  <div class="setting app-container">
    <h3>提现设置</h3>
    <el-form :model="setQuery" ref="setQuery" inline v-loading="loading" label-width="200px" :rules="rules">
      <el-row>
        <el-form-item label="单笔提现规则 最低提现金额" prop="minTotal">
          <el-input type="number" v-model="setQuery.minTotal"></el-input>
          <div>微信商家转账到零钱，现金营销场景最高额度为单笔200元。</div>
        </el-form-item>
        <el-form-item label-width="120px" label="元,最高提现金额" prop="up">
          <el-input type="number" v-model="setQuery.maxTotal"></el-input>
        </el-form-item> <el-form-item label-width="0" label="元"></el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="单日提现额度" size="normal" prop="dayMaxTotal">
          <el-input type="number" v-model="setQuery.dayMaxTotal">
            <template slot="append">元</template>
          </el-input>
          <div>微信商家转账到零钱，一个商户号单日向同一用户的最高转账额度为2万元，不可提高。</div>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="提现手续费" prop="commission">
          <el-input type="number" v-model="setQuery.commission">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="提现频次" size="normal" prop="timeUnit">
          <el-select v-model="setQuery.timeUnit" @change="checkType()">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
          的
          <template v-if="setQuery.timeUnit == 'MONTH'">
            <el-select v-model="setQuery.startDay" clearable placeholder="开始日">
              <el-option v-for="item in dayOption" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            至
            <el-select v-model="setQuery.endDay" clearable placeholder="结束日">
              <el-option v-for="item in dayOption" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </template>
          <template v-else>
            <el-time-select placeholder="起始时间" v-model="startTime" @change="changeStart" :picker-options="{
              start: '00:00',
              step: '00:10',
              end: '24:00'
            }">
            </el-time-select>
            到
            <el-time-select placeholder="结束时间" v-model="endTime" @change="changeTime" :picker-options="{
              start: '00:00',
              step: '00:10',
              end: '24:00',
              minTime: startTime
            }">
            </el-time-select>
          </template>


        </el-form-item>
        <el-form-item label="可申请提现" label-width="90px" size="normal">
          <el-input type="number" v-model="setQuery.size" size="normal"></el-input>
        </el-form-item>
        <el-form-item label="次" label-width="0" size="normal">
        </el-form-item>
      </el-row>
    </el-form>

    <div class="btn-group">
      <el-button>取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save">确 定</el-button>
    </div>

    <h3>小程序功能屏蔽设置</h3>
    <el-form :model="setQuery1" ref="setQuery1" inline v-loading="loading1" label-width="200px" :rules="rules1">
       <el-form-item label="移动会员订购开通" size="normal" prop="cmVipEnable">
          <el-select v-model="setQuery1.cmVipEnable">
            <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
       </el-form-item>
      </el-form>

    <div class="btn-group">
      <el-button>取 消</el-button>
      <el-button type="primary" :loading="loading1" @click="save1">确 定</el-button>
    </div>


  </div>
</template>

<script>


import { saveOrUpdate, getRuler,getMiniSetting,setMini } from "@/api/setting"
import currency from "@/utils/currency";
export default {

  data() {
    return {
      loading: false,
      loading1: false,
      startTime: '',
      endTime: '',
      setQuery1: {
        cmVipEnable: true, //是否开启会员功能
      },
      setQuery: {
        timeUnit: 'DAY', //时间单位 DAY MONTH 

        minTotal: '', //单笔最小提现金额
        size: '', //提现次数
        maxTotal: '',//单笔最大提现金额
        id: '',

        endMinute: '', //每日结束分
        endHour: '', //每日结束时
        startMinute: '', //每日开始分
        startHour: '', //每日开始时

        startDay: '', //每月开始号数
        endDay: '',//每月结束号数

        dayMaxTotal: '',//单日最大转账金额
        commission: '' //提现手续费
      },
      options: [
        {
          label: '每天',
          value: 'DAY'
        },
        {
          label: '每月',
          value: 'MONTH'
        }
      ],
      options1: [
        {
          label: '开启',
          value: true
        },
        {
          label: '关闭',
          value: false
        }
      ],
      dayOption: [

      ],
       rules1: {
        cmVipEnable: [
          {
            required: true,
            message: '请选择是否开启移动会员功能',
            trigger: 'change'
          }
        ],
       },
      rules: {
        timeUnit: [
          {
            required: true,
            message: '提现频次不能为空',
            trigger: 'blur'
          }
        ],
        minTotal: [
          { required: true, message: '单笔最小提现金额不能为空', trigger: 'blur' },
        ],
        size: [
          { required: true, message: '提现次数不能为空', trigger: 'blur' },
        ],
        maxTotal: [
          { required: true, message: '单笔最大提现金额不能为空', trigger: 'blur' },
        ],
        endMinute: [
          { required: true, message: '每日结束分不能为空', trigger: 'blur' },
        ],
        endHour: [
          { required: true, message: '每日结束时不能为空', trigger: 'blur' },
        ],
        startMinute: [
          { required: true, message: '每日开始分不能为空', trigger: 'blur' },
        ],
        startHour: [
          { required: true, message: '每日开始时不能为空', trigger: 'blur' },
        ],
        startDay: [
          { required: true, message: '每月开始号数不能为空', trigger: 'blur' },
        ],
        endDay: [
          { required: true, message: '每月结束号数不能为空', trigger: 'blur' },
        ],
        dayMaxTotal: [
          { required: true, message: '单日最大转账金额不能为空', trigger: 'blur' },
        ],
        commission: [
          { required: true, message: '提现手续费不能为空', trigger: 'blur' },
        ]
      }

    }
  },
  methods: {
    save() {

      this.$refs.setQuery.validate((v) => {
        this.checkType(1)
        if (v) {
          this.loading = true
          saveOrUpdate({
            ...this.setQuery,
            maxTotal: currency(this.setQuery.maxTotal).intValue,
            minTotal: currency(this.setQuery.minTotal).intValue,
            dayMaxTotal: currency(this.setQuery.dayMaxTotal).intValue
          }).then(res => {
            this.loading = false

            if (res.state == 'Success') {

              this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success'
              });

            }
          })
        }
      });
    },
     save1() {

      this.$refs.setQuery1.validate((v) => {
        if (v) {
          this.loading1 = true
          setMini(this.setQuery1).then(res => {
            this.loading1= false
            if (res.state == 'Success') {
              this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success'
              });

            }
          })
        }
      });
    },
    changeTime() {
      if (this.startTime) return
      this.$message({
        message: '请先选择开始时间',
        type: "error",
        duration: 5000,
      });
      this.endTime = ''
    },
    changeStart() {
      this.endTime = ''
    },
    checkType(edit) {

      if (this.setQuery.timeUnit == 'DAY') {
        this.setQuery.startDay = ''
        this.setQuery.endDay = ''

        if (edit) {
          const [startHour, startMinute] = this.startTime.split(':')
          const [endHour, endMinute] = this.endTime.split(':')

          this.setQuery.startHour = startHour
          this.setQuery.endHour = endHour
          this.setQuery.startMinute = startMinute
          this.setQuery.endMinute = endMinute
        }
      } else {
        this.setQuery.startHour = ''
        this.setQuery.endHour = ''
        this.setQuery.startMinute = ''
        this.setQuery.endMinute = ''
      }
    },
    getMiniSetting(){
      this.loading1 =true
      getMiniSetting().then(res=>{
      this.loading1 =false

        if(res.state=='Success'){
          this.setQuery1.cmVipEnable = res.content.cmVipEnable
        }
      })
    }
  },
  created() {
    this.getMiniSetting()
    // 月份天数
    for (let index = 0; index < 31; index++) {
      this.dayOption.push(
        {
          label: index + 1 + '号',
          value: index + 1
        }
      )
    }

    getRuler().then(res => {
      if (res.content) {
        this.setQuery = res.content;
        this.setQuery.maxTotal = currency(res.content.maxTotal, {
          fromCents: true
        })
        this.setQuery.minTotal = currency(res.content.minTotal, {
          fromCents: true
        })
        this.setQuery.dayMaxTotal = currency(res.content.dayMaxTotal, {
          fromCents: true
        })
        if (this.setQuery.timeUnit == 'DAY') {
          console.log()

          this.startTime = (this.setQuery.startHour + "").padStart(2, 0) +
            ':' + (this.setQuery.startMinute + "").padStart(2, 0)
          this.endTime = (this.setQuery.endHour + "").padStart(2, 0) +
            ':' + (this.setQuery.endMinute + "").padStart(2, 0)
        }
      }
    })
  },
};
</script>

<style lang="scss" scoped>
.setting {
  padding-bottom: 40px;




  .btn-group {
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>