<template>
  <BasicModal width="800px" v-bind="$attrs" @register="register" @ok="confirm" :title="title">
    <BasicForm @register="registerForm" :model="model" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, PropType, reactive, ref, toRefs } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { schemas } from './data';
  import moment from 'moment';
  interface PopupData {
    title: string;
  }
  interface Role {
    id: string | number;
  }

  export default defineComponent({
    components: { BasicModal, BasicForm },
    props: {
      popupData: {
        type: Object as PropType<PopupData>,
        default: () => {},
      },
    },
    emits: ['saveData'],
    setup(props, { emit }) {
      const popupData = props.popupData as PopupData;
      const modelRef = ref({});
      const role = reactive<Role>({
        id: 0,
      });
      const { createMessage } = useMessage();
      const { error } = createMessage;

      const [registerForm, { updateSchema, setFieldsValue, resetFields, validate }] = useForm({
        labelWidth: 120,
        schemas,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      //   const [register, { closeModal }] = useModalInner((data) => {
      //     modelRef.value = data;

      //
      //   });
      const [register, { closeModal }] = useModalInner((data) => {
        resetFields();
        if (data.id) {
          role.id = data.id;
          updateSchema([
            {
              field: 'year',
              componentProps: {
                disabled: true,
              },
            },
            {
              field: 'duty',
              componentProps: {
                disabled: true,
              },
            },
          ]);
        } else {
          role.id = 0;
          data.car = '';
          updateSchema([
            {
              field: 'year',
              componentProps: {
                disabled: false,
              },
            },
            {
              field: 'duty',
              componentProps: {
                disabled: false,
              },
            },
          ]);
        }
        // 时间戳处理
        data.starttime = data.starttime * 1000;
        data.endtime = data.endtime * 1000;
        setFieldsValue(data);
      });

      async function confirm() {
        try {
          const data = await validate();
          console.log(`确定`, data);
          data.id = role.id;
          if (data.plan && data.plan[0]) {
            const plan: any[] = [];
            data.plan.map((item) => {
              if (item.id) {
                plan.push(item.id);
              }
            });
            data.plan = plan.toString();
          }
          if (data.apply && data.apply[0]) {
            const apply: any[] = [];
            data.apply.map((item) => {
              if (item.id) {
                apply.push(item.id);
              }
            });
            data.apply = apply.toString();
          }
          if (data.actual && data.actual[0]) {
            const actual: any[] = [];
            data.actual.map((item) => {
              if (item.id) {
                actual.push(item.id);
              }
            });
            data.actual = actual.toString();
          }
          if (data.car) {
            data.car = JSON.stringify(data.car);
          }
          data.starttime = moment(data.starttime).unix();
          data.endtime = moment(data.endtime).unix();
          const popupData = { closeModal, data };
          emit('saveData', popupData);
        } catch (err: any) {
          console.log(`err`, err);
          error(err.errorFields[0].errors[0]);
        }
      }
      return {
        register,
        schemas,
        registerForm,
        model: modelRef,
        confirm,
        ...toRefs(popupData),
      };
    },
  });
</script>
<style lang="less">
  .ant-form-item-label {
    overflow: hidden;
    text-align: center !important;
    // white-space: pre-wrap !important;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  @media (max-width: 639px) {
    .ant-form-item-label {
      line-height: 2.5715 !important;
      text-align: center !important;
    }
  }
</style>
