<template>
  <div class="app-container">
    <el-form label-width="80px">
      <el-row>
        <el-col :span="4">
          <el-form-item label="企业名称">
            <el-input v-model="KEYWORDS" placeholder="搜索"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
              搜索
            </el-button>
            <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">
              重置
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="varList"
      :row-key="getRowKey"
      :header-cell-style="{
        '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="CORP_NAME" label="企业名称" />
      <el-table-column label="操作类型" >
        <template slot-scope="{row}">
          <span v-if="row.TYPE=='0'">修改</span>
          <span v-else>删除</span>
        </template>
      </el-table-column>
      <el-table-column prop="CORP_STATE" label="企业状态" />
      <el-table-column prop="CREATOR" label="操作用户" />
      <el-table-column prop="LOGIN_IP" label="登录IP" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{row}">
          <el-button v-show="edit" type="success" icon="el-icon-view" size="mini" @click="handleEdit(row.CORPCHANGE_ID)">查看</el-button>
          <!--          <el-button v-show="del" type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(row.CORPINFOLOG_ID)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div/>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>

    <el-dialog :visible.sync="dialogFormEdit" title="查看" width="600px">
      <el-form ref="form" :rules="rules" :model="form" label-width="110px" style="width: 500px;">
        <el-form-item label="属性名称" prop="FIELDS">
          <el-input id="FIELDS" ref="FIELDS" :disabled="true" v-model="form.FIELDNAME" maxlength="255" placeholder="这里输入属性名称..." title="属性名称"/>
        </el-form-item>
        <el-form-item label="修改字段内容" prop="FIELDS_VAL">
          <el-input id="FIELDS_VAL" ref="FIELDS_VAL" :disabled="true" v-model="form.NEWNAME" maxlength="255" placeholder="这里输入修改字段内容..." title="修改字段内容"/>
        </el-form-item>
        <el-form-item label="修改前的内容" prop="FIELDS_OLD_VAL">
          <el-input id="FIELDS_OLD_VAL" ref="FIELDS_OLD_VAL" :disabled="true" v-model="form.OLDNAME" maxlength="255" placeholder="这里输入修改前的内容..." title="修改前的内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormEdit = false">取 消</el-button>
        <el-button @click="dialogFormEdit = false">确 定</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' // waves directive
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 20
      },
      total: 0,
      KEYWORDS: '',
      varList: [],
      pd: [],
      form: {
        CORPINFO_ID: '', // 企业ID
        CREATTIME: '', // 添加时间
        CREATOR: '', // 添加人
        FIELDS: '', // 属性名称
        FIELDS_VAL: '', // 修改字段内容
        FIELDS_OLD_VAL: '', // 修改前的内容
        OPERATION_TYPE: '', // 操作类型
        FIELDNAME: '',
        OLDNAME: '',
        NEWNAME: '',
        LOGIN_IP: ''// 登录ip
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      dialogFormEdit: false,
      dialogType: 'add',
      rules: {
        CORPINFO_ID: [{ required: true, message: '企业ID不能为空', trigger: 'blur' }],
        CREATTIME: [{ required: true, message: '添加时间不能为空', trigger: 'blur' }],
        CREATOR: [{ required: true, message: '添加人不能为空', trigger: 'blur' }],
        FIELDS: [{ required: true, message: '属性名称不能为空', trigger: 'blur' }],
        FIELDS_VAL: [{ required: true, message: '修改字段内容不能为空', trigger: 'blur' }],
        FIELDS_OLD_VAL: [{ required: true, message: '修改前的内容不能为空', trigger: 'blur' }],
        OPERATION_TYPE: [
          { required: true, message: '操作类型不能为空', trigger: 'change' },
          { type: 'number', message: '操作类型必须为数字' }
        ],
        LOGIN_IP: [{ required: true, message: '登录ip不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList(this.ROLE_ID)
  },
  methods: {
    getRowKey(row) {
      return row.CORPINFOLOG_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.getList()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/corpchange/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()
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.dialogType = 'add'
      this.resetForm()
      this.getDict()
      this.dialogFormEdit = true
    },
    // 查看
    handleEdit(ID) {
      this.getDict()
      this.dialogType = 'edit'
      requestFN(
        '/corpchange/goEdit',
        {
          CORPCHANGE_ID: ID
        }
      ).then((data) => {
        this.form = Object.assign({}, data.pd) // copy obj
      }).catch((e) => {
        this.listLoading = false
      })
      this.dialogFormEdit = true
    },
    // 保存
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.listLoading = true
          requestFN(
            '/corpinfolog/' + this.dialogType,
            this.form
          ).then((data) => {
            this.listLoading = false
            this.dialogFormEdit = false
            this.getList()
          }).catch((e) => {
            this.listLoading = false
          })
        } else {
          return false
        }
      })
    },

    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/corpinfolog/delete',
          {
            CORPINFOLOG_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(() => {
      })
    },

    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.CORPINFOLOG_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/corpinfolog/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 = 'corpinfolog:add,corpinfolog:del,corpinfolog:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.corpinfologfhadminadd		// 新增权限
        this.del = data.corpinfologfhadmindel		// 删除权限
        this.edit = data.corpinfologfhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 获取数据字典数据
    getDict: function() {
    },
    resetForm() {
      this.form = {
        CORPINFO_ID: '', // 企业ID
        CREATTIME: '', // 添加时间
        CREATOR: '', // 添加人
        FIELDS: '', // 属性名称
        FIELDS_VAL: '', // 修改字段内容
        FIELDS_OLD_VAL: '', // 修改前的内容
        OPERATION_TYPE: '', // 操作类型
        LOGIN_IP: ''// 登录ip
      }
    }
  }
}

</script>
