<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author gaoxq
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'biz:cities:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="70%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsBizCitiesForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '@jeesite/core/hooks/web/useI18n';
  import { useMessage } from '@jeesite/core/hooks/web/useMessage';
  import { router } from '@jeesite/core/router';
  import { Icon } from '@jeesite/core/components/Icon';
  import { BasicForm, FormSchema, useForm } from '@jeesite/core/components/Form';
  import { BasicDrawer, useDrawerInner } from '@jeesite/core/components/Drawer';
  import { BizCities, bizCitiesSave, bizCitiesForm } from '@jeesite/biz/api/biz/cities';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('biz.cities');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<BizCities>({} as BizCities);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增市区信息') : t('编辑市区信息'),
  }));

  const inputFormSchemas: FormSchema<BizCities>[] = [
	{
	  label: t('省份名称'),
	  field: 'provinceCode',
	  fieldLabel: 'provinceName',
	  component: 'ListSelect',
	  componentProps: {
	    selectType: 'bizProvSelect',
	  },
	  required: true,
	},
    {
      label: t('市区编码'),
      field: 'cityCode',
      component: 'Input',
      componentProps: {
        maxlength: 24,
      },
      required: true,
    },
    {
      label: t('市区名称'),
      field: 'cityName',
      component: 'Input',
      componentProps: {
        maxlength: 125,
      },
      required: true,
    },
    {
      label: t('市区区号'),
      field: 'areaCode',
      component: 'Input',
      componentProps: {
        maxlength: 12,
      },
      required: true,
    },
    {
      label: t('市区级别'),
      field: 'areaType',
      component: 'Select',
      componentProps: {
        dictType: 'area_type',
        allowClear: true,
      },
      required: true,
    },
    {
      label: t('数据状态'),
      field: 'dataStatus',
      component: 'Select',
      componentProps: {
        dictType: 'ustatus',
        allowClear: true,
      },
      required: true,
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm<BizCities>({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await bizCitiesForm(data);
    record.value = (res.bizCities || {}) as BizCities;
    record.value.__t = new Date().getTime();
    await setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id || data.id,
      };
      // console.log('submit', params, data, record);
      const res = await bizCitiesSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
