<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="请输入节目名称" />
      </el-form-item>
      <el-form-item label="节目介绍：" prop="description">
        <el-input v-model="form.description" type="textarea" :autosize="{ minRows: 5, maxRows: 10}"
          placeholder="请输入节目介绍" />
      </el-form-item>
      <el-form-item label="资源平台：" prop="platformId">
        <el-select v-model="form.platformId" placeholder="请选择资源平台">
          <el-option v-for="item in platformOptions" :key="item.value" :value="item.value"
            :label="item.label" />
        </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 v-model="form.price" placeholder="请输入原价" />
      </el-form-item>
      <el-form-item v-if="form.isFree === 0" label="折扣价：" prop="discountedPrice">
        <el-input v-model="form.discountedPrice" placeholder="请输入折扣价" />
      </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="fullUrl">
        <Upload listType="audio" @upload="upload($event, 'fullUrl')" />
        <div style="color:#909399">单个上传音频文件不超过500MB</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'
import Upload from "@/components/Upload/index"
export default {
  mixins: [platformMixin, isFreeMixin],
  components: {
    Upload,
  },
  data() {
    // 小数点后两位 正则
    var str = /^\d+.?\d{0,2}$/
    // 判断原价是否大于折扣价
    var checkPrice = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入原价'))
      } else if (!str.test(value)) {
        return callback(new Error('小数点后最多输入两位'))
      } else if (this.form.discountedPrice) {
        Number(value) > Number(this.form.discountedPrice) ? this.$refs.form.clearValidate('discountedPrice') : callback(new Error('原价必须大于折扣价'))
      }
    }
    // 判断折扣价是否小于原价
    var checkDiscount = (rule, value, callback) => {
      if (value) {
        if (!str.test(value)) {
          return callback(new Error('小数点后最多输入两位'))
        } else if (this.form.price) {
          Number(value) < Number(this.form.price) ? this.$refs.form.clearValidate('price') : callback(new Error('折扣价必须小于原价'))
        }
      }
    }
    return {
      // 音频类型
      audioType: 6,
      // 表单
      form: {
        status: 1
      },
      // 校验
      rules: {
        name: [{
          required: true, message: '请输入节目名称', trigger: 'blur'
        }],
        description: [{
          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'
        }],
        fullUrl: [{
          required: true, message: '请上传音频文件', trigger: 'change'
        }]
      },
      // 只读
      disabled: false
    };
  },
  mounted() {
    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
      }
    },

    // 确定
    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;
}
</style>