<template>
  <div class="app-container">
    <el-card>
      <span class="show-text">数据名称:</span>
      <el-input v-model="TagName" clearable placeholder="请输入数据名称" style="width: 280px;margin-right: 26px"/>
      <el-button :disabled="loading" icon="el-icon-search" plain type="primary" @click="searchTable()">查询</el-button>
      <el-button icon="el-icon-circle-plus-outline" style="margin-left: 14px" type="success" @click="handleAdd('form')">
        新增
      </el-button>
      <div style="margin-top: 10px">
        <el-table
          v-loading="loading"
          :data="tableData"
          border
          element-loading-background="rgba(0, 0, 0, 0.2)"
          element-loading-spinner="el-icon-loading"
          element-loading-text="数据加载中"
          height="694"
          highlight-current-row
          size="mini"
          stripe
          stripe
          style="width: 1580px"
          tooltip-effect="dark">
          <el-table-column align="center" label=" " type="index" width="100"/>
          <el-table-column align="center" label="数据说明" width="320">
            <template slot-scope="scope">
              {{ scope.row.变量类型名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="一级上限" width="232">
            <template slot-scope="scope">
              {{ scope.row.LimitUp1 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="一级下限" width="232">
            <template slot-scope="scope">
              {{ scope.row.LimitDown1 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="二级上限" width="232">
            <template slot-scope="scope">
              {{ scope.row.LimitUp2 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="二级下限" width="232">
            <template slot-scope="scope">
              {{ scope.row.LimitDown2 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="编辑" width="200">
            <template slot-scope="scope">
              <el-button
                icon="el-icon-edit"
                style="font-family: MiSans Regular,serif;color: #4d77d6"
                type="text"
                @click="handleEdit(scope.row)">编辑
              </el-button>
              <el-button
                icon="el-icon-delete"
                style="font-family: MiSans Regular,serif;color: #c61a1a"
                type="text"
                @click="handleDelete(scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 10px;">
          <el-pagination
            v-if="!loading"
            :current-page="pageList"
            :page-size="pageSize"
            :page-sizes="[10, 20, 30, 40]"
            :total="total"
            background
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"/>
        </div>
      </div>
    </el-card>

    <el-dialog :visible.sync="dialogFormVisible1" center title="新增数据设定" width="440px">
      <el-divider style="margin: 0"/>
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="100px">
        <el-form-item label="数据名称" prop="数据名称">
          <el-select v-model="form.数据名称" filterable placeholder="数据名称" style="width:240px">
            <el-option
              v-for="item in measureDatas"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="一级上限" prop="LimitUp1">
          <el-input
            v-model="form.LimitUp1"
            clearable
            oninput="value=value.replace(/[^\-?\d.]/g,'').replace(/^\./g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.').replace('-','$#$').replace(/\-/g,'').replace('$#$','-')"
            placeholder="一级上限"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="一级下限" prop="LimitDown1">
          <el-input
            v-model="form.LimitDown1"
            clearable
            oninput="value=value.replace(/[^\-?\d.]/g,'').replace(/^\./g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.').replace('-','$#$').replace(/\-/g,'').replace('$#$','-')"
            placeholder="一级下限"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="二级上限" prop="LimitUp2">
          <el-input
            v-model="form.LimitUp2"
            clearable
            oninput="value=value.replace(/[^\-?\d.]/g,'').replace(/^\./g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.').replace('-','$#$').replace(/\-/g,'').replace('$#$','-')"
            placeholder="二级上限"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="二级下限" prop="LimitDown2">
          <el-input
            v-model="form.LimitDown2"
            clearable
            oninput="value=value.replace(/[^\-?\d.]/g,'').replace(/^\./g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.').replace('-','$#$').replace(/\-/g,'').replace('$#$','-')"
            placeholder="二级下限"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item>
            <el-button style="width: 80px;height: 40px" type="primary" @click="submitAdd('form')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="callOff('form')">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible2" center title="编辑数据设定" width="440px">
      <el-divider style="margin: 0"/>
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="right" label-width="100px">
        <el-form-item label="数据名称" prop="数据名称">
          <el-select v-model="form1.数据名称" disabled placeholder="请选择数据名称" style="width:240px">
            <el-option
              v-for="item in measureDatas"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>

        <el-form-item label="一级上限" prop="LimitUp1">
          <el-input
            v-model="form1.LimitUp1"
            clearable
            oninput="value=value.replace(/[^\-?\d.]/g,'').replace(/^\./g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.').replace('-','$#$').replace(/\-/g,'').replace('$#$','-')"
            placeholder="一级上限"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="一级下限" prop="LimitDown1">
          <el-input
            v-model="form1.LimitDown1"
            clearable
            oninput="value=value.replace(/[^\-?\d.]/g,'').replace(/^\./g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.').replace('-','$#$').replace(/\-/g,'').replace('$#$','-')"
            placeholder="一级下限"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="二级上限" prop="LimitUp2">
          <el-input
            v-model="form1.LimitUp2"
            clearable
            oninput="value=value.replace(/[^\-?\d.]/g,'').replace(/^\./g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.').replace('-','$#$').replace(/\-/g,'').replace('$#$','-')"
            placeholder="二级上限"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="二级下限" prop="LimitDown2">
          <el-input
            v-model="form1.LimitDown2"
            clearable
            oninput="value=value.replace(/[^\-?\d.]/g,'').replace(/^\./g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.').replace('-','$#$').replace(/\-/g,'').replace('$#$','-')"
            placeholder="二级下限"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item>
            <el-button style="width: 80px;height: 40px" type="primary" @click="submitAdd1('form1')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="callOff1('form1')">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      TagName: '',
      loading: false,
      tableData: [],
      measureData: '',
      measureDatas: [],
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      form: {
        数据名称: '',
        LimitUp1: '',
        LimitDown1: '',
        LimitUp2: '',
        LimitDown2: ''
      },
      form1: {
        数据名称: '',
        TagID: '',
        LimitUp1: '',
        LimitDown1: '',
        LimitUp2: '',
        LimitDown2: ''
      },
      rules: {
        数据名称: [{ required: true, message: '请输入数据名称', trigger: 'blur' }],
        LimitUp1: [{ required: true, message: '请输入一级上限', trigger: 'blur' }],
        LimitDown1: [{ required: true, message: '请输入一级下限', trigger: 'blur' }],
        LimitUp2: [{ required: true, message: '请输入二级上限', trigger: 'blur' }],
        LimitDown2: [{ required: true, message: '请输入二级下限', trigger: 'blur' }]
      },
      rules1: {
        数据名称: [{ required: true, message: '请输入数据名称', trigger: 'blur' }],
        LimitUp1: [{ required: true, message: '请输入一级上限', trigger: 'blur' }],
        LimitDown1: [{ required: true, message: '请输入一级下限', trigger: 'blur' }],
        LimitUp2: [{ required: true, message: '请输入二级上限', trigger: 'blur' }],
        LimitDown2: [{ required: true, message: '请输入二级下限', trigger: 'blur' }]
      },
      total: 0, // 总条数
      pageSize: 10, // 每页显示条数
      pageList: 1 // 后台获取页
    }
  },
  created() {
    this.searchTableTagID()
    this.searchTable()
  },
  methods: {
    // 查询TagID
    searchTableTagID() {
      this.measureDatas = []
      var param = []
      var Data = this.CreateData('11', '基础数据_测量数据_查询TagId', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          response.data.map(res => {
            this.measureDatas.push({
              value: res.TagID,
              label: res.名称
            })
          })
        }
      })
    },
    // 查询
    searchTable() {
      this.loading = true
      this.tableData = []
      var param = []
      param[0] = ['TagName', this.TagName]
      param[1] = ['PageCurrent', this.pageList]
      param[2] = ['PageSize', this.pageSize]
      param[3] = ['PageCount', '1111', 'int', '1']
      param[4] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', '基础数据_测量数据_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data.result
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    // 编辑
    handleEdit(row) {
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
      this.form1.数据名称 = row.变量类型名称
      this.form1.TagID = row.TagID
      this.form1.LimitUp1 = row.LimitUp1
      this.form1.LimitDown1 = row.LimitDown1
      this.form1.LimitUp2 = row.LimitUp2
      this.form1.LimitDown2 = row.LimitDown2
      this.dialogFormVisible2 = true
    },
    // 确认编辑焦点
    submitAdd1(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          var param = []
          param[0] = ['TagID', this.form1.TagID]
          param[1] = ['LimitUp1', this.form1.LimitUp1]
          param[2] = ['LimitDown1', this.form1.LimitDown1]
          param[3] = ['LimitUp2', this.form1.LimitUp2]
          param[4] = ['LimitDown2', this.form1.LimitDown2]
          var Data = this.CreateData('12', '基础数据_测量数据_编辑', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('编辑成功')
              this.form1.TagID = ''
              this.form1.LimitUp1 = ''
              this.form1.LimitDown1 = ''
              this.form1.LimitUp2 = ''
              this.searchTable()
              this.dialogFormVisible2 = false
            } else {
              this.$message.error('编辑失败')
            }
          })
        }
      })
    },
    // 增加焦点
    handleAdd() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.searchTableTagID()
      this.form.数据名称 = ''
      this.form.TagID = ''
      this.form.LimitUp1 = ''
      this.form.LimitDown1 = ''
      this.form.LimitUp2 = ''
      this.form.LimitDown2 = ''
      this.dialogFormVisible1 = true
    },
    // 确认增加焦点
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          this.tableData = []
          var param = []
          param[0] = ['TagID', this.form.数据名称]
          param[1] = ['LimitUp1', this.form.LimitUp1]
          param[2] = ['LimitDown1', this.form.LimitDown1]
          param[3] = ['LimitUp2', this.form.LimitUp2]
          param[4] = ['LimitDown2', this.form.LimitDown2]
          var Data = this.CreateData('12', '基础数据_测量数据_新增', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('增加成功')
              this.form.焦点说明 = ''
              this.searchTable()
              this.dialogFormVisible1 = false
            } else {
              this.$message.error('增加失败')
            }
          })
        }
      })
    },
    // 重置表单
    callOff1(formName) {
      this.dialogFormVisible2 = false
      this.form1.数据名称 = ''
      this.form1.TagID = ''
      this.form1.LimitUp1 = ''
      this.form1.LimitDown1 = ''
      this.form1.LimitUp2 = ''
      this.form1.LimitDown2 = ''
      this.$refs[formName].resetFields()
    },
    // 重置表单
    callOff(formName) {
      this.dialogFormVisible1 = false
      this.form.数据名称 = ''
      this.form.TagID = ''
      this.form.LimitUp1 = ''
      this.form.LimitDown1 = ''
      this.form.LimitUp2 = ''
      this.form.LimitDown2 = ''
      this.$refs[formName].resetFields()
    },
    // 删除
    handleDelete(row) {
      this.$confirm('当前数据将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirm-confirm-button',
        cancelButtonClass: 'confirm-cancel-button',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['TagID', row.TagID]
        var Data = this.CreateData('12', '基础数据_测量数据_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.searchTable()
            this.searchTableTagID()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    }
  }
}
</script>
<style scoped>
</style>

