<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :rules="rules" label-width="100px" :disabled="disabled">
      <el-form-item label="歌手名称：" prop="name">
        <el-input v-model="form.name" placeholder="请输入歌手名称" show-word-limit />
      </el-form-item>
      <el-form-item label="歌手性别：" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择歌手性别">
          <el-option v-for="item in sexOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属地区：" prop="singerArea">
        <el-select v-model="form.singerArea" placeholder="请选择歌手所属地区">
          <el-option v-for="item in regionOptions" :key="item.value" :value="item.value"
            :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="资源平台：" prop="platformId">
        <el-select v-model="form.platformId" placeholder="请选择资源平台" :disabled="disabledPlatformId(form.platformId)">
          <el-option v-for="item in platformOptions" :key="item.value" :value="item.value"
            :label="item.label" :disabled="disabledJoinType(item.joinType)" />
        </el-select>
      </el-form-item>
      <el-form-item label="歌手简介：" prop="description">
        <el-input v-model="form.description" type="textarea" rows="5" placeholder="请输入歌手简介" maxlength="100"
          show-word-limit />
      </el-form-item>
      <el-form-item label="歌手头像：" prop="coverUrl">
        <Upload listType="picture-card" :url="form.coverUrl" @upload="upload" :disabled="disabled" />
      </el-form-item>
    </el-form>
    <div class="form-btn">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="getSubmit" v-if="!disabled">确定</el-button>
    </div>
  </div>
</template>

<script>
import { detail, submit } from '@/api/music/singer'
import { sexMixin, regionMixin, platformMixin } from '@/mixin/index'
export default {
  mixins: [sexMixin, regionMixin, platformMixin],
  data() {
    return {
      // 表单
      form: {},
      // 校验
      rules: {
        name: [{
          required: true, message: '请输入歌手名称', trigger: 'blur'
        }],
        sex: [{
          required: true, message: '请选择歌手性别', trigger: 'change'
        }],
        singerArea: [{
          required: true, message: '请选择歌手所属地区', trigger: 'change'
        }],
        platformId: [{
          required: true, message: '请选择资源平台', trigger: 'change'
        }],
        status: [{
          required: true, message: '请选择当前状态', trigger: 'change'
        }],
        coverUrl: [{
          required: true, message: '请上传歌手头像', trigger: 'change'
        }]
      },
      // 是否只读
      disabled: false
    }
  },
  mounted() {
    // 获取资源平台
    this.getPlatform({
      audioType: 9
    })
    if (this.$route.query.id) {
      this.form.id = this.$route.query.id
      this.disabled = Boolean(this.$route.query.disabled)
      this.getList()
    }
  },
  methods: {
    // 详情
    getList() {
      detail(this.form.id).then(res => {
        if (res.code === 0) {
          this.form = res.data
        }
      })
    },

    // 上传
    upload(e) {
      this.form.coverUrl = e.file
    },

    // 新增
    getSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let title = this.form.id ? '编辑成功！' : '新增成功！'
          submit(this.form).then(res => {
            if (res.code === 0) {
              this.$message.success(`${title}`)
              this.cancel()
            }
          })
        } else {
          return false
        }
      })
    },

    // 取消
    cancel() {
      this.$tab.closeOpenPage("/music/singer");
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form {
  width: 500px;
}
</style>