<template>
  <CollapseContainer title="基本配置" :canExpan="false">
    <Row :gutter="24">
      <Col :span="24">
        <BasicForm @register="register" />
      </Col>
      <Col :span="12" :offset="8">
        <Button type="primary" @click="handleSubmit">保存默认</Button>
      </Col>
    </Row>
  </CollapseContainer>
</template>
<script lang="ts">
  import { Button, Row, Col } from 'ant-design-vue';
  import { defineComponent, onMounted } from 'vue';
  import { BasicForm, useForm } from '@jdlinker/ui';
  import { CollapseContainer } from '@jdlinker/ui';
  import { useMessage } from '@jdlinker/func';
  import { baseSetting } from './setting_api';
  import { baseSetschemas } from './data';
  import { defHttp } from '@jdlinker/func';

  export default defineComponent({
    components: {
      BasicForm,
      CollapseContainer,
      Button,
      Row,
      Col
    },
    setup() {
      const { createMessage } = useMessage();

      const [register, { setFieldsValue, validate }] = useForm({
        labelWidth: 120,
        schemas: baseSetschemas,
        showActionButtonGroup: false,
        baseColProps: { span: 24 }
      });

      onMounted(async () => {
        const data = await baseSetting({});
        setFieldsValue(data);
      });

      function updateAvatar(src: string, data: string) {
        if (data) {
          defHttp.post({ url: '/sys/base/config', params: { logo: data } });
        }
      }
      /**
       *更新基本信息
       * */
      async function handleSubmit() {
        try {
          let values = await validate();
          //提交表单
          defHttp.post({ url: '/sys/base/config', params: values });
          createMessage.success('更新成功');
        } catch (e) {
          console.log('e', e);
        }
      }

      return {
        register,
        handleSubmit
      };
    }
  });
</script>

<style lang="less" scoped>
  .change-avatar {
    img {
      display: block;
      margin-bottom: 15px;
      border-radius: 50%;
    }
  }
</style>
