import { dataUtils, enums, stringUtils } from '@utils';
import { Modal } from 'antd';
import React from 'react';
import { connect } from 'dva';
import { routerRedux } from 'dva/router';
import {
  DataTable,
  EditableDataTable,
  FormEL,
  Loader,
  MonthPicker,
  SuggestSelect,
  UdcSelect,
  UploadEL
} from '@components';
import { Block, Page, Panel, Toolbar } from '@bizComponents';
import styles from './expandedRow.less';
import config from './reviewCfg';


const { bidStatus } = enums;
const TenderReview = ({ dispatch, tenderReview, loading }) => {
  const {
    remoteCfg, formData, list, total, modalVisible, currentItem, sData, sList, submitBtnFlag,
    acceptBtnFlag
  } = tenderReview;

  const domain = 'tenderReview';
  const domainCN = '评标审核';

  const viewTableProps = {
    rowKey: 'id',
    scroll: {
      x: '150%',
    },
    domain,
    dispatch,
    loading,
    showAdd: false,
    showDelete: false,
    showEdit: false,
    showView: false,
    dataSource: sList,
    withFetch: false,
    enablePagination: false,
    showColumn: false,
    enableSelection: false,
    columns: [{
      title: '供应商编号',
      dataIndex: 'suppCode',
      width: '8%',
    }, {
      title: '供应商名称',
      dataIndex: 'suppName',
      width: '8%',
    }, {
      title: '物料编号',
      dataIndex: 'itemCode',
      width: '8%',
    }, {
      title: '物料名称',
      dataIndex: 'itemName',
      width: '8%',
    }, {
      title: '评标人',
      dataIndex: 'bidUser',
      width: '6%',
    }, {
      title: `${sData.businessName}`,
      dataIndex: 'businessName',
      children: [{
        title: `${sData.registeredCapitalName}`,
        dataIndex: 'registeredCapitalScore',
        width: '6%',
      }, {
        title: `${sData.enterpriseQualificationName}`,
        dataIndex: 'enterpriseQualificationScore',
        width: '8%',
      }, {
        title: `${sData.enterpriseReputationName}`,
        dataIndex: 'enterpriseReputationScore',
        width: '9%',
      }, {
        title: `${sData.enterpriseSalesName}`,
        dataIndex: 'enterpriseSalesScore',
        width: '7%',
      }, {
        title: `${sData.quotationName}`,
        dataIndex: 'quotationScore',
        width: '6%',
      }]
    }, {
      title: `${sData.technicalName}`,
      dataIndex: 'technicalName',
      children: [{
        title: `${sData.qualityContentName}`,
        dataIndex: 'qualityContentScore',
        width: '6%',
      }, {
        title: `${sData.saleServiceName}`,
        dataIndex: 'saleServiceScore',
        width: '6%',
      }]
    }]
  }

  const invTableProps = {
    rowKey: 'id',
    scroll: {
      x: '100%',
    },
    domain,
    dispatch,
    loading,
    showAdd: false,
    showDelete: false,
    showEdit: false,
    showView: false,
    dataSource: sList,
    withFetch: false,
    enablePagination: false,
    showColumn: false,
    enableSelection: false,
    columns: [{
      title: '供应商编号',
      dataIndex: 'suppCode',
      width: '10%',
    }, {
      title: '供应商名称',
      dataIndex: 'suppName',
      width: '10%',
    }, {
      title: '物料编号',
      dataIndex: 'itemCode',
      width: '10%',
    }, {
      title: '物料名称',
      dataIndex: 'itemName',
      width: '10%',
    }, {
      title: '评标人',
      dataIndex: 'bidUser',
      width: '10%',
    }, {
      title: `${sData.quotationName}`,
      dataIndex: 'quotationScore',
      width: '10%',
    }, {
      title: `${sData.technicalName}`,
      dataIndex: 'technicalName',
      children: [{
        title: `${sData.qualityContentName}`,
        dataIndex: 'qualityContentScore',
        width: '10%',
      }, {
        title: `${sData.saleServiceName}`,
        dataIndex: 'saleServiceScore',
        width: '10%',
      }]
    }]
  }

  const resultTableProps = {
    rowKey: 'id',
    scroll: {
      x: 2500,
      y: 500,
    },
    domain,
    dispatch,
    loading,
    // total,
    showAdd: false,
    showDelete: false,
    showCopy: false,
    enableSelection: false,
    // readOnly: false,
    // filter: [],
    // columns: [],
  };

  const basic = [{
    title: '招标单编号',
    dataIndex: 'docNo',
  }, {
    title: '招标单名称',
    dataIndex: 'biName',
  }]
  const modalProps = {
    mode: 'view',
    domain,
    dispatch,
    loading,
    record: currentItem,
    visible: modalVisible,
    maskClosable: false,
    width: '70%',
    readOnly: true,
    title: '查看评分详情',
    wrapClassName: 'vertical-center-modal',
    onOk(data) {//todo 消灭掉
      dispatch({
        type: `${domain}/hideModal`,
      });
    },
    onCancel() {
      dispatch({
        type: `${domain}/hideModal`,
      });
    },
  };
  const handleFormChange = (field) => {
    // console.log('field=>',field)
  };
  const doSubmit = () => {
    dispatch({ type: `${domain}/status`, payload: bidStatus.TO_FINAL_CONFIRMATION });
  }

  const doDrop = () => {
    dispatch({ type: `${domain}/status`, payload: bidStatus.DROP });
  }

  const doApprove = () => {
    dispatch({ type: `${domain}/finalStatus`, payload: bidStatus.FINISH });
  }

  const doReject = () => {
    dispatch({ type: `${domain}/finalStatus`, payload: bidStatus.PENDING_ADJUST });
  }

  const handleCancel = () => {
    dispatch({ type: `${domain}/cancel` });
  };
  if (remoteCfg) {

    // remoteCfg.buttons.find((b) => b.id === 'approve').onClick = () => {
    //   handleApprove();
    // };
    //
    // remoteCfg.buttons.find((b) => b.id === 'reject').onClick = () => {
    //   handleReject();
    // };

    remoteCfg.buttons.find((b) => b.id === 'notice').onClick = () => {
      // dispatch({ type: `${domain}/notice` });
    };
    const buttons = [
      {
        id: 'advice',
        placeholder: '请输入意见',
        onChange: (e) => {
          dispatch({ type: `${domain}/updateState`, payload: { advice: e.target.value } });
        },
      }, {
        id: "submit",
        text: "提交",
        type: "primary",
        hidden: (bidStatus.PENDING_ADJUST !== formData.docStatus) || !submitBtnFlag,
        onClick: doSubmit
      },
      {
        id: "drop",
        text: "弃标",
        type: "primary",
        hidden: (bidStatus.PENDING_ADJUST !== formData.docStatus) || !submitBtnFlag,
        onClick: doDrop
      },
      {
        id: "accept",
        text: "同意",
        type: "primary",
        hidden: (bidStatus.TO_FINAL_CONFIRMATION !== formData.docStatus) || !acceptBtnFlag,
        onClick: doApprove
      },
      {
        id: "reject",
        text: "拒绝",
        type: "primary",
        hidden: (bidStatus.TO_FINAL_CONFIRMATION !== formData.docStatus) || !acceptBtnFlag,
        onClick: doReject
      },
      ...remoteCfg.buttons,
    ];

    return (
      <div>
        <Toolbar
          buttons={buttons}
          defId={formData.defId}
          loading={loading}
          record={formData}
          mode='view'
          prcId={formData.prcId}
          handleCancel={handleCancel}
        />
        <Page dispatch={dispatch} id="tenderReview" localCfg={config} loading={loading} remoteCfg={remoteCfg}>
          <Panel id="reviewBasic" title="基本信息">
            <Block id="reviewBasicForm" type="form" data={formData} onFieldsChange={handleFormChange}/>
          </Panel>
          <Panel id="reviewResult" title="评分结果">
            <Block
              id="reviewResultList"
              type="table"
              data={list}
              extraInfo={{ docStatus: formData ? formData.docStatus : null }}
              {...resultTableProps}
            />
          </Panel>
        </Page>
        {modalVisible && (<Modal {...modalProps}>
          <DataTable {...(formData.biType === '1' ? viewTableProps : invTableProps)} />
        </Modal>)}
      </div>
    );
  } else {
    return <Loader fullScreen={false} spinning={loading.effects[`${domain}/query`]}/>;
  }
};

export default connect(({ tenderReview, loading }) => ({ tenderReview, loading }))(TenderReview);
