import { enums } from '@utils';
import { Card, InputNumber } from 'antd';
import { connect } from 'dva';
import React from 'react';
import { DataTable, FormEL, UploadEL } from '../../../components';
import { Toolbar } from '@bizComponents';
import styles from './exam.less';
import { numberUtils } from '@utils';

//计算行总分
const scoreTotal = (row) => {
  return row.score5
    + (Number.isFinite(row.score1) ? row.score1 : 0)
    + (Number.isFinite(row.score2) ? row.score2 : 0)
    + (Number.isFinite(row.score3) ? row.score3 : 0)
    + (Number.isFinite(row.score4) ? row.score4 : 0)
    + (Number.isFinite(row.score6) ? row.score6 : 0)
    + (Number.isFinite(row.score7) ? row.score7 : 0);
}
const { bidStatus } = enums;
const Detail = ({ examsDetails, dispatch, loading }) => {
  const { mode, formData, list, saveBtnFlag } = examsDetails;
  const { businessName, registeredCapitalName, enterpriseQualificationName, enterpriseReputationName, enterpriseSalesName, quotationName, technicalName, qualityContentName, saleServiceName, isBidOver, biType } = formData;
  const domain = 'examsDetails';
  let readOnly = true;
  if (mode === 'create' || (mode === 'update' && isBidOver !== 1)) {
    readOnly = false;
  }

  // 基本信息form
  const basic = [{
    title: '招标单编号',
    dataIndex: 'docNo',
  }, {
    title: '招标单名称',
    dataIndex: 'biName',
  }];

  const firstColumns = [{
    title: '物料',
    dataIndex: 'itemCode',
    className: 'alignLeft',
    width: '8%',
    render: (text, record, index) => {
      return (<div>{record.itemName} {record.itemCode}</div>);
    }
  }, {
    title: '供应商',
    dataIndex: 'suppCode',
    className: 'alignLeft',
    width: '8%',
    render: (text, record, index) => {
      return (<div>{record.suppName} {record.suppCode}</div>);
    }
  }];

  const secondColumns = [{
    title: '评标人',
    dataIndex: 'empName',
    width: '6%',
  }];

  const thirdColumns = [{
    title: `${businessName}`,
    dataIndex: 'businessName',
    children: [{
      title: `${registeredCapitalName}`,
      dataIndex: 'score1',
      width: '6%',
      render: (value, row, index) => {
        return (
          <InputNumber
            value={value}
            disabled={readOnly}
            precision={0}
            step={1}
            min={0}
            max={parseInt(registeredCapitalName.substring(registeredCapitalName.indexOf('(') + 1, registeredCapitalName.indexOf(')') - 1))}
            onChange={(value) => {
              handleChange('score1', index, value);
            }}
          />);
      },
    }, {
      title: `${enterpriseQualificationName}`,
      dataIndex: 'score2',
      width: '9%',
      render: (value, row, index) => {
        return (
          <InputNumber
            value={value}
            disabled={readOnly}
            precision={0}
            step={1}
            min={0}
            max={parseInt(enterpriseQualificationName.substring(enterpriseQualificationName.indexOf('(') + 1, enterpriseQualificationName.indexOf(')') - 1))}
            onChange={(value) => {
              handleChange('score2', index, value);
            }}
          />);
      },
    }, {
      title: `${enterpriseReputationName}`,
      dataIndex: 'score3',
      width: '9%',
      render: (value, row, index) => {
        return (
          <InputNumber
            value={value}
            disabled={readOnly}
            precision={0}
            step={1}
            min={0}
            max={parseInt(enterpriseReputationName.substring(enterpriseReputationName.indexOf('(') + 1, enterpriseReputationName.indexOf(')') - 1))}
            onChange={(value) => {
              handleChange('score3', index, value);
            }}
          />);
      },
    }, {
      title: `${enterpriseSalesName}`,
      dataIndex: 'score4',
      width: '8%',
      render: (value, row, index) => {
        return (
          <InputNumber
            value={value}
            disabled={readOnly}
            precision={0}
            step={1}
            min={0}
            max={parseInt(enterpriseSalesName.substring(enterpriseSalesName.indexOf('(') + 1, enterpriseSalesName.indexOf(')') - 1))}
            onChange={(value) => {
              handleChange('score4', index, value);
            }}
          />);
      },
    }, {
      title: `${quotationName}`,
      dataIndex: 'score5',
      width: '6%',
      render: (value, row, index) => {
        return (
          <InputNumber
            value={value}
            disabled={true}
            precision={0}
            step={1}
            min={0}
            onChange={(value) => {
              handleChange('score5', index, value);
            }}
          />);
      },
    }]
  }];

  const forthColumns = [{
    title: `${businessName}`,
    dataIndex: 'score5',
    width: '6%',
    render: (value, row, index) => {
      return (
        <InputNumber
          value={value}
          disabled={true}
          precision={0}
          step={1}
          min={0}
          onChange={(value) => {
            handleChange('score5', index, value);
          }}
        />);
    },
  }];

  const fifthColumns = [{
    title: `${technicalName}`,
    dataIndex: 'technicalName',
    children: [{
      title: `${qualityContentName}`,
      dataIndex: 'score6',
      width: '6%',
      render: (value, row, index) => {
        return (
          <InputNumber
            value={value}
            disabled={readOnly}
            precision={0}
            step={1}
            min={0}
            max={parseInt(qualityContentName.substring(qualityContentName.indexOf('(') + 1, qualityContentName.indexOf(')') - 1))}
            onChange={(value) => {
              handleChange('score6', index, value);
            }}
          />);
      },
    }, {
      title: `${saleServiceName}`,
      dataIndex: 'score7',
      width: '6%',
      render: (value, row, index) => {
        return (
          <InputNumber
            value={value}
            disabled={readOnly}
            precision={0}
            step={1}
            min={0}
            max={parseInt(saleServiceName.substring(saleServiceName.indexOf('(') + 1, saleServiceName.indexOf(')') - 1))}
            onChange={(value) => {
              handleChange('score7', index, value);
            }}
          />);
      },
    }]
  }, {
    title: '总得分',
    dataIndex: 'totalScore',
    width: '5%',
    render: (value, row, index) => {
      return (
        scoreTotal(row)
      );
    }
  }];

  const sixthColumns = [{
    title: <div><div>当前价格</div><div>最大供货量</div></div>,
    dataIndex: 'priceAndMax',
    width: '6%',
    render: (text, record, index) => {
      return (<div><div>{record.taxPrice ? numberUtils.round_half_up_format(record.taxPrice) : '—'}</div><div>{numberUtils.round_half_up_format(record.maxProvideNum)}</div></div>);
    }
  }, {
    title: <div><div>未税单价</div><div>含税单价</div></div>,
    dataIndex: 'amt',
    width: '5%',
    render: (text, record, index) => {
      return (<div><div>{numberUtils.round_half_up_format(record.netPrice)}</div><div>{numberUtils.round_half_up_format(record.price)}</div></div>);
    }
  }, {
    title: <div><div>税率</div><div>税额</div></div>,
    dataIndex: 'tax',
    width: '5%',
    render: (text, record, index) => {
      return (<div><div>{numberUtils.round_half_up_format(record.taxRate)}</div><div>{numberUtils.round_half_up_format(record.taxAmt)}</div></div>);
    }
  }, {
    title: <div><div>未税总价</div><div>含税总价</div></div>,
    dataIndex: 'quotationScore',
    width: '5%',
    render: (text, record, index) => {
      return (<div><div>{numberUtils.round_half_up_format(record.netAmt)}</div><div>{numberUtils.round_half_up_format(record.amt)}</div></div>);
    }
  },{
    title: '外部参考价',
    dataIndex: 'outerPrice',
    width: '5%',
    render: (text, record, index) => {
      return (
        numberUtils.round_half_up_format(record.outerPrice)
      );
    }
  }];

  const seventhColumns = [{
    title: '投标文件',
    dataIndex: 'bidFile',
    width: '10%',
    render: (value, row, index) => {
      return (
        <UploadEL size={'small'}
          api={`/api/source-mgmt/bid-mgmt/bids/bids/sfs/token?clsPath=${row.attachRepo}&dataKey=${row.dataKey}`}
          dataKey={row.dataKey}
          listTypes={'text'}
          disabled={true} />
      );
    },
  }]


  const columns = firstColumns.concat(readOnly ? secondColumns.concat(biType === '1' ? thirdColumns : forthColumns) : biType === '1' ? thirdColumns : forthColumns, fifthColumns, readOnly ? seventhColumns : sixthColumns.concat(seventhColumns));


  const tableProps = {
    rowKey: 'id',
    scroll: {
      x: '140%',
      y: 500,
    },
    domain,
    dispatch,
    loading,
    showAdd: false,
    showDelete: false,
    showEdit: false,
    showView: false,
    dataSource: list,
    withFetch: false,
    enablePagination: false,
    enableSelection: false,
    showColumn: false,
    columns: columns,
  }

  // 列表编辑事件
  const handleChange = (key, index, newValue) => {
    dispatch({ type: `${domain}/updateCell`, payload: { key, index, value: newValue } });
  };

  const handleSave = () => {
    dispatch({ type: `${domain}/save` });
  };

  const handleCancel = () => {
    dispatch({ type: `${domain}/cancel` });
  };
  const handleFormChange = (field) => {
    // console.log('field',field)
  };
  const button = [{
    id: 'save',
    text: '保存',
    type: 'primary',
    hidden: saveBtnFlag ? (bidStatus.TO_OPEN_BID !== formData.docStatus) : true,
    onClick: () => {
      handleSave();
    },
  }];

  return (
    <div className={styles['exam-last']}>
      <Toolbar
        prcId={formData.prcId}
        defId={formData.defId}
        loading={loading}
        readOnly={readOnly}
        mode={mode}
        record={formData}
        buttons={!readOnly ? button : []}
        handleSave={handleSave}
        handleCancel={handleCancel}
      />

      <Card title="基本信息" id="basic">
        <FormEL items={basic} record={formData} readOnly={readOnly} onFieldsChange={handleFormChange} />
      </Card>

      <Card title="评分列表" id="editTableProps">
        {Object.keys(formData).length > 0 && <DataTable {...tableProps} />}
      </Card>

    </div>);
};

export default connect(({ examsDetails, loading }) => ({
  examsDetails,
  loading,
}))(Detail);
