<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="成果分类" prop="achievementCategory">
        <el-input
          v-model="queryParams.achievementCategory"
          placeholder="请输入成果分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="研究领域" prop="researchField">
        <el-input
          v-model="queryParams.researchField"
          placeholder="请输入研究领域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布人" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入发布人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </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="['achievement: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="['achievement: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="['achievement:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="achievementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" width="80" />
      <el-table-column label="缩略图" align="center" prop="thumbnail" width="100">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.thumbnail"
            :src="scope.row.thumbnail"
            :preview-src-list="[scope.row.thumbnail]"
            style="width: 60px; height: 60px"
            fit="cover"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="成果名称"
        align="center"
        prop="achievementName"
        :show-overflow-tooltip="true"
        min-width="150"
      />
      <el-table-column label="成果分类" align="center" prop="achievementCategory" width="120" />
      <el-table-column label="研究领域" align="center" prop="researchField" width="120" />
      <el-table-column label="研究单位" align="center" prop="researchUnit" width="150" :show-overflow-tooltip="true" />
      <el-table-column label="研究人员" align="center" prop="researchers" width="150" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '0' ? 'success' : 'danger'">
            {{ scope.row.status === '0' ? '正常' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" width="80" />
      <el-table-column label="发布人" align="center" prop="publisher" width="100" />
      <el-table-column label="发布时间" align="center" prop="publishTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['achievement:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['achievement: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="成果名称" prop="achievementName">
              <el-input v-model="form.achievementName" placeholder="请输入成果名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="成果分类" prop="achievementCategory">
              <el-input v-model="form.achievementCategory" placeholder="请输入成果分类" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="研究领域" prop="researchField">
              <el-input v-model="form.researchField" placeholder="请输入研究领域" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="研究单位" prop="researchUnit">
              <el-input v-model="form.researchUnit" placeholder="请输入研究单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="研究人员" prop="researchers">
              <el-input v-model="form.researchers" placeholder="请输入研究人员" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布人" prop="publisher">
              <el-input v-model="form.publisher" placeholder="请输入发布人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布时间" prop="publishTime">
              <el-date-picker
                v-model="form.publishTime"
                type="datetime"
                placeholder="选择发布时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input-number v-model="form.sort" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio label="0">正常</el-radio>
                <el-radio label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="缩略图">
              <image-upload v-model="form.thumbnail" :limit="1"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="简介">
              <editor v-model="form.introduction" :min-height="150" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="详情">
              <editor v-model="form.details" :min-height="200" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" :rows="2" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!isView">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAchievement, getAchievement, delAchievement, addAchievement, updateAchievement } from "@/api/achievement"

export default {
  name: "Achievement",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      achievementList: [],
      title: "",
      open: false,
      isView: false,
      dateRange: [],
      baseUrl: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        achievementName: undefined,
        achievementCategory: undefined,
        researchField: undefined,
        publisher: undefined,
        status: undefined
      },
      form: {},
      rules: {
        achievementName: [
          { required: true, message: "成果名称不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.baseUrl = process.env.VUE_APP_BASE_API
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (this.dateRange && this.dateRange.length === 2) {
        this.queryParams.params['beginTime'] = this.dateRange[0]
        this.queryParams.params['endTime'] = this.dateRange[1]
      }
      listAchievement(this.queryParams).then(response => {
        this.achievementList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        id: undefined,
        achievementName: undefined,
        thumbnail: undefined,
        achievementCategory: undefined,
        researchField: undefined,
        researchUnit: undefined,
        researchers: undefined,
        introduction: undefined,
        details: undefined,
        publishTime: undefined,
        publisher: undefined,
        sort: 0,
        status: "0",
        remark: undefined
      }
      this.resetForm("form")
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.dateRange = []
      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.isView = false
      this.title = "添加产出成果"
    },
    handleView(row) {
      this.reset()
      const id = row.id || this.ids
      getAchievement(id).then(response => {
        this.form = response.data
        this.open = true
        this.isView = true
        this.title = "查看产出成果详情"
      })
    },
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAchievement(id).then(response => {
        this.form = response.data
        this.open = true
        this.isView = false
        this.title = "修改产出成果"
      })
    },
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateAchievement(this.form).then(() => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addAchievement(this.form).then(() => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除产出成果编号为"' + ids + '"的数据项？').then(function() {
        return delAchievement(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    }
  }
}
</script>
