<!--首页配置-->
<template>
  <div class="app-container">
    <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name"/>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" clearable>
          <el-option label="图片" value="图片"/>
          <el-option label="视频" value="视频"/>
          <el-option label="语音" value="语音"/>
        </el-select>
      </el-form-item>
      <el-form-item label="所属类别" prop="category">
        <el-input v-model="queryParams.category"/>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="序号" type="index"/>
      <el-table-column label="类型" prop="type"/>
      <el-table-column label="名称" prop="name"/>
      <el-table-column label="所属类别" prop="category"/>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handlePreview(scope.row.id)">预览</el-button>
          <el-button type="text" @click="handleEdit(scope.row.id)">修改</el-button>
          <el-button type="text" @click="handleRemove(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"/>
    <el-dialog :title="title" :visible.sync="open" append-to-body width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name"/>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" clearable style="width: 100%" :disabled="editFlag">
            <el-option label="图片" value="图片"/>
            <el-option label="视频" value="视频"/>
            <el-option label="语音" value="语音"/>
          </el-select>
        </el-form-item>
        <el-form-item label="跳转链接" prop="url">
          <el-input v-model="form.url"/>
        </el-form-item>
        <el-form-item label="所属类别" prop="category">
          <el-input v-model="form.category"/>
        </el-form-item>
        <el-form-item label="文件" prop="path">
          <image-upload v-if="form.type==='图片'" v-model="form.path" :limit="1"/>
          <file-upload v-if="form.type==='视频'" v-model="form.path" :fileType="['mp4', 'avi', 'rmvb']" :limit="1"/>
          <file-upload v-if="form.type==='语音'" v-model="form.path" :fileType="['mp3','wav']" :limit="1"/>
        </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>
    <el-dialog title="预览" :visible.sync="previewOpen" append-to-body width="700px" @close="reset">
      <image-preview v-if="form.type==='图片'" :src="form.path"/>
      <video-player v-if="form.type==='视频'" :src="form.path"/>
      <audio-player v-if="form.type==='语音'" :src="form.path"></audio-player>
    </el-dialog>
  </div>
</template>
<script>
import {add, detail, edit, page, remove} from '@/api/liandao/resources.js'

export default {
  name: "liandaoResources",
  created() {
    this.getList();
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: '',
        name: '',
        category:'',
      },
      //表格数据
      tableData: [],
      // 总数
      total: 0,
      // 表单数据
      form: {},
      // 弹出层标题
      title: '',
      open: false,
      rules: {
        name: [{required: true, message: "名称不能为空", trigger: "blur"},],
        type: [{required: true, message: "类型不能为空", trigger: "blur"},],
        path: [{required: true, message: "文件不能为空", trigger: "blur"},],
      },
      editFlag: false,
      previewOpen: false
    }
  },
  methods: {
    /** 查询数据列表 */
    getList() {
      this.loading = true;
      page(this.queryParams).then(resp => {
          if (resp.code == 200) {
            this.tableData = resp.rows;
            this.total = resp.total;
            this.loading = false;
          }
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.editFlag = false;
      this.form = {
        id: undefined,
        type: undefined,
        name: undefined,
        path: undefined,
        url: undefined,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleEdit(id) {
      this.reset();
      this.editFlag = true;
      detail(id).then(resp => {
        if (resp.code === 200) {
          this.form = {...resp.data}
          this.open = true;
          this.title = "修改";
        }
      })
    },
    handleRemove(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(id).then(resp => {
          if (resp.code === 200) {
            this.$message.success('删除成功！')
            this.resetQuery()
          }
        })
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            edit(this.form).then(resp => {
              if (resp.code === 200) {
                this.$message.success("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.$message.error(resp.msg);
              }
            });
          } else {
            add(this.form).then(resp => {
              if (resp.code === 200) {
                this.$message.success("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.$message.error(resp.msg);
              }
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handlePreview(id){
      this.reset();
      detail(id).then(resp => {
        if (resp.code === 200) {
          this.form = {...resp.data}
          this.previewOpen = true;
        }
      })
    }
  }
}
</script>

