<style>
</style>
<template>
  <q-dialog v-model="persistent" content-class="bg-grey-1" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 800px;max-width:900px;">
      <q-card-section>
        <div class="text-h6">{{objOption.StoreType==0?'新增存储桶':'修改存储桶'}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <div class="row wrap">
          <div class="col-12" style="margin-bottom:10px;">
            存储位置
            <q-radio v-model="objOption.StoreType" v-for="(item,index) in statusData" :key="index" :val="item.Id" :label="item.Name" />
          </div>
          <q-input filled stack-label maxlength="100" :dense="false" v-model="objOption.Bucket" ref="Bucket"
            class="col-6 q-pr-lg q-pb-lg" label="空间名称（Bucket）" :rules="[val => !!val || '请填写空间名称']" />
          <q-input filled stack-label maxlength="100" :dense="false" v-model="objOption.Region" ref="Region"
            class="col-6 q-pr-lg q-pb-lg" label="所属地域" :rules="[val => !!val || '请填写所属地域']" />
          <q-input filled stack-label maxlength="100" :dense="false" v-model="objOption.SecretId" ref="SecretId"
            class="col-6 q-pr-lg q-pb-lg" label="SecretId" :rules="[val => !!val || '请填写SecretId']" />
          <q-input filled stack-label maxlength="100" :dense="false" v-model="objOption.SecretKey" ref="SecretKey"
            class="col-6 q-pr-lg q-pb-lg" label="SecretKey" :rules="[val => !!val || '请填写SecretKey']" />
          <q-input filled stack-label maxlength="100" :dense="false" v-model="objOption.CustomDomain" ref="CustomDomain"
            class="col-6 q-pr-lg q-pb-lg" label="自定义域名"/>
          <q-input filled stack-label maxlength="100" :dense="false" v-model="objOption.UploadDomain" ref="UploadDomain"
            class="col-6 q-pr-lg q-pb-lg" label="上传自定义域名"/>
        </div>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeSaveForm" />
        <q-btn label="立即提交" color="accent" class="q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="setConfig" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    setSiteConfig,
  } from '../../api/system/sitecfg'
  import {
    getConfigStatus,
  } from '../../api/system/index'

  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        saveLoading: false,
        objOption: {
          ID:0,
          StoreType: 0,
          Bucket: '',
          Region: '',
          CustomDomain: '',
          SecretId: '',
          SecretKey: '',
          ImgStyle: '',
          UploadDomain:''
        },
        statusData:[], //状态数据
      }
    },
    computed: {
    },
    mounted() {
      if(this.saveObj){
        this.objOption.ID = this.saveObj.ID;
        this.objOption.StoreType=this.saveObj.StoreType;
        this.objOption.Bucket=this.saveObj.Bucket;
        this.objOption.Region=this.saveObj.Region;
        this.objOption.CustomDomain=this.saveObj.CustomDomain;
        this.objOption.SecretId=this.saveObj.SecretId;
        this.objOption.SecretKey=this.saveObj.SecretKey;
        this.objOption.UploadDomain=this.saveObj.UploadDomain;
      }

      this.getStatus();
    },
    methods: {
      //保存数据
      setConfig() {
        if(this.objOption.StoreType==0){
          this.$q.notify({
            type: 'negative',
            position: "top",
            message: `请选择存储位置`
          })
          return;
        }
        if(this.objOption.Bucket==''){
          this.$q.notify({
            type: 'negative',
            position: "top",
            message: `请填写空间域名`
          })
          return;
        }
        if(this.objOption.Region==''){
          this.$q.notify({
            type: 'negative',
            position: "top",
            message: `请填写所属地域`
          })
          return;
        }
        if(this.objOption.SecretId==''){
          this.$q.notify({
            type: 'negative',
            position: "top",
            message: `请填写SecretId`
          })
          return;
        }
        if(this.objOption.SecretKey==''){
          this.$q.notify({
            type: 'negative',
            position: "top",
            message: `请填写SecretKey`
          })
          return;
        }
        setSiteConfig(this.objOption).then(res => {
          this.$q.notify({
            icon: 'iconfont icon-chenggong',
            color: 'accent',
            timeout: 2000,
            message: '数据保存成功！',
            position: 'top'
          })
          this.$emit("success");
          this.closeSaveForm();
        }).catch(() => {
          this.saveLoading = false
        })
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      //获去下拉状态
      getStatus(){
        getConfigStatus().then(res => {
          if(res.Code==1){
            this.statusData = res.Data;
          }
        })
      },

    },
  }

</script>
