<template>
  <div class="app-container">
    <el-card>
      <span class="show-text">摄像头名称:</span>
      <el-input
        v-model="RoleFunctioning"
        clearable
        placeholder="名称"
        style="width: 280px;margin-right: 26px"/>
      <el-button icon="el-icon-search" plain type="primary" @click="searchData()">查询</el-button>
      <el-button
        icon="el-icon-circle-plus-outline"
        plain
        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
          style="width: 70%"
          tooltip-effect="dark">
          <el-table-column align="center" label="序号" type="index" width="100"/>
          <el-table-column align="center" label="摄像头名称">
            <template slot-scope="scope">
              {{ scope.row.name }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="序列号">
            <template slot-scope="scope">
              {{ scope.row.deviceSerial }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="验证码">
            <template slot-scope="scope">
              {{ scope.row.code }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="用户AppKey" width="250">
            <template slot-scope="scope">
              {{ scope.row.appKey }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="用户Secret" width="250">
            <template slot-scope="scope">
              {{ scope.row.appSecret }}
            </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>
    </el-card>

    <el-dialog :visible.sync="dialogFormVisible1" center title="新增摄像头" width="490px">
      <el-divider style="margin: 0"/> 
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="150px">
        <el-form-item label="摄像头名称" prop="name" style="display: inline-block">
          <el-input
            v-model="form.name"
            clearable
            placeholder="摄像头名称"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="序列号" prop="deviceSerial" style="display: inline-block">
          <el-input
            v-model="form.deviceSerial"
            clearable
            placeholder="序列号"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="验证码" prop="code" style="display: inline-block">
          <el-input
            v-model="form.code"
            clearable
            placeholder="验证码"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="用户AppKey" prop="appKey" style="display: inline-block">
          <el-input
            v-model="form.appKey"
            clearable
            placeholder="用户AppKey"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="用户Secret" prop="appSecret" style="display: inline-block">
          <el-input
            v-model="form.appSecret"
            clearable
            placeholder="用户Secret"
            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('form')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="dialogFormVisible1 = false">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible2" center title="编辑角色" width="490px">
      <el-divider style="margin: 0"/>
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="150px">
         <el-form-item label="摄像头名称" prop="name" style="display: inline-block">
          <el-input
            v-model="form2.name"
            clearable
            placeholder="摄像头名称"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="序列号" prop="deviceSerial" style="display: inline-block">
          <el-input
            v-model="form2.deviceSerial"
            clearable
            placeholder="序列号"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="验证码" prop="code" style="display: inline-block">
          <el-input
            v-model="form2.code"
            clearable
            placeholder="验证码"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="用户AppKey" prop="appKey" style="display: inline-block">
          <el-input
            v-model="form2.appKey"
            clearable
            placeholder="用户AppKey"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-form-item label="用户Secret" prop="appSecret" style="display: inline-block">
          <el-input
            v-model="form2.appSecret"
            clearable
            placeholder="用户Secret"
            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="submitAdd2('form2')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="dialogFormVisible2 = false">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      RoleFunctioning: '',
      loading: false,
      tableData: [],
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      name: '',
      num: '', // 角色编号
      form: {
        name: '',
        deviceSerial: '',
        code: '',
        appKey: '',
        appSecret: '',
      },
      form2: {
        AID:'',
        name: '',
        deviceSerial: '',
        code: '',
        appKey: '',
        appSecret: '',
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        deviceSerial: [{ required: true, message: '请输入序列号', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        appKey: [{ required: true, message: '请输入用户AppKey', trigger: 'blur' }],
        appSecret: [{ required: true, message: '请输入用户Secret', trigger: 'blur' }]
      },
      rules2: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        deviceSerial: [{ required: true, message: '请输入序列号', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        appKey: [{ required: true, message: '请输入用户AppKey', trigger: 'blur' }],
        appSecret: [{ required: true, message: '请输入用户Secret',}]
      },
      total: 0, // 总条数
      pageSize: 10, // 每页显示条数
      pageList: 1 // 后台获取页
    }
  },
  created() {
    this.searchData()
  },
  methods: {
    // 查询
    searchData() {
      this.loading = true
      this.tableData = []
      var param = []
      param[0] = ['name', this.RoleFunctioning]
      var Data = this.CreateData('11', 'WEB_基础_摄像头_管理_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
        this.loading = false
      })
    },
    // 编辑角色
    handleEdit(row) {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.form2.AID = row.AID
      this.form2.name = row.name
      this.form2.deviceSerial = row.deviceSerial
      this.form2.code = row.code
      this.form2.appKey = row.appKey
      this.form2.appSecret = row.appSecret
      this.dialogFormVisible2 = true
    },
    // 编辑角色功能
    submitAdd2(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param.push(['AID', this.form2.AID])
          param.push(['name', this.form2.name])
          param.push(['deviceSerial', this.form2.deviceSerial])
          param.push(['code', this.form2.code])
          param.push(['appKey', this.form2.appKey])
          param.push(['appSecret', this.form2.appSecret])
          var Data = this.CreateData('11', 'WEB_基础_摄像头_修改', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result == 1) {
              this.$message({
                message: '信息编辑成功',
                type: 'success'
              })
              this.searchData()
              this.dialogFormVisible2 = false
            } else {
              this.$message.error('信息编辑失败')
            }
          })
        }
      })
    },

    // 增加角色
    handleAdd() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.dialogFormVisible1 = true
    },
    // 确认增加角色
    submitAdd1(formName) { // 增加角色功能
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param.push(['name', this.form.name])
          param.push(['deviceSerial', this.form.deviceSerial])
          param.push(['code', this.form.code])
          param.push(['appKey', this.form.appKey])
          param.push(['appSecret', this.form.appSecret])
          var Data = this.CreateData('12', 'WEB_基础_摄像头_新增', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result == 1) {
              this.$message({
                message: '新增成功！',
                type: 'success'
              })
              this.searchData()
              this.dialogFormVisible1 = false
            } else {
              this.$message.error('信息增加失败')
            }
          })
        }
      })
    },
    // 删除角色
    handleDelete(row) {
      this.$confirm('当前摄像头将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirm-confirm-button',
        cancelButtonClass: 'confirm-cancel-button',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['AID', row.AID]
        var Data = this.CreateData('12', 'WEB_基础_摄像头_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result == 1) {
            this.$message.success('删除成功')
          } else {
            this.$message.error('删除失败')
          }
          this.searchData()
        })
      }).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>
.el-card {
  margin: 0px;
}
</style>
