<script setup lang="ts">
import { computed, reactive, watch } from 'vue';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { $t } from '@/locales';
import { enablePropertyFLAGOptions } from '@/constants/business';
import { AddGoodsTypeInfo, UpDateGoodsTypeInfo } from '@/service/api/planmg';

defineOptions({
  name: 'MtlTypeOperateDrawer'
});

interface Props {
  /** the type of operation */
  operateType: NaiveUI.TableOperateType;
  /** the edit row data */
  rowData?: Api.Material.GoodsType | null;
}

const props = defineProps<Props>();

interface Emits {
  (e: 'submitted'): void;
}

const emit = defineEmits<Emits>();

const visible = defineModel<boolean>('visible', {
  default: false
});

const { formRef, validate, restoreValidation } = useNaiveForm();
const { defaultRequiredRule } = useFormRules();

const title = computed(() => {
  const titles: Record<NaiveUI.TableOperateType, string> = {
    add: '新增物料类型',
    edit: '编辑物料类型'
  };
  return titles[props.operateType];
});

type Model = Pick<
  Api.Material.GoodsType,
  'GOODS_TYPE_CODE' | 'GOODS_TYPE_NAME' | 'GOODS_TYPE_REMARK' | 'GOODS_TYPE_FLAG'
>;

const model: Model = reactive(createDefaultModel());

function createDefaultModel(): Model {
  return {
    GOODS_TYPE_CODE: '',
    GOODS_TYPE_NAME: '',
    GOODS_TYPE_REMARK: '',
    GOODS_TYPE_FLAG: ''
  };
}

type RuleKey = Exclude<keyof Model, 'roleDesc'>;

const rules: Record<RuleKey, App.Global.FormRule> = {
  GOODS_TYPE_CODE: defaultRequiredRule,
  GOODS_TYPE_NAME: defaultRequiredRule,
  GOODS_TYPE_REMARK: {},
  GOODS_TYPE_FLAG: defaultRequiredRule
};

function handleInitModel() {
  Object.assign(model, createDefaultModel());

  if (props.operateType === 'edit' && props.rowData) {
    Object.assign(model, props.rowData);
  }
}

function closeDrawer() {
  visible.value = false;
}

async function handleSubmit() {
  await validate();
  if (props.operateType === 'edit') {
    // 修改信息
    const delinfo = await UpDateGoodsTypeInfo(model);
    if (delinfo.response !== undefined) {
      if (delinfo.response.data.code === '0000') {
        window.$message?.success(delinfo.response.data.msg as string);
        closeDrawer();
        emit('submitted');
      }
    }
  } else {
    // 新增账户
    const delinfo = await AddGoodsTypeInfo(model);
    if (delinfo.response !== undefined) {
      if (delinfo.response.data.code === '0000') {
        window.$message?.success(delinfo.response.data.msg as string);
        closeDrawer();
        emit('submitted');
      }
    }
  }
}

watch(visible, () => {
  if (visible.value) {
    handleInitModel();
    restoreValidation();
  }
});
</script>

<template>
  <NDrawer v-model:show="visible" display-directive="show" :width="360">
    <NDrawerContent :title="title" :native-scrollbar="false" closable>
      <NForm ref="formRef" :model="model" :rules="rules">
        <NFormItem label="物料名称" path="GOODS_TYPE_NAME">
          <NInput v-model:value="model.GOODS_TYPE_NAME" placeholder="请输入物料名称" />
        </NFormItem>
        <NFormItem label="编码" path="GOODS_TYPE_CODE">
          <NInput v-model:value="model.GOODS_TYPE_CODE" placeholder="请输入物料编码" />
        </NFormItem>
        <NFormItem label="状态" path="GOODS_TYPE_FLAG">
          <NRadioGroup v-model:value="model.GOODS_TYPE_FLAG">
            <NRadio
              v-for="item in enablePropertyFLAGOptions"
              :key="item.value"
              :value="item.value"
              :label="$t(item.label)"
            />
          </NRadioGroup>
        </NFormItem>
        <NFormItem label="备注" path="GOODS_TYPE_REMARK">
          <NInput v-model:value="model.GOODS_TYPE_REMARK" placeholder="备注信息" />
        </NFormItem>
      </NForm>
      <template #footer>
        <NSpace :size="16">
          <NButton @click="closeDrawer">{{ $t('common.cancel') }}</NButton>
          <NButton type="primary" @click="handleSubmit">{{ $t('common.confirm') }}</NButton>
        </NSpace>
      </template>
    </NDrawerContent>
  </NDrawer>
</template>

<style scoped></style>
