<template>
  <div class="app-container syntheticMusic">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item style="float: right;">
        <el-col :span="1.5" v-hasPermi="['laboratory:synthesis:add']">
          <p class="add-button-one-90"
             @click="handleAdd"
          ><i class="el-icon-plus"></i>新增</p>
        </el-col>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="synthesisList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="音乐地址" align="center" prop="musicUrl" />
      <el-table-column label="音量大小" align="center" prop="musicVolume" />
      <el-table-column label="背景音乐地址" align="center" prop="bgmusicUrl" />
      <el-table-column label="背景音乐大小" align="center" prop="bgmusicVolume" />
      <el-table-column label="合成音乐新地址" align="center" prop="newMusicUrl" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160" v-if="tableButtonType">
        <template slot-scope="scope">
          <div class="button-box">
            <p class="table-min-button"
               style="margin-right:10px;"
               @click="handleUpdate(scope.row)"
               v-hasPermiAnd="['laboratory:synthesis:query','laboratory:synthesis:edit']"
            >修改</p>
            <p class="table-min-button"
               @click="handleDelete(scope.row)"
               v-hasPermi="['laboratory:synthesis:remove']"
            >删除</p>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination :page-sizes="[20, 30, 40, 50]"
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改音乐合成对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="预案" prop="riskPlanId">
          <el-select v-model="form.riskPlanId" placeholder="请选择预案" clearable style="width:350px;">
            <el-option
              v-for="dict in riskPlanList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="音乐地址" prop="musicUrl">
          <el-input v-model="form.musicUrl" placeholder="请上传音乐" disabled>
            <template slot="append">
              <el-upload
                :action="uploadImgUrl"
                :show-file-list="false"
                accept="audio/mpeg"
                :on-success="(res, file)=>handleAvatarSuccess(res, file,1)"
                :headers="headers"
                :before-upload="beforeAvatarUpload">
                <p style="margin:0;">上传</p>
              </el-upload>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="音量大小" prop="musicVolume">
          <el-slider :min="0.1" :max="1" v-model="form.musicVolume" :step="0.1"></el-slider>
        </el-form-item>
        <el-form-item label="背景音乐地址" prop="bgmusicUrl">
          <el-input v-model="form.bgmusicUrl" placeholder="请上传背景音乐" disabled>
            <template slot="append">
              <el-upload
                :action="uploadImgUrl"
                :show-file-list="false"
                accept="audio/mpeg"
                :on-success="(res, file)=>handleAvatarSuccess(res, file,2)"
                :headers="headers"
                :before-upload="beforeAvatarUpload">
                <p style="margin:0;">上传</p>
              </el-upload>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="背景音乐大小" prop="bgmusicVolume">
          <el-slider :min="0.1" :max="1" v-model="form.bgmusicVolume" :step="0.1"></el-slider>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSynthesis, getSynthesis, delSynthesis, addSynthesis, updateSynthesis ,listRiskPlan } from "@/api/laboratory/synthesis";
import { getToken } from "@/utils/auth";
export default {
  name: "Synthesis",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['exam:el_classify:query','exam:el_classify:edit','exam:el_classify:remove']),
      uploadImgUrl: window.location.href.split('://')[0]+'://' + process.env.VUE_APP_BASE_API + "/file/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 音乐合成表格数据
      synthesisList: [],
      riskPlanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize:20,
        musicUrl: null,
        musicVolume: null,
        bgmusicUrl: null,
        bgmusicVolume: null,
        newMusicUrl: null,
      },
      // 表单参数
      form: {
        musicUrl:"",
        musicVolume:5,
        bgmusicUrl:"",
        bgmusicVolume:5,
      },
      // 表单校验
      rules: {
        riskPlanId: [
          { required: true, message: "请选择预案", trigger: "blur" },
        ],
        musicUrl: [
          { required: true, message: "请上传音乐", trigger: "blur" },
        ],
        bgmusicUrl: [
          { required: true, message: "请上传背景音乐", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getRiskPlanList();
  },
  methods: {
    /** 查询预案列表 */
    getRiskPlanList()
    {
      listRiskPlan().then(response => {
        // this.deptOptions = response.data;
        this.$set(this, 'riskPlanList', response.data)
      });
    },
    //上传
    handleAvatarSuccess(res, file, type) {
      // console.log("res",res);
      // console.log("file",file);
      // console.log("type",type);
      if(type == 1){
        this.$set(this.form,'musicUrl',res.data.url);
      }else if(type == 2){
        this.$set(this.form,'bgmusicUrl',res.data.url);
      }
    },
    beforeAvatarUpload(file) {
      let type = false;
      console.log('file',file);
      if (file.type == 'audio/mpeg') {
        type = true;
      }else{
        this.$message.error('只能上传mp3格式');
        type = false;
      }
      return type;
    },
    /** 查询音乐合成列表 */
    getList() {
      this.loading = true;
      listSynthesis(this.queryParams).then( response => {
        this.synthesisList =  response.rows;
        this.total =  response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        musicUrl:"",
        musicVolume:0.5,
        bgmusicUrl:"",
        bgmusicVolume:0.5,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加音乐合成";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSynthesis(id).then( response => {
        this.form =  response.data;
        this.open = true;
        this.title = "修改音乐合成";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSynthesis(this.form).then( response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSynthesis(this.form).then( response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除音乐合成编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSynthesis(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/synthesis/export', {
        ...this.queryParams
      }, `laboratory_synthesis.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
  .syntheticMusic{
    padding:20px!important;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    .button-box{
      margin:0 auto;
      width:190px;
      display: flex;
    }
  }
</style>
