<template>
  <div class="app-warp">
    <div class="app-structure">
      <div class="app-search">
        <el-form class="app-search-form">
          <div class="form-item">
            <label> 更新内容：</label>
            <el-input v-model="KEYWORDS" placeholder="搜索更新内容" clearable @clear="getQuery"/>
          </div>
          <div class="form-item text-right">
            <el-button v-waves type="primary" icon="el-icon-search" @click="getQuery">
              搜索
            </el-button>
            <el-button v-waves icon="el-icon-refresh" @click="goKeyReset">
              重置
            </el-button>
          </div>
        </el-form>
      </div>
      <div class="app-tools">
        <div class="app-function">
          <el-button v-show="add" type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
          <el-button v-show="del" type="danger" icon="el-icon-delete" plain @click="batchDel">批量删除</el-button>
        </div>
      </div>
      <div class="app-container">
        <el-table
          v-loading="listLoading"
          ref="multipleTable"
          :data="varList"
          :row-key="getRowKey"
          :header-cell-style="{
            background:'#f5f7fa',
            'font-weight': 'bold',
            'color': '#000'
          }"
          tooltip-effect="dark"
          border
          fit
          highlight-current-row
        >
          <el-table-column
            :reserve-selection="true"
            type="selection"
            width="55"
            align="center"/>
          <el-table-column type="index" label="序号" width="50" align="center" />
          <el-table-column prop="VERSION" label="版本" />
          <el-table-column prop="LOGINFO" label="更新内容" ><template slot-scope="scope"><span v-html="scope.row.LOGINFO"/></template></el-table-column>
          <el-table-column prop="CREATOR" label="添加人" />
          <el-table-column prop="CREATTIME" label="添加时间" />
          <el-table-column prop="OPERATOR" label="修改人" />
          <el-table-column prop="OPERATTIME" label="修改时间" />
          <el-table-column label="操作" align="center" width="150">
            <template slot-scope="{row}">
              <el-button v-show="edit" type="text" @click="handleEdit(row.VERSIONLOG_ID)">编辑</el-button>
              <span class="app-divider"/>
              <el-button v-show="del" type="text" class="color-red" @click="handleDelete(row.VERSIONLOG_ID)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page-btn-group">
        <div/>
        <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
      </div>
    </div>

    <el-dialog :visible.sync="dialogFormEdit" :title="dialogType==='edit'?'修改':'新增'" width="600px">
      <el-form ref="form" :rules="rules" :model="form" label-width="110px" style="width: 500px;">
        <el-form-item label="版本" prop="VERSION">
          <el-input id="VERSION" ref="VERSION" v-model="form.VERSION" maxlength="255" placeholder="这里输入版本..." title="版本"/>
        </el-form-item>
        <el-form-item label="更新内容" prop="LOGINFO">
          <el-input id="LOGINFO" ref="LOGINFO" v-model="form.LOGINFO" type="textarea" maxlength="255" placeholder="这里输入更新内容..." title="更新内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormEdit = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves'
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      dialogType: 'add',
      dialogFormEdit: false,
      varList: [],
      rules: {
        VERSION: [{ required: true, message: '版本不能为空', trigger: 'blur' }],
        LOGINFO: [{ required: true, message: '更新内容不能为空', trigger: 'blur' }]
      },
      form: {
        VERSIONLOG_ID: '', // 版本id
        VERSION: '', // 版本
        LOGINFO: '' // 更新内容
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getRowKey(row) {
      return row.VERSIONLOG_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.getQuery()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/versionlog/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.dialogType = 'add'
      this.resetForm()
      this.dialogFormEdit = true
    },
    // 修改
    handleEdit(ID) {
      this.dialogType = 'edit'
      requestFN(
        '/versionlog/goEdit',
        {
          VERSIONLOG_ID: ID
        }
      ).then((data) => {
        this.form = Object.assign({}, data.pd) // copy obj
        this.form.LOGINFO = this.form.LOGINFO.replace(/<br>/g, '\r\n')
      }).catch((e) => {
        this.listLoading = false
      })
      this.dialogFormEdit = true
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/versionlog/delete',
          {
            VERSIONLOG_ID: id
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 保存
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.listLoading = true
          this.form.LOGINFO = this.form.LOGINFO.replace(/\r\n/g, '<br>').replace(/\n/g, '<br>').replace(/\s/g, ' ') // 转换格式
          requestFN(
            '/versionlog/' + this.dialogType,
            this.form
          ).then((data) => {
            this.listLoading = false
            this.dialogFormEdit = false
            this.getList()
          }).catch((e) => {
            this.listLoading = false
          })
        } else {
          return false
        }
      })
    },
    batchDel() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要删除的项...',
          type: 'error'
        })
        return false
      }
      const ids = _selectData.map((item, index) => {
        return item.VERSIONLOG_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/versionlog/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton: function() {
      var keys = 'versionlog:add,versionlog:del,versionlog:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.versionlogfhadminadd		// 新增权限
        this.del = data.versionlogfhadmindel		// 删除权限
        this.edit = data.versionlogfhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    resetForm() {
      this.form = {
        VERSIONLOG_ID: '', // 版本id
        VERSION: '', // 版本
        LOGINFO: '' // 更新内容
      }
    }
  }
}

</script>
