<template lang='pug'>
    el-container(style="padding:0px;" :style="{height:currentHeight-65+'px'}")
        el-header(style='height:42px;line-height:42px;border: 1px solid rgba(195, 195, 195, 1);background: #f4f4f4;')
            span.title-bold.title-left-color() 参数列表
            //- el-button(style="float:right;transform:translateY(6px);margin-right:20px;" size='mini' type="primary" icon='el-icon-refresh' @click="refresh") 刷新
        el-main.box_main
            el-card()
                div.tableCard()
                    el-table( :data="tableData" @sort-change='sortChange' @cell-dblclick="tableDbEdit" style="width: 100%;" :stripe="true" :header-cell-style="{background:'#EEF8FF',color:'#333333'}")
                        el-table-column(header-align="center" align="center"  prop="name" label="参数名称" sortable='custom' )
                        el-table-column(header-align="center" align="center"  prop="remark" label="参数描述"  sortable='custom' width="700")
                        el-table-column(header-align="center" align="center"  prop="value" label="当前值(双击修改)" sortable='custom')
                        el-table-column(header-align="center" align="center"  prop="introduction" label="参数说明"   sortable='custom')
        el-footer.box_footer
            el-pagination(background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage")
            el-dialog( width="35%" :append-to-body="true" :visible.sync="visible"  :modal-append-to-body="false"  @close="closeForm()")
                div.title-bold(slot='title') {{title}}
                div(style="margin:0 auto; width:60%;padding:0;")
                  el-form(:model="ruleForm" :rules="rules" ref="ruleForm" label-width="90px" @submit.native.prevent)
                    el-form-item(label="当前值:" prop="value")
                       el-input(v-model="ruleForm.value" placeholder="请输入内容" clearable)
                div(slot="footer" align="center")
                    el-button(type='primary' size='medium' @click="cancel") 取 消
                    el-button(type='primary' size='medium' @click="addsave" v-if="isAuth('sys:parameter:save')" v-prevent-re-click) 保存
</template>
<script>
//正则表达式，判断修改当前值是否在范围内
export default {
  computed: {
    currentHeight() {
      return this.$store.state.d2admin.currentHeight.height
    }
  },
  data () {
    return {
      visible: false,
      title: null,
      tableData: [
        {
          id: '1',
          name: 'operlogMaxkeepMonth',
          remark: '操作日志最大保留时长(月);默认为12个月，最小12，最大24',
          value: 12,
          introduction: '可修改，立即生效'
        },
        {
          id: '2',
          name: 'historyAlarmMaxkeepMonth',
          remark: '历史警告最大保留时长(月);默认为12个月，最小12，最大24',
          value: 12,
          introduction: '可修改，立即生效'
        },
        {
          id: '3',
          name: 'instructionMaxkeepMonth',
          remark: '历史指令最大保留时长(月);默认为12个月，最小12，最大24',
          value: 12,
          introduction: '可修改，立即生效'
        },
        {
          id: '4',
          name: 'eventMaxkeepMonth',
          remark: '历史事件最大保留时长(月);默认为12个月，最小12，最大24',
          value: 12,
          introduction: '可修改，立即生效'
        }
      ],
      currentPage: 1,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      ruleForm: {},
      rules: null //添加规则
    }
  },
  watch: {
    totalPage() { //注意这个函数的名字必须和你监听data中的属性的名字一样，这样才能当你data中的属性发生变化时，触发这个函数
      let pages = Math.ceil(this.totalPage / this.pageSize)//新数据总页数
      //总页数小于当前页数则重新加载列表数据
      if (pages < this.pageIndex) {
          this.pageIndex = pages || 1
          this.getDataList()//获取表格数据的方法
      }
    }
  },
  mounted () {
    this.getDataList()
  },
  methods: {
    //排序
    sortChange (column) {
      if (column.order === 'descending') {
        this.order = 'desc'
      } else {
        this.order = 'asc'
      }
      if (column.column.columnKey) {
        this.sort = column.column.columnKey
      } else {
        this.sort = column.prop
      }
      this.getDataList()
    },
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sysParams/paramsAllList'),
        method: 'post',
        params: {
          stationId: localStorage.getItem('stationId'),
          page: this.pageIndex,
          limit: this.pageSize,
          sort: this.sort,
          order: this.order
        }
      }).then(data => {
        if (data) {
          this.tableData = data.page.records
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    tableDbEdit (row, column, cell, event) {
      this.visible = true
      this.rules = null
      if (row) {
        //深度拷贝
        this.ruleForm = JSON.parse(JSON.stringify(row))
        this.title =
          '修改当前值(只能在' +
          this.ruleForm.byx1 +
          '和' +
          this.ruleForm.byx2 +
          '之间) '
        let min = Number(this.ruleForm.byx1)
        let max = Number(this.ruleForm.byx2)
        this.rules = {
          value: [
            { required: true, message: '当前值不能为空', trigger: 'blur' },
            {
              validator: (rule, value, callback) => {
                const age = /^[0-9]*$/
                if (!age.test(value)) {
                  callback(
                    new Error(
                      '当前值只能为数字，并在' + min + '到' + max + '之间'
                    )
                  )
                } else {
                  if (value < min || value > max) {
                    callback(
                      new Error(
                        '当前值只能为数字，并在' + min + '到' + max + '之间'
                      )
                    )
                  } else {
                    callback()
                  }
                }
              },
              trigger: 'blur'
            }
          ]
        }
      } else {
        this.ruleForm = {}
        this.ruleForm.id = row.id
        this.ruleForm.value = row.value
      }
    },
    // 提交
    addsave () {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          this.closeForm()
          this.$http({
            url: this.$http.adornUrl(`/sysParams/update`),
            method: 'post',
            data: this.ruleForm
          }).then(data => {
            console.log(data)
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data)
            }
          })
        }
      })
    },
    closeForm () {
      this.visible = false
    },
    refresh () {
      this.getDataList()
    },
    // 每页数
    handleSizeChange (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    handleCurrentChange (val) {
      this.pageIndex = val
      this.getDataList()
    },
    //取消
    cancel() {
      this.$confirm('确认取消?', this.title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.closeForm()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出'
          })
        })
    }
  }
}
</script>
