import React, { useState, useEffect } from 'react';
import { message, Modal, Space } from 'antd';
import ProForm, {
  ModalForm,
  ProFormText,
  ProFormDatePicker,
  ProFormSwitch,
  ProFormDigitRange,
  ProFormList,
  ProFormDependency,
  ProFormSelect,
  ProFormDigit
} from '@ant-design/pro-form';
import { add, Update } from '../services';

const CreateForm = (props) => {

  const [dataSource, setDataSource] = useState([]);


  // 新增
  const handleAdd = async (values) => {
    try {
      var re = await add(values);
      if (re.data) {
        message.success("请求成功");
      } else {
        message.error("请求失败");
      }
      return re.data;
    } catch (error) {
      message.error('操作异常');
      return false;
    }
  };

  // 修改
  const handleUpdate = async (values) => {
    try {

      var re = await Update(values);
      if (re.data) {
        message.success("请求成功");
      } else {
        message.error("请求失败");
      }
      return re.data;
    } catch (error) {
      message.error('操作异常');
      return false;
    }
  };


  return (
    <ModalForm
      labelCol={{ span: 3 }}
      title="采购信息"
      width={1200}
      height={600}
      // layout="horizontal"
      formRef={props.formRef}
      visible={props.modalVisit}
      onVisibleChange={props.setModalVisit}
      initialValues={{
        state: true,
      }}
      onFinish={async (values) => {
        var data = false;
        if (props.formRef?.current?.getFieldValue('id')) {

          data = await handleUpdate(values);
        } else {
          data = await handleAdd(values);
        }
        if (data) {
          props.actionRef.current.reload();
        }
        return data;
      }}
    >
      <ProFormDigitRange name="id" hidden={true} />
      <ProFormText name="receivingUser" label="收货人" rules={[{ required: true, max: 20 }]} />
      <ProFormText name="receivingTel" label="收货人电话" rules={[{ required: true, max: 20 }]} />
      <ProFormText name="receivingAddress" label="收货地址" rules={[{ required: true, max: 300 }]} />
      <ProFormList
        creatorButtonProps={false}
        // itemRender={(doms,listMeta)=>{
        //   console.log(doms.action);
        //   return   <ProForm.Group alwaysShowItemLabel={false}>{doms.listDom}</ProForm.Group>;
        // }}
        name="purchaseOrderEntryLits"
        itemContainerRender={(doms) => {
          //console.log(doms);
          return <ProForm.Group >{doms}</ProForm.Group>;
        }}
      >
        {(f, index, action) => {
          return (
            <>
              <Space >
                <ProFormSelect name="aittribute" label="物料属性" width="110px" disabled
                  request={async (params) => [
                    { label: '成品', value: 0 },
                    { label: '半成品', value: 1 },
                    { label: '易耗品', value: 2 },
                  ]} />
                <ProFormSelect name="productType" label="物料类型" width="110px" disabled
                  request={async (params) => {
                    var list = [];
                    props.itemType.map((item) => {
                      list.push({ label: item.name, value: item.id });
                    });
                    return list;
                  }
                  }
                />
                <ProFormSelect name="itemId" label="物料" width="140px" disabled
                  request={async (params) => {
                    var list = [];
                    props.batchinglist.map((item) => {
                      list.push({ label: item.batching_Name, value: item.id });
                    });
                    return list;
                  }
                  }
                />
                <ProFormText name="supplierId" label="供应商" width="110px" disabled />
                <ProFormDigit name="barcode" label="条码" width="120px" disabled />
                <ProFormText name="purchaseQty" label="采购数量" width="80px"
                  onChange={async (value) => {
                    var data = props.formRef?.current?.getFieldValue('purchaseOrderEntryLits');
                    data[index].qty = data[index].purchaseQty * data[index].proportion
                    props.formRef?.current?.setFieldsValue({ purchaseOrderEntryList: data });

                    // props.SetItemLIst(props.itemLIstAll[value]);
                    // var data=form.getFieldValue('purchaseOrderEntryList');
                    // data[key].itemId=undefined;
                    // data[key].barcode=undefined;
                    // form.setFieldsValue({ purchaseOrderEntryList:data });
                  }}
                />
                <ProFormSelect name="packUnit" label="物料单位" width="110px" disabled
                  request={async (params) => {
                    var list = [];
                    props.uint.map((item) => {
                      list.push({ label: item.name, value: item.id });
                    });
                    return list;
                  }
                  } />
                <ProFormText name="proportion" label="比例" width="60px" disabled />
                <ProFormDigit name="qty" label="库存数量" width="60px" disabled />
                <ProFormSelect name="stockUint" label="库存单位" width="100px" disabled
                  request={async (params) => {
                    var list = [];
                    props.uint.map((item) => {
                      list.push({ label: item.name, value: item.id });
                    });
                    return list;
                  }
                  } />
              </Space>
            </>
          );
        }}
      </ProFormList>
    </ModalForm>
  );
};

export default CreateForm;
