<template>
  <div class="app-container base-periodical-index">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="期刊名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入期刊名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="期刊文件" prop="fileUrl">
        <el-input v-model="queryParams.fileUrl" placeholder="请输入期刊文件" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @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 type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['base:periodical:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['base:periodical:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['base:periodical:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5" v-if="1==2">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['base:periodical:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="periodicalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" width="55" />
      <el-table-column label="期刊名称" align="center" prop="name" />
      <el-table-column label="期刊主图" align="center" prop="imageUrl">
        <template slot-scope="{row}">
          <el-image :src="$basepath + row.imageUrl + '.min.jpg'" :preview-src-list="[row.imageUrl]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="期刊文件" align="center" prop="fileUrl">
        <template slot-scope="{row}">{{ row.fileName }}</template>
      </el-table-column>
      <el-table-column label="顺序" align="center" prop="ord" width="200">
        <template slot-scope="{row}">
          <el-input-number v-model="row.ord" controls-position="right" @change="doSave(row, false)" :min="1"
            :max="10000"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['base:periodical:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['base:periodical:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination 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 custom-class="base-periodical-dlg"
      :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="期刊名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入期刊名称" />
        </el-form-item>
        <el-form-item label="期刊主图" ref="fiImage" prop="imageUrl"
          :class="'form-img-' + (form.imageUrl ? form.imageUrl.length : 0)">
          <el-image :src="$basepath + form.imageUrl[0].url" v-if="form.imageUrl && form.imageUrl.length > 0"></el-image>
          <FileUpload @input="doImageInput" v-model="form.imageUrl" :fileSize="5" :limit="1"
            :fileType="['png', 'jpg', 'jpeg']" />
        </el-form-item>
        <el-form-item label="期刊文件" ref="fiFile" prop="fileUrl"
          :class="'form-pdf-' + (form.fileUrl ? form.fileUrl.length : 0)">
          <FileUpload @input="doFileInput" v-model="form.fileUrl" :fileSize="100" :limit="1"
            :fileType="['pdf', 'docx', 'doc']" />
        </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 { listPeriodical, getPeriodical, delPeriodical, addPeriodical, updatePeriodical } from "@/api/base/periodical";

export default {
  name: "Periodical",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 期刊表格数据
      periodicalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        imageUrl: null,
        fileUrl: null,
        ord: null,
        state: null,
        isDel: 0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, trigger: "blur", message: "请输入期刊名称" }],
        imageUrl: [{ required: true, trigger: "change", message: "请上传图片" }],
        fileUrl: [{ required: true, trigger: "change", message: "请上传期刊文件" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    doImageInput() {
      this.$refs.fiImage.clearValidate()
    },
    doFileInput() {
      this.$refs.fiFile.clearValidate()
    },
    /** 查询期刊列表 */
    getList() {
      this.loading = true;
      listPeriodical(this.queryParams).then(response => {
        this.periodicalList = (response.rows).map(it=>{
          it.fileUrl=JSON.parse(it.fileUrl);
          it.fileName=it.fileUrl[0].url.split("/").pop();
          return it;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        imageUrl: null,
        fileUrl: null,
        ord: null,
        state: null,
        remark: null,
        isDel: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      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
      getPeriodical(id).then(response => {
        let obj = response.data;
        obj.imageUrl = [{ name: obj.imageUrl, url: obj.imageUrl }];        
        obj.fileUrl=JSON.parse(obj.fileUrl);
        this.form = obj;
        this.open = true;
        this.title = "修改期刊";
      });
    },
    doSave(row) {
      let obj={...row};
      obj.fileUrl=JSON.stringify(obj.fileUrl);
      updatePeriodical(obj).then(response => {
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let data = {
            name: this.form.name,
            isDel: 0,
            ord: 1,
            remark: '',
            state: 0,
            fileUrl: JSON.stringify(this.form.fileUrl),
            imageUrl: this.form.imageUrl[0].url
          };
          if (this.form.id != null) {
            data.id = this.form.id;
            updatePeriodical(data).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPeriodical(data).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除期刊编号为"' + ids + '"的数据项？').then(function () {
        return delPeriodical(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/periodical/export', {
        ...this.queryParams
      }, `periodical_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss">
.base-periodical-index {
  .el-input-number--medium {
    width: 160px;
  }
}

.base-periodical-dlg {
  .el-form {
    .el-form-item--medium {
      margin-bottom: 16px;
    }

    .upload-file {
      .el-upload__tip {
        margin-top: 0px;
      }

      .upload-file-uploader {
        margin-bottom: 0px;
      }
    }

    .form-img-1 {
      .upload-file {
        .el-upload--text {
          display: none;
        }

        .el-upload__tip {
          display: none;
        }
      }
    }

    .form-pdf-1 {
      .upload-file {
        .el-upload--text {
          display: none;
        }

        .el-upload__tip {
          display: none;
        }
      }
    }

    .el-upload-list__item {
      .el-link--default {
        width: calc(100% - 40px);
        justify-content: left;

        .el-icon-document {
          line-height: 20px;
        }
      }
    }
  }
}</style>
