<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="50%"
  >
    <div>
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="客户名称">
          <oc-selector v-model="formData.ocId" placeholder="输入客户名称" prop="ocId" />
        </el-form-item>

        <el-form-item label="APP ID">
          <el-input v-model="formData.accessAppId" placeholder="输入APP ID" prop="accessAppId" />
        </el-form-item>

        <el-form-item label="APP URL">
          <el-input v-model="formData.accessUrl" placeholder="输入APP URL" prop="accessUrl" />
        </el-form-item>

        <el-form-item label="APP Key">
          <el-input v-model="formData.accessAppKey" placeholder="输入APP KEY" prop="accessAppKey" />
        </el-form-item>

        <el-form-item label="APP Secret">
          <el-input v-model="formData.accessAppSecret" placeholder="输入APP SECRET" prop="accessAppSecret" />
        </el-form-item>

        <el-form-item label="Token">
          <el-input v-model="formData.accessToken" placeholder="输入Token" prop="accessToken" />
        </el-form-item>

        <el-form-item label="说明">
          <el-input v-model="formData.remark" type="textarea" placeholder="输入说明" :rows="10" prop="remark" />
        </el-form-item>
      </el-form>
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { ACTION_ADD, ACTION_UPDATE } from '@/utils/actionType'
import { getAccessById, createAccess, updateAccess } from '@/api/system/accessApi'
import { OcSelector } from '@/components'

export default {
  name: 'ClientApiAccess',
  components: { OcSelector },
  props: {
    title: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      dialogVisible: false,
      actionType: '',
      formData: {
        accessId: undefined,
        ocId: undefined,
        accessAppId: '',
        accessUrl: '',
        accessAppKey: '',
        accessAppSecret: '',
        accessToken: '',
        status: 0,
        remark: ''
      },
      rules: {
        ocId: [
          { required: true, message: '请选择单位', trigger: 'blur' }
        ],
        accessAppId: [
          { required: true, message: '请填写接入应用 APP ID', trigger: 'blur' }
        ],
        accessUrl: [
          { required: true, message: '请填写接入应用Url', trigger: 'blur' }
        ],
        accessAppKey: [
          { required: true, message: '请填写接入应用APP Key', trigger: 'blur' }
        ],
        accessAppSecret: [
          { required: false, message: '请填写接入应用APP Secret', trigger: 'blur' }
        ],
        accessToken: [
          { required: false, message: '请填写接入应用APP Token', trigger: 'blur' }
        ],
        remark: [
          { required: false, pattern: /^\d{0,511}$/, message: '长度小于511', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {

    // Show Add Model
    showAddModel() {
      this.actionType = ACTION_ADD
      this.resetFormData()
      this.dialogVisible = true
    },

    // Show Edit Model
    showEditModel(accessId) {
      this.actionType = ACTION_UPDATE
      this.resetFormData()
      this.dialogVisible = true
      getAccessById(accessId).then((resp) => {
        const { code, data, msg } = resp
        if (code === 200) {
          this.formData = data
        } else {
          this.$message.error(msg)
        }
      }).catch((error) => {
        console.log(error)
      })
    },

    // Reset Form Data
    resetFormData() {
      this.formData = {
        accessId: undefined,
        ocId: undefined,
        accessAppId: '',
        accessUrl: '',
        accessAppKey: '',
        accessAppSecret: '',
        accessToken: '',
        status: 0,
        remark: ''
      }
    },

    // Submit
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.handleCommand()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    // Handle Command
    handleCommand() {
      const actionType = this.actionType
      switch (actionType) {
        case ACTION_ADD:
          createAccess(this.formData).then((resp) => {
            const { code, msg } = resp
            if (code === 200) {
              this.dialogVisible = false
              this.$message.success(msg)
              this.formSuccess()
            } else {
              this.$message.error(msg)
            }
          }).catch((error) => {
            console.log(error)
          })
          break

        case ACTION_UPDATE:
          updateAccess(this.formData).then((resp) => {
            const { code, msg } = resp
            if (code === 200) {
              this.dialogVisible = false
              this.$message.success(msg)
              this.formSuccess()
            } else {
              this.$message.error(msg)
            }
          }).catch((error) => {
            console.log(error)
          })
          break
      }
    },

    // Report
    formSuccess() {
      this.$emit('formSuccess')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
