<template>
  <div>
    <div>
      <el-form :inline="true" class="orderform" size="mini">
        <el-row>
          <el-form-item label="错误码:">
            <el-input v-model="tag" style="width: 250px" placeholder="请输入..." class="search_tag" clearable/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="btn_search()"><i class="el-icon-search"/> 搜索</el-button>
            <el-button @click="btn_clear()"><i class="el-icon-remove-outline"/> 重置</el-button>
            <el-button type="primary" @click="add()"><i class="el-icon-plus"/> 添加</el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <el-table
        ref="multipleTable"
        :data="tableData"
        :highlight-current-row="true"
        :header-cell-style="{color:APP_CONFIG.TABLE_DOMINANT_COLOR}"
        :cell-style="{color:APP_CONFIG.TABLE_ASSISTANT_COLOR}"
        border>
        <el-table-column
          prop="errorCode"
          label="错误码"/>
        <el-table-column
          prop="description"
          label="错误描述"/>
        <el-table-column
          prop="message"
          label="错误详情"/>
        <el-table-column
          prop="suggestion"
          label="处理意见"/>
        <el-table-column
          fixed="right"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mydiv">
        <el-pagination
          :current-page.sync="pageNo"
          :page-sizes="[10, 50, 100, 300,1000]"
          :page-size="pageSize"
          :total="totalDataNumber"
          layout="sizes,total, prev, pager, next"
          class="table-pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>

    </div>

    <el-dialog :visible.sync="addFormVisible" title="新增错误码">
      <el-form :model="addForm" :rules="rules">
        <el-form-item :label-width="formLabelWidth" label="错误码" prop="errorCode">
          <el-input v-model="addForm.errorCode" placeholder="请输入错误码"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="错误描述">
          <el-input v-model="addForm.description" placeholder="请输入错误描述"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="错误详情">
          <el-input v-model="addForm.message" placeholder="请输入错误详情"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="处理意见">
          <el-input v-model="addForm.suggestion" placeholder="请输入处理意见"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnAddCancel">取 消</el-button>
        <el-button type="primary" @click="btnAddSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="editFormVisible" title="编辑错误码">
      <el-form :model="editForm" :rules="rules">
        <el-form-item :label-width="formLabelWidth" label="错误码" prop="errorCode">
          <el-input v-model="editForm.errorCode" disabled="true" placeholder="请输入错误码"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="错误描述">
          <el-input v-model="editForm.description" placeholder="请输入错误描述"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="错误详情">
          <el-input v-model="editForm.message" placeholder="请输入错误详情"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="处理意见">
          <el-input v-model="editForm.suggestion" placeholder="请输入处理意见"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnUpdateCancel">取 消</el-button>
        <el-button type="primary" @click="btnUpdateSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="提示"
      width="30%">
      <span>确定删除选择的错误码? </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitDelete">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
import APP_CONFIG from "../../../public/config";

export default {
  name: 'Index',
  computed: {
    APP_CONFIG() {
      return APP_CONFIG
    }
  },
  data() {
    return {
      rules: {
        errorCode: [{ required: true, message: '请输入错误码', trigger: 'blur' }]
      },
      tag: '',
      allData: [], // 所有前端分页数据
      tableData: [], // 表格数据 仅展示当前页的数据
      pageNo: 1, // 当前页数
      totalDataNumber: 0, // 总记录数
      pageSize: 10,
      formLabelWidth: '100px', // 弹出框form宽
      disableFormVisible: false,
      addFormVisible: false,
      editFormVisible: false,
      addForm: {
        errorCode: '',
        description: '',
        message: '',
        suggestion: ''
      },
      editForm: {
        errorCode: '',
        description: '',
        message: '',
        suggestion: ''
      },
      errorCode: '',
      deleteDialogVisible: false
    }
  },
  created() {
    this.btn_search()
  },
  mounted() {
  },
  methods: {
    btn_search() {
      const vm = this
      axios.get(process.env.BASE_API + '/codeinfo/list', {
        params: {
          tag: this.tag,
          status: this.select_value
        }
      }).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
          vm.totalDataNumber = 0
          vm.allData = []
          vm.tableData = []
        } else {
          vm.totalDataNumber = parseInt(obj.total)
          if (vm.totalDataNumber > 0) {
            vm.allData = obj.data
            vm.tableData = obj.data.slice(0, vm.pageSize)
          } else {
            vm.allData = []
            vm.tableData = []
          }
        }
      }).catch(function(error) {
        alert(error)
      })
    },
    btn_clear() {
      this.tag = ''
      this.select_value = ''
      this.btn_search()
    },
    handleCurrentChange() {
      this.tableData = this.allData.slice((this.pageNo - 1) * this.pageSize, this.pageNo * this.pageSize)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.tableData = this.allData.slice((this.pageNo - 1) * this.pageSize, this.pageNo * this.pageSize)
    },
    add() {
      this.addFormVisible = true
      this.addForm.errorCode = ''
      this.addForm.description = ''
      this.addForm.message = ''
      this.addForm.suggestion = ''
    },
    btnAddCancel() {
      this.addFormVisible = false
      this.addForm.errorCode = ''
      this.addForm.description = ''
      this.addForm.message = ''
      this.addForm.suggestion = ''
    },
    btnAddSubmit() {
      if (this.addForm.errorCode === '' || this.addForm.errorCode === undefined) {
        this.$message.info('请输入错误码，不能为空')
        return
      }
      var vm = this
      var params = new URLSearchParams()
      params.append('errorCode', vm.addForm.errorCode)
      params.append('description', vm.addForm.description)
      params.append('message', vm.addForm.message)
      params.append('suggestion', vm.addForm.suggestion)
      axios.post(process.env.BASE_API + '/codeinfo/insert', params).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
        } else {
          vm.$message(obj.message)
          vm.addFormVisible = false
          vm.pageNo = 1
          vm.btn_search()
        }
      }).catch(function(error) {
        alert(error)
      })
    },
    handleEdit(row) {
      this.editForm.errorCode = row.errorCode
      this.editForm.description = row.description
      this.editForm.message = row.message
      this.editForm.suggestion = row.suggestion
      this.editFormVisible = true
    },
    btnUpdateCancel() {
      this.editFormVisible = false
      this.editForm.errorCode = ''
      this.editForm.description = ''
      this.editForm.message = ''
      this.editForm.suggestion = ''
    },
    btnUpdateSubmit() {
      if (this.editForm.errorCode === '' || this.editForm.errorCode === undefined) {
        this.$message.info('请输入错误码名称，不能为空')
        return
      }
      var vm = this
      var params = new URLSearchParams()
      params.append('errorCode', vm.editForm.errorCode)
      params.append('description', vm.editForm.description)
      params.append('message', vm.editForm.message)
      params.append('suggestion', vm.editForm.suggestion)
      axios.post(process.env.BASE_API + '/codeinfo/update', params).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
        } else {
          vm.$message(obj.message)
          vm.editForm.errorCode = ''
          vm.editForm.description = ''
          vm.editForm.message = ''
          vm.editForm.suggestion = ''
          vm.editFormVisible = false
          vm.pageNo = 1
          vm.btn_search()
        }
      }).catch(function(error) {
        alert(error)
      })
    },
    handleDelete(row) {
      this.errorCode = row.errorCode
      this.deleteDialogVisible = true
    },
    submitDelete() {
      var vm = this
      var params = new URLSearchParams()
      params.append('errorCode', this.errorCode)
      axios.post(process.env.BASE_API + '/codeinfo/delete', params).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
        } else {
          vm.$message(obj.message)
          vm.btn_search()
          vm.deleteDialogVisible = false
        }
      }).catch(function(error) {
        alert(error)
      })
    }
  }
}
</script>

<style scoped>
  .orderform {
    padding-top: 20px;
    padding-left: 10px;
    padding-right: 10px;
  }

  .mydiv {
    float: right;
    margin-right: 10px;
  }
</style>
