<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :rules="rules" label-width="100px" :disabled="disabled">
      <el-form-item label="有效时间：" prop="date">
        <el-date-picker v-model="form.date" type="datetimerange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="内容：" prop="audioName">
        <el-input v-model="form.audioName" placeholder="请选择内容" @focus="getDialog" />
      </el-form-item>
      <el-form-item label="封面模式：" prop="isCustom">
        <el-select v-model="form.isCustom" placeholder="请选择封面模式">
          <el-option v-for="item in coverOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.isCustom === 0" label="内容封面：" prop="pic">
        <Upload listType="picture-card" :url="form.pic" @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>
    <!-- 弹窗 -->
    <el-dialog :visible.sync="dialogVisible" title="添加内容" width="1100px">
      <el-form inline size="mini" style="width: 100%">
        <el-form-item label="资源平台：">
          <el-select v-model="dialogForm.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="音频类型：">
          <el-select v-model="dialogForm.audioType" placeholder="请选择音频类型">
            <el-option v-for="item in audioTypeOptions" :key="item.value" :value="item.value"
              :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="音频名称：">
          <el-input placeholder="请输入音频名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="getRefresh">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableData" v-loading="loading">
        <el-table-column label="内容ID" prop="audioId" align="center" show-overflow-tooltip />
        <el-table-column label="音频名称" prop="audioName" align="center" show-overflow-tooltip />
        <el-table-column label="音频封面" align="center" width="100px">
          <template slot-scope="scope">
            <el-image :src="scope.row.audioPic" />
          </template>
        </el-table-column>
        <el-table-column label="音频类型" prop="audioType" align="center" :formatter="audioTypeFormatter" />
        <el-table-column label="音频数量" prop="programCount" align="center" />
        <el-table-column label="付费类型" prop="isFree" align="center" :formatter="freeFormatter" />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="getChecked(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="dialogForm.pageNum"
        :limit.sync="dialogForm.pageSize" @pagination="getList" />
    </el-dialog>
  </div>
</template>

<script>
import Upload from '@/components/Upload/index.vue'
import { radioList } from '@/api/operation/recommend'
import { create, detail, edit } from '@/api/registration/content'
import { platformMixin, audioMixin, isFreeMixin, coverMixin } from '@/mixin/index'
export default {
  mixins: [platformMixin, audioMixin, isFreeMixin, coverMixin],
  components: {
    Upload
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        status: 0
      },
      // 弹窗
      dialogVisible: false,
      // 弹窗表单
      dialogForm: {
        pageNum: 1,
        pageSize: 10,
        platformId: 1,
        audioType: null
      },
      // 列表
      tableData: [],
      // 总数据
      total: 0,
      // 校验
      rules: {
        date: [{
          required: true, message: '请选择有效时间', trigger: 'change'
        }],
        audioName: [{
          required: true, message: '请选择内容', trigger: 'change'
        }],
        isCustom: [{
          required: true, message: '请选择封面模式', trigger: 'change'
        }],
        pic: [{
          required: true, message: '请上传内容封面', trigger: 'change'
        }]
      },
      // 只读
      disabled: false
    }
  },
  watch: {
    'dialogForm.platformId': {
      async handler(val) {
        await this.getAudioType(val)
        this.dialogForm.audioType = this.audioTypeOptions[0].value
        this.getList()
      },
      deep: true
    },
    'form.date': {
      handler(val) {
        if (val) {
          this.form.startTime = val[0]
          this.form.endTime = val[1]
        }
      },
      deep: true
    }
  },
  mounted() {
    if (this.$route.query.id) {
      this.form.id = this.$route.query.id
      this.disabled = Boolean(this.$route.query.disabled)
      this.getDetail()
    }
  },
  methods: {
    // 详情
    getDetail() {
      detail(this.form.id).then(res => {
        if (res.code === 0) {
          this.form = res.data
          this.form.date = [res.data.startTime, res.data.endTime]
        }
      })
    },

    // 打开弹窗
    async getDialog() {
      this.dialogVisible = true
      await this.getAudioType(this.dialogForm.platformId)
      this.dialogForm.audioType = this.audioTypeOptions[0].value
      this.getList()
    },

    // 音频列表
    getList() {
      this.loading = true
      radioList(this.dialogForm).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 搜索
    getSearch() {
      this.dialogForm.pageNum = 1
      this.getList()
    },

    // 重置
    getRefresh() {
      this.dialogForm = {
        pageNum: 1,
        pageSize: 10,
        audioType: this.audioTypeOptions[0].value,
        platformId: 1
      }
      this.getList()
    },

    // 选择
    getChecked(row) {
      this.form.audioId = row.audioId
      this.form.audioInfo = row.description
      this.form.audioName = row.audioName
      this.form.audioType = row.audioType
      this.form.platformId = row.platformId
      this.form.pic = row.audioPic
    },

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

    // 取消
    cancel() {
      this.$tab.closeOpenPage('/registration/contentConfig')
    },

    // 确定
    getSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          delete this.form.date
          if (this.form.id) {
            edit(this.form).then(res => {
              if (res.code === 0) {
                this.$message.success('编辑成功！')
                this.cancel()
              }
            })
          } else {
            create(this.form).then(res => {
              if (res.code === 0) {
                this.$message.success('新增成功！')
                this.cancel()
              }
            })
          }
        } else {
          return false
        }
      })
    },

    // 字典翻译
    audioTypeFormatter(row) {
      return this.selectDictLabel(this.audioOptions, row.audioType)
    },
    freeFormatter(row) {
      return this.selectDictLabel(this.freeOptions, row.isFree)
    }
  }
}
</script>

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