<template>
  <el-dialog title="字符扣除" :visible.sync="value" width="500px" @close="handleClose" @open="handleOpen">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="用户昵称" prop="nickName">
        {{ nickName }}
      </el-form-item>
      <el-form-item label="翻译平台" prop="platformId">
        <el-select v-model="form.platformId" placeholder="请选择翻译平台">
          <el-option v-for="item in platformList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="apiKey">
        {{ apiKey }}
      </el-form-item>
      <el-form-item label="扣除字符(万)" prop="totalChars">
        <el-input v-model.number="form.totalChars" placeholder="充值字符(万)" />
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { tmMemberManageDeduct } from '@/api/admin/tm-member'
import { listTmPlatform } from '@/api/admin/tm-platform'

export default {
  name: 'TmMemberRecharge',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    apiKey: {
      type: String,
      default: ''
    },
    nickName: {
      type: String,
      default: ''
    },
    memberId: {
      type: Number,
      default: 0
    },
    userId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      loading: false,
      form: {},
      remainChars: 0,
      rules: {
        platformId: [{ required: true, message: '请选择翻译平台', trigger: 'blur' }],
        totalChars: [{ required: true, message: '请输入扣除字符(万)', trigger: 'blur' }]
      },
      platformList: []
    }
  },

  methods: {
    handleOpen() {
      this.getPlatform()
    },
    getPlatform() {
      listTmPlatform({ pageIndex: 1, pageSize: 1000 }).then(response => {
        if (response.code === 200) {
          this.platformList = response.data.list
        }
      })
    },
    resetForm() {
      this.form = {
        id: undefined,
        apiKey: undefined,
        totalChars: undefined,
        userId: undefined,
        memberId: undefined,
        expireDays: undefined
      }
    },
    handleClose() {
      this.resetForm()
      this.$emit('input', false)
      this.$emit('close')
    },
    cancel() {
      this.loading = false
      this.handleClose()
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          this.form.memberId = this.memberId
          this.form.userId = this.userId
          const platform = this.platformList.find(item => item.id === this.form.platformId)
          this.form.platformCode = platform.code

          tmMemberManageDeduct(this.form).then(response => {
            if (response.code === 200) {
              this.$message.success('操作成功')
              this.handleClose()
            } else {
              this.$message.error(response.message)
            }
          })
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>
