<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="客户端ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'clientId', validatorRules.clientId]" placeholder="请输入客户端ID" :disabled="model.clientId"></a-input>
        </a-form-item>
          
        <a-form-item label="客户端秘钥" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input :type="model.clientId ? 'password':''" v-decorator="[ 'clientSecret', validatorRules.clientSecret]" placeholder="请输入客户端秘钥"></a-input>
        </a-form-item>
          
        <a-form-item label="范围" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'scope', validatorRules.scope]" placeholder="请输入范围" :disabled="readOnly"></a-input>
        </a-form-item>
          
        <a-form-item label="认证模式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'authorizedGrantTypes', validatorRules.authorizedGrantTypes]" placeholder="请输入认证模式" :disabled="readOnly"></a-input>
        </a-form-item>
          
        <a-form-item label="访问令牌有效期(秒)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'accessTokenValidity', validatorRules.accessTokenValidity]" placeholder="" style="width: 100%" :disabled="readOnly"/>
        </a-form-item>
          
        <a-form-item label="刷新令牌有效期(秒)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'refreshTokenValidity', validatorRules.refreshTokenValidity]" placeholder="" style="width: 100%" :disabled="readOnly"/>
        </a-form-item>
          
        <a-form-item label="自动授权" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'autoapprove', validatorRules.autoapprove]" placeholder="" :disabled="readOnly"></a-input>
        </a-form-item>
          
        
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  
  export default {
    name: "OauthClientDetailsModal",
    components: { 
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        readOnly: true,
        confirmLoading: false,
        validatorRules:{
        clientId:{rules: [{ required: true, message: '请输入客户端ID!' }]},
        resourceIds:{},
        clientSecret:{rules: [{ required: true, message: '请输入客户端秘钥!' }]},
        scope:{rules: [{ required: true, message: '请输入范围!' }],initialValue: 'all'},
        authorizedGrantTypes:{rules: [{ required: true, message: '请输入认证模式!' }],initialValue: 'client_credentials,password,refresh_token'},
        webServerRedirectUri:{},
        authorities:{},
        accessTokenValidity:{rules: [{ required: true, message: '请输入访问令牌有效期!' }],initialValue: '7200'},
        refreshTokenValidity:{rules: [{ required: true, message: '请输入刷新令牌有效期!' }],initialValue: '604800'},
        additionalInformation:{},
        autoapprove:{rules: [{ required: true, message: '请输入自动授权!' }],initialValue: 'false'},
        },
        url: {
          add: "/oauth/oauthClientDetails/add",
          edit: "/oauth/oauthClientDetails/edit",
        }
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'clientId','resourceIds','clientSecret','scope','authorizedGrantTypes','webServerRedirectUri','authorities','accessTokenValidity','refreshTokenValidity','additionalInformation','autoapprove'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.clientId){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'clientId','resourceIds','clientSecret','scope','authorizedGrantTypes','webServerRedirectUri','authorities','accessTokenValidity','refreshTokenValidity','additionalInformation','autoapprove'))
      }
      
    }
  }
</script>