<template>
  <CollapseContainer title="短信设置" :canExpan="false">
    <a-row :gutter="24">
      <a-col :span="24">
        <BasicForm @register="register" />
      </a-col>
      <a-col :span="12" :offset="8">
        <c-button v-show="hasPermission('system:setting:msg')" type="primary" @click="handleSubmit"> 保存默认 </c-button>
      </a-col>
    </a-row>
  </CollapseContainer>
</template>
<script lang="ts">
  import { Row, Col } from 'ant-design-vue';
  import { defineComponent, onMounted, ref, unref } from 'vue';
  import { BasicForm, useForm } from '@jdlinker/ui';
  import { CollapseContainer } from '@jdlinker/ui';
  import { msgSetschemas } from './data';
  import { msgSave, msgList } from './setting_api';
  import { usePermission } from '@jdlinker/func';
  const { hasPermission } = usePermission();
  const isUpdate = ref(false);

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

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

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

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