<template>
  <CollapseContainer title="OSS设置" :canExpan="false">
    <Row :gutter="24">
      <Col :span="24">
        <BasicForm @register="register" />
      </Col>
      <Col :span="12" :offset="8">
        <c-button type="primary" @click="handleSubmit"> 保存默认 </c-button>
      </Col>
    </Row>
  </CollapseContainer>
</template>
<script lang="ts">
  import { Row, Col } from 'ant-design-vue';
  import { defineComponent, onMounted, ref, unref } from 'vue';
  import { BasicForm, useForm, CollapseContainer } from '@jdlinker/ui';
  import { ossSetschemas } from './data';
  import { ossSave, ossList } from './setting_api';

  const isUpdate = ref(false);
  export default defineComponent({
    components: {
      BasicForm,
      CollapseContainer,
      Row,
      Col
    },
    setup() {
      const [register, { setFieldsValue, validate }] = useForm({
        labelWidth: 120,
        schemas: ossSetschemas,
        showActionButtonGroup: false
      });

      onMounted(async () => {
        const data = await ossList(null);
        if (data && data.records.length > 0) {
          isUpdate.value = true;
          setFieldsValue(data.records[0]);
        }
      });

      /**
       *更新基本信息
       * */
      async function handleSubmit() {
        try {
          let values = await validate();
          await ossSave(values, unref(isUpdate));
        } catch (e) {
          console.log('e', e);
        }
      }
      return {
        register,
        handleSubmit
      };
    }
  });
</script>

<style lang="less" scoped></style>
