import { Card} from 'antd';
import { connect } from 'dva';
import React from 'react';
import { Toolbar } from '@bizComponents';
import { FormEL, UdcSelect } from '@components';
import formUtils from '../../../utils/formUtils';
// import { throttle, debounce } from 'lodash';


const SupplierLifeStyleDetail = ({ dispatch, supplierLifeStyleDetail, loading }) => {
  const { formData,mode,saveBtnHidden } = supplierLifeStyleDetail;

  const domain = 'supplierLifeStyleDetail';
  const domainCN = '供应商考核周期维护';


  let readOnly = true,control=false;
  console.log("readOnly1"+readOnly);

  if (mode !== 'view') {
    readOnly = false;
    console.log("readOnly2"+readOnly);
  }

  // if (statusReadOnly) {
  //   readOnly = true;
  // }
  if (saveBtnHidden) {
    readOnly = true;
    // } else {
    //   readOnly = false
  }
  console.dir(formData);


  const performanceDetail = [{
    title: '供应商名称',
    dataIndex: 'suppName',
  },{
    title: '评分规则',
    dataIndex: 'perforType',
    options: {
      rules: [{
        required: true ,
        whitespace: false,
        message: '必填'
      }]
    },
    tag: <UdcSelect domain="SUPP" code="PERFOR_TYPE" disabled={readOnly || control} />,
  },{
    title: '考核周期',
    dataIndex: 'assessPeriod',
    options: {
      rules: [{
        required: true,

        message: '必填'
      }]
    },
    tag: <UdcSelect domain="SUPP" code="ASSESS_PERIOD" disabled={readOnly || control} />,
  }];


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



  const getDefaultValues = () => {
    const init = {};
    performanceDetail.forEach((performanceDetail) => {
      if (performanceDetail.options && performanceDetail.options.initialValue) {
        init[performanceDetail.dataIndex] = performanceDetail.options.initialValue;
      }
      console.dir(performanceDetail);
    });
    return init;
  };

  const handleSave = () => {
    formUtils.validateForm([...performanceDetail], Object.assign(getDefaultValues(), formData), () => {
      dispatch({ type: `${domain}/save`, payload: getDefaultValues() });
    });
  };

  const buttons = [{
    id: 'save',
    text: '保存',
    onClick: handleSave,
    // hidden: saveBtn,
  }];

  const handleFormChange = (field) => {

    const key = Object.keys(field)[0];
    console.dir(field);
    dispatch({
      type: `${domain}/updateForm
      `,
      payload: {
        key,
        value: field[key],
      },
    });
  };

  return (
    <div>
      <Toolbar
        //prcId={formData.prcId}
        //defId={formData.defId}
        loading={loading}
        record={formData}
        readOnly={readOnly}
        mode={mode}
        buttons={readOnly ? [] : buttons}
        //handleSync
        //handleMail
        //handlePrint
        handleCancel={handleCancel}
      />



      <Card title="供应商绩效属性" id="performanceDetail">
        <FormEL items={performanceDetail} record={formData} readOnly={readOnly}  onFieldsChange={handleFormChange} />
      </Card>

    </div>
  );
};

export default connect(({ supplierLifeStyleDetail, loading, app }) => ({ supplierLifeStyleDetail, loading, app }))(SupplierLifeStyleDetail);
