<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="节目介绍：">
        <el-input v-model="form.description" type="textarea" :autosize="{ minRows: 5, maxRows: 10}"
          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="isFree">
        <el-select v-model="form.isFree" placeholder="请选择付费类型">
          <el-option v-for="item in freeOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.isFree === 0" label="原价：" prop="price">
        <el-input-number v-model="form.price" placeholder="请输入原价" :min="1" :precision="2" :controls="false" />
      </el-form-item>
      <el-form-item v-if="form.isFree === 0" label="折扣价：" prop="discountedPrice">
        <el-input-number v-model="form.discountedPrice" placeholder="请输入折扣价" :min="1" :precision="2"
          :controls="false" />
      </el-form-item>
      <el-form-item label="节目封面：" prop="cover">
        <Upload listType="picture-card" :url="form.cover" @upload="upload($event, 'cover')"
          :disabled="disabled" />
      </el-form-item>
      <el-form-item label="音频文件：" prop="realName">
        <el-input v-model="form.realName" readonly>
          <template slot="suffix">
            <Upload listType="audio" :size="100" @upload="upload($event, 'fullUrl')" />
          </template>
        </el-input>
        <div style="color:#909399">单个上传音频文件不超过100MB</div>
      </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/program'
import { platformMixin, isFreeMixin } from '@/mixin/index'
export default {
  mixins: [platformMixin, isFreeMixin],
  data() {
    // 小数点后两位 正则
    var str = /^\d+.?\d{0,2}$/
    // 判断原价是否大于折扣价
    var checkPrice = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入原价'))
      } else if (value.toString().length > 10) {
        callback(new Error('字符长度不超过10个'))
      } else if (this.form.discountedPrice) {
        value > this.form.discountedPrice ? callback() : callback(new Error('原价必须大于折扣价'))
      }
    }
    // 判断折扣价是否小于原价
    var checkDiscount = (rule, value, callback) => {
      if (value) {
        if (value.toString().length > 10) {
          callback(new Error('字符长度不超过10个'))
        } else if (this.form.price) {
          value < this.form.price ? callback() : callback(new Error('折扣价必须小于原价'))
        }
      }
    }
    return {
      // 表单
      form: {
        status: 1,
        realName: ''
      },
      // 校验
      rules: {
        name: [{
          required: true, message: '请输入节目名称', trigger: 'blur'
        }],
        platformId: [{
          required: true, message: '请选择资源平台', trigger: 'change'
        }],
        isFree: [{
          required: true, message: '请选择付费类型', trigger: 'change'
        }],
        price: [{
          required: true, validator: checkPrice, trigger: 'blur'
        }],
        discountedPrice: [{
          validator: checkDiscount, trigger: 'blur'
        }],
        cover: [{
          required: true, message: '请上传节目封面', trigger: 'change'
        }],
        realName: [{
          required: true, message: '请上传音频文件', trigger: 'blur, change'
        }]
      },
      // 只读
      disabled: false
    };
  },
  mounted() {
    // 获取资源平台
    this.getPlatform({
      audioType: 6
    })
    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, key) {
      if (key === 'cover') {
        this.form.cover = e.file
      } else {
        this.form.fullUrl = e.file.url
        this.form.fullDuration = e.file.time
        this.form.realName = e.file.realName
      }
    },

    // 确定
    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/program");
    },
  },
};
</script>

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

::v-deep .el-input__suffix {
  right: 0;
}
</style>