<template>
  <div class="form-wrap">
    <div class="searchBox">
      <div class="btn-wrap">
        <el-button
          type="primary"
          @click="showDialog('add')">新增账号</el-button>
      </div>
    </div>
    <div class="tableBox">
      <el-table
        v-loading="loading"
        row-class-name="is-center"
        :data="tableData">
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="180">
        </el-table-column>
        <el-table-column
          prop="updateTime"
          label="更新时间"
          width="180">
        </el-table-column>
        <el-table-column
          prop="accountId"
          label="接口帐号"
          min-width="160">
        </el-table-column>
        <el-table-column
          prop="secretKey"
          label="接口密钥"
          min-width="200"
          show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="aesKey"
          label="AES 加密密钥"
          min-width="200"
          show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="aesVector"
          label="AES 加密初始向量"
          min-width="200"
          show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="160">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="showDialog('edit', scope.row)">编辑</el-button>
            <el-button
              type="text"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 新增/编辑弹框 -->
    <el-dialog
      :title="dialogType === 'add' ? '新增账号' : '编辑账号'"
      :visible.sync="dialogVisible"
      width="600px">
      <el-form
        ref="accountForm"
        :model="accountForm"
        label-width="130px">
        <el-form-item
          label="接口帐号"
          prop="accountId"
          :rules="[{ required: true, message: '请输入接口帐号', trigger: 'blur' }]">
          <el-input
            v-model="accountForm.accountId"
            placeholder="请输入接口帐号"></el-input>
        </el-form-item>
        <el-form-item
          label="接口密钥"
          prop="secretKey"
          :rules="[{ required: true, message: '请输入接口密钥', trigger: 'blur' }]">
          <el-input
            v-model="accountForm.secretKey"
            placeholder="请输入接口密钥"></el-input>
        </el-form-item>
        <el-form-item
          label="AES 加密密钥"
          prop="aesKey"
          :rules="[{ required: true, message: '请输入AES 加密密钥', trigger: 'blur' }]">
          <el-input
            v-model="accountForm.aesKey"
            placeholder="请输入AES 加密密钥"></el-input>
        </el-form-item>
        <el-form-item
          label="AES 加密初始向量"
          prop="aesVector"
          :rules="[{ required: true, message: '请输入AES 加密初始向量', trigger: 'blur' }]">
          <el-input
            v-model="accountForm.aesVector"
            placeholder="请输入AES 加密初始向量"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          :loading="submitLoading"
          @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getXcAccountList, addXcAccount, updateXcAccount, deleteXcAccount } from '@/api/platform'

export default {
  name: 'XcAccountParam',
  data () {
    return {
      loading: false,
      submitLoading: false,
      tableData: [],
      dialogVisible: false,
      dialogType: 'add',
      accountForm: {
        id: '',
        accountId: '',
        secretKey: '',
        aesKey: '',
        aesVector: ''
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.loading = true
      getXcAccountList().then(res => {
        this.loading = false
        this.tableData = res.data || []
      }).catch(() => {
        this.loading = false
      })
    },
    showDialog (type, row) {
      this.dialogType = type
      if (type === 'edit' && row) {
        this.accountForm = {
          id: row.id,
          accountId: row.accountId,
          secretKey: row.secretKey,
          aesKey: row.aesKey,
          aesVector: row.aesVector
        }
      } else {
        this.accountForm = {
          id: '',
          accountId: '',
          secretKey: '',
          aesKey: '',
          aesVector: ''
        }
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.accountForm && this.$refs.accountForm.clearValidate()
      })
    },
    handleSubmit () {
      this.$refs.accountForm.validate(valid => {
        if (!valid) return
        this.submitLoading = true
        const api = this.dialogType === 'add' ? addXcAccount : updateXcAccount
        const params = { ...this.accountForm }
        if (this.dialogType === 'add') {
          delete params.id
        }
        api(params).then(() => {
          this.submitLoading = false
          this.dialogVisible = false
          this.$message.success(this.dialogType === 'add' ? '新增成功' : '编辑成功')
          this.getList()
        }).catch(() => {
          this.submitLoading = false
        })
      })
    },
    handleDelete (row) {
      this.$confirm('确定删除该账号?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteXcAccount({ idList: [row.id] }).then(() => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
