<template>
  <el-container>
    <el-container class="resize-fit-container">
      <div class="app-container">
        <div class="filter-container blue-top-border">
          <el-input
            v-model="listQuery.clientId"
            placeholder="请输入客户端Id"
            class="filter-item"
            clearable
            @change="getList"
          />
          <el-button
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleFilter"
          >查询</el-button>
          <el-button
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-edit"
            size="small"
            @click="handleCreate"
          >新增</el-button>
        </div>
        <div class="content-container">
          <el-table
            v-loading="listLoading"
            row-key="id"
            :data="list"
            border
            fit
            highlight-current-row
            style="width: 100%"
            height="calc(100vh - 186px)"
          >
            <el-table-column label="序号" type="index" width="50" align="center" />
            <el-table-column label="客户端Id" prop="clientId" align="center" />
            <el-table-column label="客户端密钥" prop="clientSecret" align="center" />
            <el-table-column label="Token过期时间" prop="expiresIn" align="center" />
            <el-table-column
              label="操作"
              align="center"
              width="80"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="{ row }">
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            :total="total"
            :skip-count.sync="listQuery.skipCount"
            :max-result-count.sync="listQuery.maxResultCount"
            @pagination="getList"
          />
        </div>
        <el-dialog
          title="创建客户端凭证"
          :close-on-click-modal="false"
          :visible.sync="dialogFormVisible"
        >
          <el-form
            ref="dataForm"
            v-loading="formLoading"
            :rules="rules"
            :model="entity"
            label-position="right"
            label-width="120px"
          >
            <el-row :gutter="16">
              <el-col :span="8">
                <el-form-item label="客户端Id" prop="clientId">
                  <el-input v-model="entity.clientId" :show-overflow-tooltip="true" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="客户端密钥" prop="clientSecret">
                  <el-input v-model="entity.clientSecret" :show-overflow-tooltip="true" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="Token过期时间" prop="expiresIn">
                  <el-input v-model="entity.expiresIn" :show-overflow-tooltip="true" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button
              :disabled="formLoading"
              @click="dialogFormVisible = false"
            >取消</el-button>
            <el-button
              :disabled="formLoading"
              type="primary"
              @click="createData()"
            >确认</el-button>
          </div>
        </el-dialog>
      </div>
    </el-container>
  </el-container>
</template>

<script>
import * as clientApi from '@/api/client-setting'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'CameraMng',
  directives: { waves },
  components: { Pagination },
  data() {
    return {
      list: [],
      entity: {},
      total: 0,
      listLoading: false,
      listQuery: {
        skipCount: 0,
        maxResultCount: 20
      },
      dialogFormVisible: false,
      formLoading: false,
      rules: {
        clientId: [
          { required: true, message: '请输入客户端Id', trigger: 'blur' },
          { max: 32, message: '长度必须少于32个字符', trigger: 'blur' }
        ],
        clientSecret: [
          { required: true, message: '请输入客户端密钥', trigger: 'blur' },
          { max: 64, message: '长度必须少于64个字符', trigger: 'blur' }
        ],
        expiresIn: [
          { required: true, message: '请输入Token过期时长', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 获取数据列表
    getList() {
      this.listLoading = true
      clientApi
        .getList(this.listQuery)
        .then(res => {
          this.list = res.items
          this.total = res.totalCount
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    // 查询
    handleFilter() {
      this.listQuery.skipCount = 0
      this.getList()
    },
    // 创建点击事件
    handleCreate() {
      this.dialogFormVisible = true
      this.formLoading = false
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 创建
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.formLoading = true
          clientApi
            .create(this.entity)
            .then(() => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
            .finally(() => {
              this.formLoading = false
            })
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将删除该项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        clientApi
          .remove(row.id)
          .then(() => {
            this.getList()
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
          .finally(() => {
            this.listLoading = false
          })
      })
    }
  }
}
</script>

<style  scoped>
.number >>> input{
  text-align: left !important;
}
.el-selects >>> .el-input--suffix{
  width: 150px !important;
}
</style>
