<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
    wrapClassName="custom-modal-box"
    :width="600"
  >
    <div class="custom-modal-padding-box">
      <a-row> 产品编码： {{ baseInfo?.code }} </a-row>
      <a-row> 产品名称： {{ baseInfo?.name }} </a-row>
      <a-row> 规格型号： {{ baseInfo?.model }} </a-row>
      <a-row> 产品单位： {{ baseInfo?.unitName }} </a-row>
      <a-row class="count-row"> 当前数量： {{ baseInfo?.inventory }} </a-row>
      <div class="basic-box">
        <BasicForm @register="registerForm" />
      </div>
    </div>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { FormSchema } from '/@/components/Table';
  import { updateMaterialStock } from '/@/api/erp/material/list';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';

  const { t } = useI18n();
  const CheckFormSchema: FormSchema[] = [
    {
      field: 'type',
      label: '操作类型',
      component: 'Select',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请选择操作类型',
        allowClear: false,
        options: [
          { label: '增加库存', value: 1 },
          { label: '减少库存', value: -1 },
        ],
      },
    },
    {
      field: 'count',
      label: '操作数量',
      component: 'InputNumber',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请输入操作数量',
      },
    },
    {
      field: 'remark',
      label: '备注',
      component: 'InputTextArea',
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请输入备注',
      },
    },
  ];

  const ScrapFormSchema: FormSchema[] = [
    {
      field: 'count',
      label: '报废数量',
      component: 'InputNumber',
      required: true,
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请输入报废数量',
      },
    },
    {
      field: 'remark',
      label: '备注',
      component: 'InputTextArea',
      colProps: { span: 24 },
      componentProps: {
        placeholder: '请输入备注',
      },
    },
  ];

  const { notification } = useMessage();
  const rowId = ref('');
  const getTitle = ref('');
  const type = ref('');
  const baseInfo = ref();
  const formSchema = ref<any[]>([]);
  const emit = defineEmits(['success', 'register']);

  const [registerForm, { resetFields, validate }] = useForm({
    labelWidth: 100,
    layout: 'vertical',
    schemas: formSchema,
    showActionButtonGroup: false,
    actionColOptions: {
      span: 23,
    },
  });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false, destroyOnClose: true });

    rowId.value = data.id;
    baseInfo.value = data.baseInfo;
    type.value = data.type;
    if (type.value === 'check') {
      getTitle.value = '盘点';
      formSchema.value = CheckFormSchema;
    } else {
      getTitle.value = '报废';
      formSchema.value = ScrapFormSchema;
    }
  });

  const handleSubmit = async () => {
    try {
      const values = await validate();
      values.id = rowId.value;
      if (type.value === 'scrap') values.type = 0;
      setModalProps({ confirmLoading: true });

      await updateMaterialStock(values);
      notification.success({
        message: getTitle.value,
        description: t('成功'),
      });

      closeModal();
      emit('success');
    } catch (error) {
      setModalProps({ confirmLoading: false });
    }
  };
</script>
<style lang="less" scoped>
  .ant-row {
    padding: 8px 0;
    font-weight: 400;
    font-size: 14px;
    color: #333;
  }

  .basic-box {
    margin-top: 16px;
  }

  .custom-modal-padding-box {
    height: 100%;
    padding: 0 16px;
    padding-bottom: 60px;
  }
</style>
