import { CheckOutlined, PlusOutlined } from '@ant-design/icons';
import { Button, message, Input, Drawer, Popconfirm, 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 { getPage, getUintList, getTypeList } from './services';
import { gettree } from '../../sys/org/service';

const DeliveryCheck = () => {
  const [createModalVisible, handleModalVisible] = useState(false);
  /** 分布更新窗口的弹窗 */

  const [showDetail, setShowDetail] = useState(false);
  //绑定
  const actionRef = useRef();
  const [currentRow, setCurrentRow] = useState();
  const [UintList, setUintList] = useState();
  const [TypeList, setTypeList] = useState();
  const [OrgList, setOrgList] = useState([]);
  const [OrgListArray, setOrgListArray] = useState([]);
  useEffect(() => {
    const initUintList = async (value) => {
      var data = await getUintList();
      var list = [];
      if (data.data.length > 0) {
        data.data.forEach((item) => {
          list.push({ stockuint: item.id, text: item.name, value: item.id, label: item.name });
        });
      }
      setUintList(list);
    };
    const initTypeList = async (value) => {
      var data = await getTypeList();
      var list = [];
      if (data.data.length > 0) {
        data.data.forEach((item) => {
          list.push({ typeid: item.id, text: item.name, value: item.id, label: item.name });
        });
      }
      setTypeList(list);
    };
    function initOrgList() {
      gettree().then((r) => {
        setOrgList(r.data);
        let list = {};
        formateData(r.data);
        function formateData(datas) {
          for (var i in datas) {
            list[datas[i]['value']] = { text: datas[i]['title'] };
            if (datas[i].children) {
              formateData(datas[i].children);
            }
          }
        }
        setOrgListArray(list);
      });
    }
    initOrgList();
    initUintList();
    initTypeList();
  }, []);
  const columns = [
    {
      title: '主键',
      dataIndex: 'id',
      tip: '规则名称是唯一的 key',
      hideInSearch: true,
      hideInTable: true,
      render: (dom, entity) => {
        return (
          <a
            onClick={() => {
              setCurrentRow(entity);
              setShowDetail(true);
            }}
          >
            {dom}
          </a>
        );
      },
    },
    {
      title: '收货机构',
      dataIndex: 'orgId',
      hideInTable: true,
      valueEnum: OrgListArray,
    },
    {
      title: '收货机构',
      hideInSearch: true,
      dataIndex: 'orgName',
    },
    {
      title: '物料名称',
      hideInSearch: true,
      dataIndex: 'productName',
    },
    {
      title: '物料编码',
      hideInSearch: true,
      dataIndex: 'code',
    },
    {
      title: '物料单位',
      hideInSearch: true,
      dataIndex: 'uintName',
    },
    {
      title: '库存数量',
      hideInSearch: true,
      dataIndex: 'stockQty',
    },
    {
      title: '物料规格',
      hideInSearch: true,
      dataIndex: 'specs',
    },
  ];

  return (
    <PageContainer>
      <ProTable
        headerTitle="库存查询"
        actionRef={actionRef}
        rowKey="id"
        search={{
          labelWidth: 120,
        }}
        toolBarRender={() => []}
        request={async (params) => {
          var Data = [];
          var total = 0;
          await getPage(params).then((r) => {
            Data = r.data.data;
            total = r.data.total;
          });
          return {
            data: Data,
            success: true,
            total: total,
          };
        }}
        columns={columns}
        rowSelection={{
          onChange: (_, selectedRows) => setSelectedRows(selectedRows),
        }}
      />
    </PageContainer>
  );
};

export default DeliveryCheck;
