import { FontColorsOutlined, PlusOutlined, DownOutlined, CheckOutlined, CloseOutlined } from '@ant-design/icons';
import { Modal, Form, Input, Button,Popconfirm , Select, TreeSelect, Space, Checkbox, Col, Row, DatePicker, Card, List, Tree, message, Drawer, Divider, Tag, Tabs } 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 moment from 'moment';
const { TabPane } = Tabs;
import {
  addPracticeType,
  getPracticeType,
  editPracticeType,
  editPracticeRecord,
  addPracticeRecord,
  getPracticeRecord,
  getPracticeRelevance,
  addPracticeRelevance,
  editPracticeRelevance,
  GetAllType,
  GetAllSmallType,
  GetAllGoods,
  GetAllPractice
} from './service';

import CreateRecordForm from './components/createRecordForm';

const PageIndex = (props) => {
  const actionRef = useRef();
  const [createRecordModalVisible, handleRecordModalVisible] = useState();
  const [RecordCurrentRow, setRecordCurrentRow] = useState();

  const [AllTypeList, setAllTypeList] = useState([]);



  /**
* 数据操作
*/
  /**
     * 添加节点
     *
     * @param fields
     */
  function handleRecordAdd(fields) {
    try {
      addPracticeRecord(fields)
        .then((r) => {
          if (r.data) {
            message.success('添加成功');
            //刷新数据
          } else {
            message.error('添加失败请重试！');
          }
        });
      return true;
    } catch (error) {
      message.error('添加失败请重试！');
      return false;
    }
  }




  /**
   * 更新节点
   *
   * @param fields
   */

  function handleRecordUpdate(fields) {
    try {
      editPracticeRecord(fields)
        .then((r) => {
          if (r.data) {
            message.success('配置成功');
          } else {
            message.error('配置失败请重试！');
          }
          setRecordCurrentRow(undefined);
          //刷新数据
        });
      return true;
    } catch (error) {
      message.error('配置失败请重试！');
      return false;
    }
  }



  useEffect(() => {
    const parmar = {
      current: 1,
      pagesize: 99999
    };
    function initPracticeType() {
      GetAllType(parmar).then((r) => {
        setAllTypeList(r.data.data);
      });
    }
    initPracticeType();
  }, []);


  /**
  * 做法档案列表
  */
  const PracticeRecordcolumns = [
    {
      title: '规则id',
      dataIndex: 'id',
      tip: '规则名称是唯一的 key',
      hideInSearch: true,
      hideInTable: true,
    },
    {
      title: '做法名称',
      dataIndex: 'makeName',
    },
    {
      title: '做法编码',
      dataIndex: 'code',
    },
    {
      title: '做法类别',
      dataIndex: 'makeTypeName',
    },
    {
      title: '加价',
      dataIndex: 'raise',
    },
    {
      title: '加价方式',
      dataIndex: 'raiseType',
      render: (_, record) => {
        if (record.raiseType == 1) {
          return (
            <label>按出品数量加价</label>
          );

        } else
          if (record.raiseType == 1) {
            return (
              <label>加价与出品数量无关 </label>
            );

          } else {
            return (
              <CloseOutlined />
            );
          }
      }
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => [
        <a
          key="primary"
          onClick={() => {
            handleRecordModalVisible(true);
            setRecordCurrentRow(record);
          }}
        >
          {' '}
          修改
        </a>,
        <a
          key="primary"
          onClick={() => {
            record.status = record.status == 0 ? 1 : 0;
            editPracticeRecord(record);
          }}
        >
          {' '}
          {record.status == 0 ? '停用' : '启用'}
        </a>,
      //   <Popconfirm
      //   type="primary"
      //   key="primary"
      //   title="确认删除吗？"
      //   okText="是"
      //   cancelText="否"
      //   onConfirm={() => {
      //     handleRemove(record.id);
      //     actionRef.current?.reloadAndRest();
      //   }}
      //   onCancel={() => { }}
      // >
      //   <a href="#">删除</a>
      // </Popconfirm>,

      ],
    },
  ];

  return (

    <PageContainer>
      <CreateRecordForm
        onFinish={async (value) => {
          var success = false;
          if (value.id) {
            success = handleRecordUpdate(value);
          } else {
            success = handleRecordAdd(value);
          }
          if (success) {
            handleRecordModalVisible(false);
            setRecordCurrentRow(undefined);
            if (actionRef.current) {
              actionRef.current.reload();
            }
          }
        }}
        onCancel={() => {
          setRecordCurrentRow(undefined);
          handleRecordModalVisible(false);
          actionRef.current.reload();
        }}
        TypeList={AllTypeList || []}
        createModalVisible={createRecordModalVisible}
        values={RecordCurrentRow || {}}
      >
      </CreateRecordForm>
      <ProTable
        actionRef={actionRef}
        rowKey="id"
        search={{
          labelWidth: 120,
        }}
        toolBarRender={() => [
          <Button
            type="primary"
            key="primary"
            onClick={() => {
              handleRecordModalVisible(true);
            }}
          >
            <PlusOutlined /> 添加
          </Button>
        ]}
        request={async (params) => {
          var datas = [];
          var total = 0;
          await getPracticeRecord(params).then((r) => {
            datas = r.data.data;
            total = r.data.total;
          });
          return {
            data: datas,
            success: true,
            total: total,
          };
        }}
        modalVisible={createRecordModalVisible}
        columns={PracticeRecordcolumns}
        rowSelection={{
          onChange: (_, selectedRows) => {
            setRecordCurrentRow(selectedRows);
          },
        }}
      />
    </PageContainer>
  );
};

export default PageIndex;
