import { PlusOutlined } from '@ant-design/icons';
import { Button, message, Input, Drawer, Modal } from 'antd';
import React, { useState, useRef, useEffect } from 'react';
import { PageContainer, FooterToolbar } from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import ProDescriptions from '@ant-design/pro-descriptions';
import api from '../services';
import AddBomInfo from './AddBomInfo';

const BomList = (props) => {
  /** 新建/更新窗口的弹窗 */
  const actionRef = useRef();
  const [AddBomModalVisible, handleAddBomModalVisible] = useState(false);
  const [currentRow, setCurrentRow] = useState();
  const [stockGoods, setstockGoods] = useState([]);
  useEffect(() => {
    /** 获取原料名称*/
    function initStockGoods() {
      api.getbygoods({IsMain: props.IsMain,goodsId: props.values.goodsId}).then((r) => {
        var arr = r.data;
        arr.forEach((element) => {
          element.text = element.name;
          element.stockGoodsId = element.value;
        });
        setstockGoods(arr);
      });
    }
    initStockGoods(); //回调原料信息
  }, []);
  /**
   * 更新节点
   *
   * @param fields
   */

  function handleUpdate(fields) {
    try {
      api
        .DeleteEntry(fields.id)
        .then((r) => {
          if (r.data) {
            message.success('配置成功');
          } else {
            message.error('配置失败请重试！');
          }
          //刷新数据
          actionRef.current.reload();
        });
      return true;
    } catch (error) {
      message.error('配置失败请重试！');
      return false;
    }
  }


/**
  * 添加节点
  *
  * @param fields
  */
 function handleAddEntry(fields) {
  try {
    api.AddUpdateEntry(JSON.stringify(fields)).then((r) => {
      if (r.data) {
        message.success('添加成功');
        //刷新数据
        actionRef.current.reload();
      } else {
        message.error('添加失败请重试！');
      }
    });
    return true;
  } catch (error) {
    message.error('添加失败请重试！');
    return false;
  }
}
  /** 国际化配置 */

  const columns = [
    {
      title: '主键',
      dataIndex: 'id',
      hideInTable: true,
      hideInSearch: true,
      tip: '规则名称是唯一的 key',
      render: (dom, entity) => {
        return (
          <a
            onClick={() => {
              setCurrentRow(entity);
              setShowDetail(true);
            }}
          >
            {dom}
          </a>
        );
      },
    },
    {
      title: '原料名称',
      dataIndex: 'batchingName',
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => [
        <a
          key="link4"
          onClick={() => {
            handleUpdate(record);
          }}
        >
          删除
        </a>
      ],
    },
  ];
  return (
    <Modal
      title={props.IsMain?'主料配置':'辅料配置'}
      width={640}
      visible={props.createModalVisible}
      bodyStyle={{ padding: '32px 40px 48px' }}
      footer={null}
      onCancel={() => {
        props.onCancel();
      }}
      destroyOnClose
    >
      <ProTable
        headerTitle=""
        actionRef={actionRef}
        rowKey="id"
        pagination={{ defaultPageSize: 20 }}
        search={false}
        toolBarRender={() => [
          <Button type="primary" key="create" onClick={() => handleAddBomModalVisible(true)}>
            <PlusOutlined /> 添加原料
          </Button>,
        ]}
        //数据绑定
        request={async (params) => {
          var goodsBomsData = [];
          var total = 0;
          if (props.values.id != undefined) {
            params['IsMain'] = props.IsMain;
            params['StirFryTimeId'] = props.values.id;
          }
          await api.GetEntryPage(params).then((r) => {
            goodsBomsData = r.data.data;
            total = r.data.total;
          });
          return {
            data: goodsBomsData,
            success: true,
            total: total,
          };
        }}
        columns={columns}
      />
       {/*商品配方(新增，修改) */}
       <AddBomInfo
        onFinish={async (value) => {
          value['StirFryTimeId'] = props.values.id;
          value['IsMain'] = props.IsMain;
          var success = false;
          success = await handleAddEntry(value);
          if (success) {
            handleAddBomModalVisible(false);
          }
          actionRef.current.reload();
        }}
        onCancel={() => {
          handleAddBomModalVisible(false);
        }}
        createModalVisible={AddBomModalVisible}
        stockGoodsData={stockGoods}
      />
    </Modal>
  );
};

export default BomList;
