<!-- 预案音乐 -->
<template>
  <div class="app-container planMusic">
    <div class="page-container planMusicPage" v-if="pageType === 1">
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm"
                 :inline="true" style="width:100%;">
          <el-form-item label="" prop="searchValue">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="请输入关键词"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="" prop="musicType">
            <el-select v-model="queryParams.musicType" placeholder="请选择类型" style="width: 200px">
              <el-option label="预案音乐" :value="1"/>
              <el-option label="疏散音乐" :value="2"/>
            </el-select>
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-add-common-style-button"
             style="float: right;"
             @click="dialogOpen"
             v-hasPermiRouter="['lab:riskMusci:add']"
          >新增</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="dataList">
          <el-table-column label="名称" prop="musicName"  show-overflow-tooltip/>
          <el-table-column label="类型" prop="musicType" width="140" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.musicType == 1 ? '预案音乐' : (scope.row.musicType == 2 ? '疏散音乐' : '')}}</span>
            </template>
          </el-table-column>
          <el-table-column label="地址" prop="fileUrl" width="500" show-overflow-tooltip>
            <template slot-scope="scope">
              <span class="clickCopy" @click="clickCopy(scope.row.fileUrl)">{{scope.row.fileUrl}}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createTime" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="300" show-overflow-tooltip/>
          <el-table-column label="操作" width="120" show-overflow-tooltip v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="tableButton(1,scope.row)"
                   v-hasPermiRouter="['lab:riskMusci:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="tableButton(2,scope.row)"
                   v-hasPermiRouter="['lab:riskMusci:del']"
                >删除</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination :page-sizes="[20, 30, 40, 50]"
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <!--新增弹窗-->
    <el-dialog class="planMusicPage-dialog" :title='dialogTitle' width="540px" append-to-body
               :visible.sync="dialogType" v-if="dialogType" @close="dialogOff()"
               :close-on-click-modal="false" :close-on-press-escape="false">
      <div class="planMusicPage-dialog-box">
        <el-form :model="dialogForm" class="dialogForm" ref="dialogForm"
                 :rules="rules" style="width:100%;" label-width="90px">
          <el-form-item label="名称：" prop="musicName">
            <el-input
              maxLength="30"
              v-model="dialogForm.musicName"
              placeholder="请输入名称"
              clearable
              style="width: 360px"
            />
          </el-form-item>
          <el-form-item label="类型：" prop="musicType">
            <el-radio-group v-model="dialogForm.musicType">
              <el-radio :label="1">预案音乐</el-radio>
              <el-radio :label="2">疏散音乐</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="文件：" prop="fileUrl">
            <div>
              <el-upload
                class="certificate-avatar-uploader"
                :action="uploadImgUrl"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :headers="headers"
                :before-upload="beforeAvatarUpload">
                <p class="dialog-up-button-p">{{dialogForm.fileUrl?dialogForm.fileUrl:'上传'}}</p>
              </el-upload>
            </div>
          </el-form-item>
          <el-form-item label="说明：" prop="remark">
            <el-input
              v-model="dialogForm.remark"
              maxLength="200"
              placeholder="请输入说明"
              :show-word-limit="true"
              style="width:360px;"
              type="textarea"
              resize="none"
              :rows="2"
            />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="dialogOff()">取消</p>
        <p class="dialog-footer-button-primary" @click="dialogSubmit">提交</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import { getToken } from "@/utils/auth";
  import { laboratoryMusicList,laboratoryMusicAdd,laboratoryMusicUpdate,laboratoryMusicDelete,laboratoryMusicDetail } from "@/api/emergencyManagement/index";
  export default {
    name: 'index',
    data () {
      return {
        uploadImgUrl: this.uploadUrl(), // 上传的图片服务器地址
        headers: {
          Authorization:getToken(),
        },
        tableButtonType:this.hasPermiDom(['lab:riskMusci:edit','lab:riskMusci:del',]),
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        //下拉列表数据
        optionList:[{value:true,label:'启用'},{value:false,label:'停用'}],
        //查询条件
        queryParams:{
          page:1,
          pageSize:20,
          searchValue:"",
          musicType  :null,
        },
        //列表数据
        dataList:[],
        //数据数量
        total:0,
        //弹窗相关
        dialogTitle:null,
        dialogType:false,
        dialogForm:{},
        rules:{
          musicName:[
            { required: true, message: '请输入音乐名称', trigger: 'blur' },
            { required: true, message: "请输入音乐名称", validator: this.spaceJudgment, trigger: "blur" }
          ],
          musicType:[
            { required: true, message: '请选择音乐类型', trigger: 'blur' },
            { required: true, message: "请选择音乐类型", validator: this.spaceJudgment, trigger: "blur" }
          ],
          fileUrl:[
            { required: true, message: '请选择音乐文件', trigger: 'blur' },
            { required: true, message: "请选择音乐文件", validator: this.spaceJudgment, trigger: "blur" }
          ],
        },
      }
    },
    created () {

    },
    mounted () {
      this.getList();
    },
    methods: {
      //弹层关闭
      dialogOff(){
        this.$set(this,'dialogType',false);
      },
      //弹层开启
      dialogOpen(){
        this.dialogFormReset();
        this.$set(this,'dialogTitle','新增');
        this.$set(this,'dialogType',true);
      },
      //弹层确定
      dialogSubmit(){
        // let obj = JSON.parse(JSON.stringify(this.dialogForm))
        let obj = {
          musicName:this.dialogForm.musicName,
          musicType:this.dialogForm.musicType,
          fileId:this.dialogForm.fileId,
          fileName:this.dialogForm.fileName,
          fileUrl:this.dialogForm.fileUrl,
          remark:this.dialogForm.remark,
        }
        if(this.dialogForm.id){
          obj.id = this.dialogForm.id;
          laboratoryMusicUpdate(obj).then(response => {
            this.msgSuccess(response.message);
            this.getList();
          });
        }else{
          laboratoryMusicAdd(obj).then(response => {
            this.msgSuccess(response.message);
            this.getList();
          });
        }
        this.$set(this,'dialogType',false);
      },
      dialogFormReset(){
        this.$set(this,'dialogForm',{
          musicName:'',
          musicType:'',
          fileId:'',
          fileName:'',
          fileUrl:'',
          remark:'',
        });
      },
      //查询按钮
      handleQuery(){
        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          searchValue:"",
          musicType:null,
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        laboratoryMusicList(obj).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //操作按钮
      tableButton(type,row){
        let self = this;
        if(type == 1){
          //编辑
          this.$set(this,'dialogTitle','编辑');
          this.$set(this,'dialogForm',JSON.parse(JSON.stringify(row)));
          this.$set(this,'dialogType',true);
        }else if(type == 2){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            laboratoryMusicDelete({id:row.id}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }
      },
      //上传
      handleAvatarSuccess(res) {
        this.$set(this.dialogForm,'fileId',res.data.id);
        this.$set(this.dialogForm,'fileName',res.data.name);
        this.$set(this.dialogForm,'fileUrl',res.data.url);
        this.$forceUpdate()
      },
      beforeAvatarUpload(file) {
        let fileNameList = file.name.split(".");
        let type = false;
        if (fileNameList[fileNameList.length-1] == 'mp3') {
          type = true;
        }else{
          this.msgError('只能上传mp3格式');
          type = false;
        }
        return type;
      },
    },
  }
</script>
<style scoped lang="scss">
  .planMusic{
    .planMusicPage{

    }
  }
</style>
<style lang="scss">
  .planMusicPage-dialog{
    .dialog-up-button-p{
      padding:0 20px;
      width:360px;
      border:1px solid #dedede;
      text-align: center;
      line-height:40px;
      height:40px;
      border-radius:4px;
      color:#999;
      /*单行省略号*/
      display:block;
      overflow:hidden;
      text-overflow:ellipsis;
      white-space:nowrap;
    }
  }
</style>
