<template>
  <div>
    <el-dialog :title="exData.id ? '编辑': '新增'"
               :visible.sync="dialog"
               width="50%"
               :close-on-click-modal="false"
               top="50px">
      <el-form ref="form"
               :model="form"
               :rules="formRules"
               label-width="160px">
        <el-form-item prop="song_name"
                      :rules="formRules.required"
                      label="歌曲名称：">
          <el-col :span="16">
            <el-input v-model="form.song_name"
                      placeholder="请输入歌曲名称"
                      clearable></el-input>
          </el-col>
        </el-form-item>
        <el-form-item :rules="formRules.required"
                      prop="song_auth_name"
                      label="原唱：">
          <el-col :span="16">
            <el-input v-model="form.song_auth_name"
                      placeholder="请输入原唱"
                      clearable></el-input>
          </el-col>
        </el-form-item>
        <el-form-item prop="song_status"
                      :rules="formRules.required"
                      label="歌曲状态：">
          <el-radio v-model="form.song_status" label="1">有效</el-radio>
          <el-radio v-model="form.song_status" label="0">无效</el-radio>
        </el-form-item>
        <el-form-item prop="song_price"
                      :rules="formRules.numPot2"
                      label="单价(元)：">
          <el-col :span="16">
            <el-input v-model="form.song_price"
                      placeholder="请输入单价"
                      @input="form.song_price=form.song_price.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')"
                      clearable></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer text-center">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary" :disabled="booLock" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fen2Yuan, yuan2Fen } from '@/utils'

export default {
  components: {},
  props: {
    value: {
      type: Boolean,
      default: true
    },
    exData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      dialog: !!this.value,
      form: {
        user_id: '', // 用户ID
        song_name: '', // 歌曲名称
        song_auth_name: '', // 歌曲作者
        song_status: '1', // 歌曲状态(0无效1有效)
        song_price: '' // 歌曲价格
      },
      booLock: false
    }
  },
  methods: {
    handleSubmit () {
      const url = this.exData.id ? '/v1/user/song/modify' : '/v1/user/song/add'
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.booLock = true
          const data = await this.$fetch(url, {
            ...this.form,
            song_price: yuan2Fen(this.form.song_price)
          })
          this.booLock = false
          if (data.code === 200) {
            this.$message.success('提交成功')
            this.$emit('success')
            this.dialog = false
          }
        }
      })
    }
  },
  mounted () {
    this.$set(this.form, 'user_id', this.$route.query.id)
    if (this.exData.id) {
      this.$set(this.form, 'id', this.exData.id)
      for (const key in this.exData) {
        if (this.form.hasOwnProperty(key)) {
          let value = this.exData[key]
          if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') {
            if (key === 'song_price') {
              value = fen2Yuan(value)
            }
            if (key === 'song_status') {
              value = value.toString()
            }
            this.$set(this.form, key, value)
          }
        }
      }
    }
  },
  watch: {
    dialog (val) {
      if (!val) this.$emit('input', val)
    }
  }
}
</script>

<style lang="scss" scoped>
.top-tip {
  margin-top: -20px;
  margin-bottom: 20px;
}
</style>
