<template>
  <BasicModal
  :maskClosable="false"
    :width="500"
    @register="registerModal"
    v-bind="$attrs"
    :title="getTitle"
    @ok="handleSubmit"
  >
    <div class="modal-box">
      <div><BasicForm @register="registerForm" /></div>
    </div>
  </BasicModal>
</template>
<script lang="ts">
import { defineComponent, ref, computed, unref, reactive, onMounted } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
const { createMessage } = useMessage();
import { useMessage } from '/@/hooks/web/useMessage';
import { getStationCodeUpdata, getStationCodeAdd } from '/@/api/swHome/index';
import { useUserStore } from '/@/store/modules/user';
import { tenantFormSchema } from '../columnsCode';
const userStore = useUserStore();

export default defineComponent({
  name: 'AccountModal',
  components: { BasicModal, BasicForm },
  setup(_, { emit }) {
    let isUpdate = ref('');
    const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
      isUpdate.value = data.isUpdate;
      if(isUpdate.value){
        setFieldsValue(data.data);
      }else{
        resetFields()
      }
      
    });
    const [registerForm, { setFieldsValue, updateSchema, resetFields, validate, getFieldsValue }] =
      useForm({
        labelWidth: 140,
        schemas: tenantFormSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 18,
        },
      });
    const getTitle = isUpdate.value ? ref('编辑') : ref('新增');
    async function handleSubmit() {
      try {
        
       
        
        // 编辑
        if(isUpdate.value){
          const values = await validate(['d_type', 'd_name', 'ycstcd', 'ybstcd', 'stnm', 'maxvalue', 'ordern']);
          getStationCodeUpdata([values]).then((res) => {
            if (res.code == 200) {
              createMessage.success('编辑成功！');
              closeModal();
              emit('success');
            } else {
              createMessage.warning(res.msg);
            }
          });
        }else{
          let formData = getFieldsValue();
          // 新增
          getStationCodeAdd([formData]).then((res) => {
            if (res.code == 200) {
              createMessage.success('新增成功！');
              closeModal();
              emit('success');
            } else {
              createMessage.warning(res.msg);
            }
          });
        }
        
      } finally {
        setTimeout(() => {
          setModalProps({ confirmLoading: false });
        }, 300);
      }
    }
    return {
      isUpdate,
      registerModal,
      getTitle,
      handleSubmit,
      registerForm,
    };
  },
});
</script>
<style lang="less" scoped>
// .modal-box {
//   display: flex;
//   & > div:nth-child(1) {
//     width: 40%;
//   }
//   & > div:nth-child(2) {
//     width: 60%;
//   }
// }
::v-deep(.ant-calendar-picker) {
  width: 100% !important;
}
::v-deep(.ant-input-number) {
  width: 100% !important;
}
</style>
