<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :rules="rules" label-width="100px" :disabled="disabled">
      <el-form-item label="主播名称：" prop="nickname">
        <el-input v-model="form.nickname" placeholder="请输入主播名称" />
      </el-form-item>
      <el-form-item label="主播简介：" prop="description">
        <el-input v-model="form.description" type="textarea" rows="5" maxlength="100" show-word-limit
          placeholder="请输入主播简介" />
      </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="avatar">
        <Upload listType="picture-card" :url="form.avatar" @upload="upload" :disabled="disabled" />
      </el-form-item>
    </el-form>
    <div class="form-btn">
      <el-button @click="cancel">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="getSubmit">确定</el-button>
    </div>
  </div>
</template>

<script>
import { submit, detail } from '@/api/music/anchor'
import { platformMixin } from '@/mixin/index'
export default {
  mixins: [platformMixin],
  data() {
    return {
      // 表单
      form: {
        status: 1
      },
      // 校验
      rules: {
        nickname: [{
          required: true, message: '请输入主播名称', trigger: 'blur'
        }],
        description: [{
          required: true, message: '请输入主播简介', trigger: 'blur'
        }],
        platformId: [{
          required: true, message: '请选择资源平台', trigger: 'change'
        }],
        avatar: [{
          required: true, message: '请上传主播头像', trigger: 'change'
        }]
      },
      // 只读
      disabled: false
    }
  },
  mounted() {
    // 获取资源平台
    this.getPlatform({
      audioType: 12
    })
    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.avatar = 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/anchor')
    }
  }
}
</script>

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