<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item label-width="0" prop="withdraw_state">
        <el-radio-group v-model="form.withdraw_state" size="medium">
          <el-radio
            v-for="(item, index) in withdraw_stateOptions"
            :key="index"
            :disabled="item.disabled"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label-width="0" prop="withdraw_bankflow">
        <el-input
          v-model="form.withdraw_bankflow"
          clearable
          :placeholder="__('请输入打款后银行流水账号')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item label-width="0" prop="withdraw_desc">
        <el-input
          v-model="form.withdraw_desc"
          clearable
          :placeholder="__('请输入备注')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item label-width="0" prop="withdraw_opertime">
        <el-date-picker
          v-model="form.withdraw_opertime"
          :placeholder="__('选择日期时间')"
          :style="{ width: '100%' }"
          type="datetime"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { doEdit } from '@/api/consume/withdraw'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'ConsumeWithdrawEdit',
    data() {
      return {
        form: {
          withdraw_state: undefined,
          withdraw_bankflow: undefined,
          withdraw_desc: undefined,
          withdraw_opertime: undefined,
        },
        rules: {
          withdraw_state: [
            {
              required: true,
              message: this.__('不能为空'),
              trigger: 'change',
            },
          ],
          withdraw_bankflow: [
            {
              required: true,
              message: this.__('请输入打款后银行流水账号'),
              trigger: 'blur',
            },
          ],
          withdraw_desc: [
            {
              required: true,
              message: this.__('请输入备注'),
              trigger: 'blur',
            },
          ],
        },
        withdraw_stateOptions: [
          {
            label: this.__('提现通过'),
            value: 1,
          },
          {
            label: this.__('驳回'),
            value: 2,
          },
        ],
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      renderTime,
      __,
      showEdit(row) {
        this.title = this.__('打款确认')
        this.form = {
          withdraw_id: row.withdraw_id,
          withdraw_state: row.withdraw_state,
          withdraw_bankflow: row.withdraw_bankflow,
          withdraw_desc: row.withdraw_desc,
          withdraw_opertime: row.withdraw_opertime,
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {}
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.form.withdraw_opertime = this.renderTime(
              this.form.withdraw_opertime
            )
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
