<template>
  <a-modal v-model:open="visible" :title="title" @ok="handleOk" width="50%">
    <a-form :model="form" layout="vertical" ref="formRef" :rules="rules">
      <a-row :gutter="[16, 16]">
        <a-col :span="24">
          <a-form-item label="名称" name="name">
            <a-input v-model:value="form.name" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="[16, 16]">
        <a-col :span="24">
          <a-form-item label="状态" name="status">
            <a-select v-model:value="form.status" :options="statusOptions" placeholder="请选择" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="[16, 16]">
        <a-col :span="24">
          <a-form-item label="规格" name="standards">
            <a-input v-model:value="form.standards" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="[16, 16]">
        <a-col :span="24">
          <a-form-item label="单价(元)" name="price">
            <a-input v-model:value="form.price" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="[16, 16]">
        <a-col :span="24">
          <a-form-item label="数量" name="amount">
            <a-input v-model:value="form.amount" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="[16, 16]">
        <a-col :span="24">
          <a-form-item label="单位" name="unit">
            <a-input v-model:value="form.unit" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="[16, 16]">
        <a-col :span="24">
          <a-form-item label="备注" name="remark">
            <a-textarea v-model:value="form.remark" :rows="4" placeholder="请输入" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>

<script>
  import { reactive, ref } from 'vue';
  import { message } from 'ant-design-vue';
  import {
    getInfo,
    add,
    update,
  } from './api';
  export default {
    setup() {
      const title = ref('新增');
      const visible = ref(false);
      const form = reactive({
        name: '',
        status: null,
        remark: '',
        unit: '',
        price: '',
        amount: '',
        standards: '',
        projectId:null,
        id:null
      });
      //下拉框
      const statusOptions = [
        {
          value: 0,
          label: '启用',
        },
        {
          value: 1,
          label: '禁用',
        },
      ];
      const showModal = async(type, id,projectId) => {
        console.log(projectId)
        visible.value = true;
        if (type == 1) {
          title.value = '新增';
        } else if (type == 2) {
          title.value = '编辑';
          const data = await getInfo(id);
          for (let i in form) {
            form[i] = data[i];
          }
        }
        form.projectId = projectId
      };

      const handleOk = () => {
        formRef.value.validate().then((valid) => {
          if (valid) {
            if (title.value == '新增') {
              let params = {};
              for (let i in form) {
                params[i] = form[i];
              }
              add(params).then((_) => {
                message.success('新增成功');
                visible.value = false;
              });
            } else {
              let params = {};
              for (let i in form) {
                params[i] = form[i];
              }
              update(params).then((_) => {
                message.success('编辑成功');
                visible.value = false;
              });
            }
          }
        });
      };
      const closeModal = () => {
        formRef.value.resetFields();
        visible.value = false;
      };
      const formRef = ref()
      const rules = {        
        name: [{ required: true, message: '请输入' }],
        status: [{ required: true, message: '请选择' }],
        remark: [{ required: true, message: '请输入' }],
        unit: [{ required: true, message: '请输入' }],
        price: [{ required: true, message: '请输入' }],
        amount: [{ required: true, message: '请输入' }],
        standards: [{ required: true, message: '请输入' }],
      };
      return {
        visible,
        title,
        form,
        showModal,
        handleOk,
        statusOptions,
        closeModal,
        formRef,
        rules
      };
    },
  };
</script>

<style scoped>
  /* 可选样式调整 */
  .ant-modal-body {
    max-width: 600px;
    margin: 0 auto;
  }
</style>
