// 添加/修改授权
<template>
  <el-dialog
    :title="isEdit ? '编辑授权' : '添加授权'"
    width="800px"
    :visible.sync="visible"
    :destroy-on-close="false"
    :close-on-click-modal="false"
    :center="true"
  >
    <el-form ref="form" :rules="rules" :model="form" label-position="right" label-width="100px">
      <el-form-item prop="id" style="margin-bottom: 0;">&nbsp;</el-form-item>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="公司名" prop="companyName" required>
            <el-input v-model="form.companyName" placeholder="请输入公司名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="使用场景" prop="scene" required>
            <el-input v-model="form.scene" placeholder="使用场景，如：学校、工厂等" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="联系人" prop="contactPerson" required>
            <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="phone" required>
            <el-input v-model="form.phone" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="派出所" prop="pcsName" required>
            <el-input v-model="form.pcsName" placeholder="请输入派出所" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="派出所编码" prop="pcsCode" required>
            <el-input v-model="form.pcsCode" placeholder="请输入派出所编码" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="区域名称" prop="areaName" required>
            <el-input v-model="form.areaName" placeholder="请输入区域名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据源" prop="dataSource" required>
            <el-input v-model="form.dataSource" placeholder="请输入数据源" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-form-item v-if="isEdit" label="token" prop="token">
        <el-input v-model="form.token" placeholder="请求验证token" readonly />
      </el-form-item> -->
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="总调用次数" prop="reqTimesLimit">
            <el-input-number v-model="form.reqTimesLimit" :min="form.reqTimesLimit" :step="1" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button type="primary" :loading="submitLoading" @click="submit">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add } from '@/api/auth'

export default {
  data() {
    return {
      form: {
        id: null,
        companyName: null,
        scene: null,
        contactPerson: null,
        phone: null,
        pcsName: null,
        pcsCode: null,
        areaName: null,
        dataSource: null,
        // token: null,
        reqTimesLimit: 0
      },
      rules: {
        companyName: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        scene: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        contactPerson: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        phone: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        pcsName: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        pcsCode: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        areaName: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        dataSource: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ]
      },
      visible: false,
      submitLoading: false,
      isEdit: false
    }
  },
  created() {},
  methods: {
    open(obj) {
      this.visible = true
      this.submitLoading = false
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        if (obj && Object.keys(obj).length > 0) {
          this.form = obj
          this.isEdit = true
        } else {
          this.isEdit = false
        }
      })
    },
    submit(e) {
      e.preventDefault()
      this.$refs.form.validate(async(valid) => {
        if (!valid) {
          return false
        }
        if (this.submitLoading) {
          return
        }
        this.submitLoading = true
        try {
          await add(this.form)
          this.visible = false
          this.$emit('success', this.isEdit)
        } catch (e) {
          console.info('add', e)
        } finally {
          this.submitLoading = false
        }
      })
    }
  }
}
</script>

<style scoped>
::v-deep .el-select {
  width: 100%;
}
</style>
