<template>
  <el-dialog :title="(modelType == 'add' ? '添加' : '编辑') + '音乐类目信息'" width="800px" :visible.sync="dialogVisible">
    <el-form :model="form" :rules="rules" ref="MusicLibraryForm" label-width="150px">
      <el-form-item prop="musicCategoryName" label="音乐类目名称:">
        <el-input placeholder="请输入音乐类目名称" v-model="form.musicCategoryName"></el-input>
      </el-form-item>
      <el-form-item prop="musicCategorySort" label="类目排序:">
        <el-input-number v-model="form.musicCategorySort" controls-position="right" :min="0"></el-input-number>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" :loading="comfirmLoading" @click="handleConfirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addMusicConfig, editMusicConfig } from "@/api/musicLibraryManager";
export default {
  name: "MusicLibraryFormModal",
  props: {
  },
  data() {
    return {
      form: {
        musicCategoryName: '',
        musicCategorySort: ''
      },
      modelType: 'add',
      dialogVisible: false,
      comfirmLoading: false,
      editId: '',
      rules: {
        musicCategoryName: [
          { required: true, message: "请输入音乐类目名称", trigger: "blur" }
        ],
        musicCategorySort: [
          { required: true, message: "请输入类目排序", trigger: "blur" }
        ]
      }
    };
  },
  watch: {
    dialogVisible(val) {
      if (!val) {
        this.form = {
          musicCategoryName: '',
          musicCategorySort: ''
        }
        this.modelType =  'add';
        this.comfirmLoading = false;
        this.editId = '';
      }
    }
  },
  methods: {
    show(rows, type) {
      this.dialogVisible = true;
      this.comfirmLoading = false;
      this.modelType = type;
      if (type == 'edit') {
        this.editId = rows.id;
      }
      this.$nextTick(() => {
        this.form = type == 'edit' ? {
          musicCategoryName: rows.musicCategoryName,
          musicCategorySort: rows.musicCategorySort
        } : {
          musicCategoryName: '',
          musicCategorySort: ''
        }
        this.$refs.MusicLibraryForm.resetFields()
      })
    },
    handleConfirm() {
      this.$refs.MusicLibraryForm.validate(valid => {
        if (valid) {
          this.comfirmLoading = true;
          const params = this.form;
          if (this.modelType == 'add') {
            addMusicConfig(params).then(res =>{
              this.dialogVisible = false;
              if(res.code == 200){
                this.$message.success(res.msg || "操作成功");
                this.$emit("confirm");
              }
            }).finally(()=>{
              this.comfirmLoading = false;
            })
          } else {
            params.id = this.editId;
            editMusicConfig(params).then(res =>{
              this.dialogVisible = false;
              if(res.code == 200){
                this.$message.success(res.msg || "操作成功");
                this.$emit("confirm");
              }
            }).finally(()=>{
              this.comfirmLoading = false;
            })
          }
          
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss">
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    display: block;
  }
  .avatar-black {
    width: 180px;
    height: 180px;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    i {
      position: absolute;
      top: -10px;
      right: -10px;
      cursor: pointer;
      z-index: 999999;
      color: red;
      background-color: #fff;
      font-size: 30px;
      border-radius: 50%;
      outline: none;
      width: 28px;
      height: 28px;
      line-height: 28px;
      font-size: 28px;
      color: #8c939d;
      display: block;
      text-align: center;
    }
    img {
      max-height: 100%;
      vertical-align: middle;
    }
  }
</style>


