<template>
  <div class="app-container-source">

    <div class="filter-container">
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        {{ $t('pages.appManager.permission.action.addPermission') }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('pages.appManager.loading')"
      fit
      highlight-current-row
    >
      <el-table-column prop="resourceName" :label="$t('pages.appManager.permission.table.name')" align="center" />
      <el-table-column prop="auth" :label="$t('pages.appManager.permission.table.key')" align="center" />
      <el-table-column prop="groupName" :label="$t('pages.appManager.permission.table.group')" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.groupName === 'USER'">{{ $t('pages.appManager.permission.group.user') }}</span>
          <span v-if="scope.row.groupName === 'ROOM'">{{ $t('pages.appManager.permission.group.room') }}</span>
          <span v-if="scope.row.groupName === 'OTHER'">{{ $t('pages.appManager.permission.group.other') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" :label="$t('pages.appManager.permission.table.createdAt')" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('pages.appManager.permission.table.actions')" align="center">
        <template slot-scope="scope">
          <div>
            <el-button type="text" @click.native="handleUpdate(scope.row)">{{ $t('pages.appManager.permission.action.edit') }}</el-button>
            <el-button type="text" @click.native="handlDel(scope.row.id)">{{ $t('pages.appManager.permission.action.delete') }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="textOptTitle"
      :visible.sync="formVisible"
      :before-close="handleClose"
      :close-on-click-modal="false"
      width="400px"
    >
      <div v-loading="submitLoading">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item :label="$t('pages.appManager.permission.form.name')" prop="resourceName">
            <el-input v-model.trim="form.resourceName" type="text" />
          </el-form-item>
          <el-form-item :label="$t('pages.appManager.permission.form.key')" prop="auth">
            <el-input v-model.trim="form.auth" type="text" />
          </el-form-item>
          <el-form-item :label="$t('pages.appManager.permission.form.group')" prop="groupName">
            <el-select
              v-model="form.groupName"
              :placeholder="$t('pages.appManager.permission.placeholder.group')"
              clearable
              style="width:100%;"
              class="filter-item"
            >
              <el-option :label="$t('pages.appManager.permission.group.user')" :value="'USER'" />
              <el-option :label="$t('pages.appManager.permission.group.room')" :value="'ROOM'" />
              <el-option :label="$t('pages.appManager.permission.group.other')" :value="'OTHER'" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm()">{{ $t('pages.appManager.permission.action.save') }}</el-button>
            <el-button @click="handleClose()">{{ $t('common.cancel') }}</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAdministratorAuthTable, deleteAdministratorAuth, addAdministratorAuth, updateAdministratorAuth } from '@/api/app-user'

function getFormData() {
  return {
    id: '',
    resourceName: '',
    auth: '',
    groupName: ''
  }
}

export default {
  name: 'AdministratorAuthTable',
  data() {
    return {
      thisRow: {},
      level: 0,
      formVisible: false,
      textOptTitle: '',
      form: getFormData(),
      submitLoading: false,
      list: [],
      total: 0,
      listLoading: true,
      searchLoading: false
    }
  },
  computed: {
    rules() {
      return {
        resourceName: [
          { required: true, message: this.$t('pages.appManager.permission.validation.enterName'), trigger: 'blur' }
        ],
        auth: [
          { required: true, message: this.$t('pages.appManager.permission.validation.enterKey'), trigger: 'blur' }
        ],
        groupName: [
          { required: true, message: this.$t('pages.appManager.permission.validation.selectGroup'), trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.renderData()
  },
  methods: {
    renderData() {
      const that = this
      that.listLoading = true
      getAdministratorAuthTable().then(res => {
        const { body } = res
        that.list = body
        that.searchLoading = that.listLoading = false
      }).catch(er => {
        that.searchLoading = that.listLoading = false
      })
    },
    handleCreate() {
      this.textOptTitle = this.$t('pages.appManager.permission.dialog.create')
      this.formVisible = true
    },
    handleUpdate(row) {
      this.textOptTitle = this.$t('pages.appManager.permission.dialog.edit')
      this.formVisible = true
      this.form = Object.assign(this.form, row)
    },
    handleClose() {
      // 去除校验
      this.$refs.form.clearValidate()
      this.formVisible = false
      this.resetForm()
    },
    resetForm() {
      this.form = getFormData()
    },
    submitForm() {
      const that = this
      that.$refs.form.validate(valid => {
        if (valid) {
          that.submitLoading = true
          if (that.form.id) {
            updateAdministratorAuth(that.form).then(res => {
              that.submitLoading = false
              that.formVisible = false
              that.form = getFormData()
              that.renderData()
            }).catch(er => {
              that.submitLoading = false
              that.$emit('fial', er)
            })
            return
          }
          addAdministratorAuth(that.form).then(res => {
            that.submitLoading = false
            that.formVisible = false
            that.form = getFormData()
            that.renderData()
          }).catch(er => {
            that.submitLoading = false
            console.error(er)
            that.$emit('fial', er)
          })
        } else {
          console.error('error submit!!')
          return false
        }
      })
    },
    handlDel(id) {
      const that = this
      this.$confirm(this.$t('pages.appManager.permission.confirm.deletePermission'), this.$t('pages.appManager.permission.confirm.title'), {
        confirmButtonText: this.$t('pages.appManager.permission.confirm.confirm'),
        cancelButtonText: this.$t('pages.appManager.permission.confirm.cancel')
      }).then(() => {
        deleteAdministratorAuth(id).then(res => {
          that.renderData()
          that.$opsMessage.success()
        }).catch(er => {
          that.$opsMessage.success()
        })
      }).catch(() => {})
    }
  }
}
</script>
<style scoped lang="scss">
.this-level {
  padding-bottom: 30px;
  display: flex;
  > div {
    width: 150px;
  }
}

</style>
