import React from 'react';
import { Form, Input, Button, Select, Tabs, message, Radio, Checkbox, Row, Col } from 'antd';
import LogoTitlte from '../../components/LogoTitlte/index';
import { Jsme } from 'jsme-react';
import qs from 'qs';
import request from '../../utils/request';
import './style.css';
const { TabPane } = Tabs;
const { Option } = Select;
class Home extends React.Component {
  state = {
    select_db: 'Ligands',
    data: '',
    page: 1,
    page_size: 10,
    data_type: 'smile',
    target_class: '',
    // target_families:'',
    searchData: {
      name: 'Drug',
      drugs_data: '',
      pdb_status: 0,
      page: 1,
      page_size: 10,
      finger: 0,
    },

    TargetSearchData: {
      name: 'Target',
      target_class: '',
      target_data: '',
      target_family: '',
      pdb_status: 0,
      page: 1,
      page_size: 10,
    },
    ligandSearchData: {
      ligand_data: '',
      name: 'Ligand',
      page: 1,
      page_size: 10,
      finger: 0,
    },
    IndicationSearchData: {
      name: 'Indication',
      indication: '',
      page: 1,
      page_size: 10,
    },
    MutationSearchData: {
      name: 'Mutation',
      chembl_id: '',
      target_class: 'All Class',
      target_data: '',
      target_families: '',
      idselect_data: [],
      page: 1,
      page_size: 10,
    },
    disabled: true,
    loading: false,
    SelectList: [],
    draw: false,
    classList: [],
    classData: 'All Class',
    allClassList: {},
    childClassList: [],
    TargetClassList: [],
    TargetAllClassList: [],
    TargetChildClassList: [],
    compoundList: ['DrugName', 'Smiles'],
    targetList: ['TargetName', 'UniprotId'],
    optionsList: [{ label: '查询drug相关靶点', value: 1 }, { label: '查询ligand相关靶点', value: 2, disabled: false }, { label: '是否有PDB结构', value: 3 }],
  };
  // Mutation
  CheckChange = (e) => {
    console.log(e);
    let MutationSearchData = this.state.MutationSearchData;
    MutationSearchData.idselect_data = e;
    this.setState(() => ({ MutationSearchData: MutationSearchData }));
  };
  onAllChange = (e) => {
    if (e.target.checked) {
      let MutationSearchData = this.state.MutationSearchData;
      let all_idselect_data = ['TM1', 'TM4', 'TM7', 'ICL3', 'ECL3', 'N-Term', 'ECL1', 'ICL1', 'TM5', 'TM2', 'TM3', 'TM6', 'ICL2', 'ECL2', 'C-Term'];
      MutationSearchData.idselect_data = all_idselect_data;
      this.setState(() => ({ MutationSearchData: MutationSearchData }));
    } else {
      let MutationSearchData = this.state.MutationSearchData;
      let all_idselect_data = [];
      MutationSearchData.idselect_data = all_idselect_data;
      this.setState(() => ({ MutationSearchData: MutationSearchData }));
    }
  };
  getMutationList = () => {
    let data = JSON.parse(JSON.stringify(this.state.MutationSearchData));
    data.idselect_data = data.idselect_data.toString();
    this.setState(() => ({ loading: true }));
    request
      .post('/mutation_search', qs.stringify(data))
      .then((res) => {
        this.setState(() => ({ loading: false }));
        if (res.data.code === 200) {
          console.log(data);
          this.props.history.push({ pathname: '/home/SearchList', search: qs.stringify(data) });
        } else {
          message.error(res.data.msg);
        }
      })
      .catch((err) => {
        console.error(err);
      });
  };
  childClassChange3 = (e) => {
    console.log(e);
    let MutationSearchData = this.state.MutationSearchData;
    MutationSearchData.target_families = e;
    this.setState(() => ({ MutationSearchData: MutationSearchData }));
  };
  getClassData3 = (e) => {
    console.log(e);
    this.setState(() => ({ classData: e }));
    let MutationSearchData = this.state.MutationSearchData;
    MutationSearchData.target_class = e;
    this.setState(() => ({ MutationSearchData: MutationSearchData }));
    let value = this.state.allClassList;
    let childData = value[e];
    console.log(childData);
    this.setState(() => ({ childClassList: childData }));
  };
  getMutationValue = (e) => {
    const { value } = e.target;
    let MutationSearchData = this.state.MutationSearchData;
    MutationSearchData.target_data = value;
    this.setState(() => ({ MutationSearchData: MutationSearchData }));
  };
  getChemblValue = (e) => {
    const { value } = e.target;
    let MutationSearchData = this.state.MutationSearchData;
    MutationSearchData.chembl_id = value;
    this.setState(() => ({ MutationSearchData: MutationSearchData }));
  };
  // Indication

  getIndicationValue = (e) => {
    const { value } = e.target;
    let IndicationSearchData = this.state.IndicationSearchData;
    IndicationSearchData.indication = value;
    this.setState(() => ({ IndicationSearchData: IndicationSearchData }));
  };
  getIndicationList = () => {
    let data = this.state.IndicationSearchData;
    if (data.indication == '') {
      return message.warning('请输入查询条件！');
    }
    this.setState(() => ({ loading: true }));
    request
      .post('/kinase/indication_search', data)
      .then((res) => {
        this.setState(() => ({ loading: false }));
        if (res.data.code === 200) {
          this.props.history.push({ pathname: '/Kinase/IndicationDetails', search: qs.stringify(data) });
        } else {
          message.error(res.data.msg);
        }
      })
      .catch((err) => {
        console.error(err);
      });
  };

  // ligend
  getLigendValue = (e) => {
    const { value } = e.target;
    let ligandSearchData = this.state.ligandSearchData;
    ligandSearchData.ligand_data = value;
    this.setState(() => ({ ligandSearchData: ligandSearchData }));
  };

  logSmiles2 = (e) => {
    let ligandSearchData = this.state.ligandSearchData;
    ligandSearchData.ligand_data = e;
    this.setState(() => ({ ligandSearchData: ligandSearchData }));
    // this.setState(()=>({data:e }))
  };
  getLigendList = () => {
    let data = this.state.ligandSearchData;
    if (data.ligand_data == '') {
      return message.warning('请输入查询条件！');
    }
    this.setState(() => ({ loading: true }));
    request
      .post('/kinase/ligand_search', data)
      .then((res) => {
        console.log(res);
        this.setState(() => ({ loading: false }));
        if (res.data.code === 200) {
          this.props.history.push({ pathname: '/Kinase/SearchList', search: qs.stringify(data) });
        } else {
          message.error(res.data.msg);
        }
      })
      .catch((err) => {
        console.error(err);
      });
  };
  getLigandData = (e) => {
    const { value } = e.target;
    let ligandSearchData = this.state.ligandSearchData;
    ligandSearchData.target_data = value;
    this.setState(() => ({ ligandSearchData: ligandSearchData }));
  };
  ligandChange = (e) => {
    let ligandSearchData = this.state.ligandSearchData;
    ligandSearchData.target_type = e;
    this.setState(() => ({ ligandSearchData: ligandSearchData }));
  };
  childClassChange2 = (e) => {
    console.log(e);
    let ligandSearchData = this.state.ligandSearchData;
    ligandSearchData.target_families = e;
    this.setState(() => ({ ligandSearchData: ligandSearchData }));
  };
  getClassData2 = (e) => {
    console.log(e);
    this.setState(() => ({ classData: e }));
    let ligandSearchData = this.state.ligandSearchData;
    ligandSearchData.target_class = e;
    this.setState(() => ({ ligandSearchData: ligandSearchData }));
    let value = this.state.allClassList;
    let childData = value[e];
    console.log(childData);
    this.setState(() => ({ childClassList: childData }));
  };
  // Target
  getTargetClassData = (e) => {
    this.setState(() => ({ classData: e }));
    let TargetSearchData = this.state.TargetSearchData;
    TargetSearchData.target_family = '';
    this.setState(() => ({ TargetSearchData: TargetSearchData }));
    TargetSearchData.target_class = e;
    this.setState(() => ({ TargetSearchData: TargetSearchData }));
    let value = this.state.TargetClassList;
    value.map((item) => {
      if (item.name === e) {
        this.setState(() => ({ TargetChildClassList: item.content }));
      }
    });

    // let childData=value[e]
    // console.log(childData);
    // this.setState(()=>({childClassList:childData}))
  };

  childTargetClassChange = (e) => {
    console.log(e);
    let TargetSearchData = this.state.TargetSearchData;
    TargetSearchData.target_family = e;
    this.setState(() => ({ TargetSearchData: TargetSearchData }));
  };
  targetChange = (e) => {
    let TargetSearchData = this.state.TargetSearchData;
    TargetSearchData.target_type = e;
    this.setState(() => ({ TargetSearchData: TargetSearchData }));
  };
  getindTargetication = (e) => {
    const { value } = e.target;
    let TargetSearchData = this.state.TargetSearchData;
    TargetSearchData.indication = value;
    this.setState(() => ({ TargetSearchData: TargetSearchData }));
  };
  getTargetData2 = (e) => {
    const { value } = e.target;
    let TargetSearchData = this.state.TargetSearchData;
    TargetSearchData.target_data = value;
    this.setState(() => ({ TargetSearchData: TargetSearchData }));
  };
  get_drug_name = (e) => {
    const { value } = e.target;
    let TargetSearchData = this.state.TargetSearchData;
    TargetSearchData.drug_name = value;
    this.setState(() => ({ TargetSearchData: TargetSearchData }));
  };
  getTargetList = () => {
    let data = this.state.TargetSearchData;
    if (data.target_class == '' && data.target_data == '') {
      return message.warning('请输入查询条件！');
    }
    this.setState(() => ({ loading: true }));
    request
      .post('/kinase/targets_search', data)
      .then((res) => {
        console.log(res);
        this.setState(() => ({ loading: false }));
        if (res.data.code === 200) {
          this.props.history.push({ pathname: '/Kinase/SearchList', search: qs.stringify(data) });
        } else {
          message.error(res.data.msg);
        }
      })
      .catch((err) => {
        console.error(err);
      });
  };

  onChangeCheckbox = (e) => {
    console.log(e);
    let TargetSearchData = this.state.TargetSearchData;
    TargetSearchData.select_status = e;
    this.setState(() => ({ TargetSearchData: TargetSearchData }));
    console.log(this.state.TargetSearchData);
  };
  handleChange = (e) => {
    console.log(e);
    this.setState(() => ({ select_db: e }));
    if (e != 'Ligands') {
      this.setState(() => ({ data: '' }));
    }
  };
  childClassChange = (e) => {
    console.log(e);
    let searchData = this.state.searchData;
    searchData.target_families = e;
    this.setState(() => ({ searchData: searchData }));
  };
  getClassData = (e) => {
    console.log(e);
    this.setState(() => ({ classData: e }));
    let searchData = this.state.searchData;
    searchData.target_class = e;
    this.setState(() => ({ searchData: searchData }));
    console.log(this.state.searchData);
    let value = this.state.allClassList;
    let childData = value[e];
    console.log(childData);
    this.setState(() => ({ childClassList: childData }));
  };
  targetChange = (e) => {
    let searchData = this.state.searchData;
    searchData.target_type = e;
    this.setState(() => ({ searchData: searchData }));
  };
  logSmiles = (e) => {
    let searchData = this.state.searchData;
    searchData.drugs_data = e;
    this.setState(() => ({ searchData: searchData }));
    // this.setState(()=>({data:e }))
  };
  componentDidMount() {
    // this.getList()
    // this.getSelectList()
    this.getTargetSelectList();
  }
  getSelectList = () => {
    request
      .get('/kinase/target_class/drug')
      .then((res) => {
        console.log(res);
        if (res.data.code === 200) {
          console.log(res.data.data);

          this.setState({
            classList: Object.keys(res.data.data),
            allClassList: res.data.data,
          });
          console.log(this.state);
        } else {
          message.error(res.data.msg);
        }
      })
      .catch((err) => {
        console.error(err);
      });
  };

  //首页启动调用接口  先checkToken == localStorage.getItem('token')
  getTargetSelectList = () => {
    // debugger
    localStorage.getItem('token') &&
      request
        .get('/kinase/target_class/target')
        .then((res) => {
          console.log(res);
          if (res.data.code === 200) {
            console.log(res.data.data);

            this.setState({
              TargetClassList: res.data.data,
              TargetAllClassList: res.data.data,
            });
            console.log(this.state);
          } else {
            message.error(res.data.msg);
          }
        })
        .catch((err) => {
          console.error(err);
        });
  };
  getValue = (e) => {
    const { value } = e.target;
    let searchData = this.state.searchData;
    searchData.drugs_data = value;
    this.setState(() => ({ searchData: searchData }));
  };
  getindication = (e) => {
    const { value } = e.target;
    let searchData = this.state.searchData;
    searchData.indication = value;
    this.setState(() => ({ searchData: searchData }));
  };
  getTargetData = (e) => {
    const { value } = e.target;
    let searchData = this.state.searchData;
    searchData.target_data = value;
    this.setState(() => ({ searchData: searchData }));
  };

  onChangeRadio = (e) => {
    let value = e.target.value;
    let searchData = this.state.searchData;
    searchData.pdb_status = value;
    this.setState({
      searchData: searchData,
    });
  };
  onChangeRadio2 = (e) => {
    let value = e.target.value;
    let TargetSearchData = this.state.TargetSearchData;
    TargetSearchData.pdb_status = value;
    this.setState({
      TargetSearchData: TargetSearchData,
    });
  };
  onChangeRadio3 = (e) => {
    let value = e.target.value;
    let searchData = this.state.searchData;
    searchData.finger = value;
    this.setState({
      searchData: searchData,
    });
  };
  onChangeRadio4 = (e) => {
    let value = e.target.value;
    let ligandSearchData = this.state.ligandSearchData;
    ligandSearchData.finger = value;
    this.setState({
      ligandSearchData: ligandSearchData,
    });
  };
  getList = () => {
    let data = this.state.searchData;
    request
      .post('/ligand_search', qs.stringify(data))
      .then((res) => {
        console.log(res);
        if (res.data.code === 200) {
          this.props.history.push({ pathname: '/home/SearchList', search: qs.stringify(data) });
        } else {
          message.error(res.data.msg);
        }
      })
      .catch((err) => {
        console.error(err);
      });
  };
  getDrugsList = () => {
    let data = this.state.searchData;
    if (data.drugs_data == '') {
      return message.warning('请输入查询条件！');
    }
    this.setState(() => ({ loading: true }));
    this.props.history.push({ pathname: '/kinase/SearchList', search: qs.stringify(data) });
    // request
    //   .post('/kinase/drug_search', data)
    //   .then((res) => {
    //     console.log(res);
    //     this.setState(() => ({ loading: false }));
    //     if (res.data.code === 200) {
    //       this.props.history.push({ pathname: '/kinase/SearchList', search: qs.stringify(data) });
    //     } else {
    //       message.error(res.data.msg);
    //     }
    //   })
    //   .catch((err) => {
    //     console.error(err);
    //   });
  };
  getDraw = () => {
    this.setState(() => ({ draw: !this.state.draw }));
  };
  compoundChange = (e) => {
    console.log(e);
    let searchData = this.state.searchData;
    searchData.drugs_type = e;
    this.setState(() => ({ searchData: searchData }));
    console.log(this.state.searchData);
  };
  callback = (e) => {
    console.log(e);
    let searchData = this.state.searchData;
    searchData.name = e;
    this.setState(() => ({ searchData: searchData }));
    console.log(this.state.searchData);
  };
  getJsme = () => {
    console.log(1);
  };

  render() {
    const { select_db, data, data_type, optionsList, draw, SelectList, targetList, classList, classData, childClassList, compoundList, TargetClassList, TargetChildClassList } = this.state;
    const formItemLayout = {
      labelCol: { span: 8 },
      wrapperCol: { span: 12 },
    };
    const formTailLayout = {
      labelCol: { span: 8 },
      wrapperCol: { span: 12, offset: 8 },
    };
    const formTailLayout2 = {
      labelCol: { span: 8 },
      wrapperCol: { span: 13, offset: 8 },
    };
    return (
      <div className='home'>
        <div className='home-div'>
          {/* <LogoTitlte></LogoTitlte> */}
          <Tabs onChange={this.callback} type='card'>
            <TabPane tab='Drug' key='Drug'>
              <div className='top-div'>
                <div>
                  <Form layout='horizontal'>
                    <Form.Item label='请输入药物名或Smiles' {...formItemLayout}>
                      <Input placeholder='请输入化合物' style={{ width: 250 }} size='large' onChange={this.getValue} value={this.state.searchData.drugs_data} />
                    </Form.Item>
                    <Form.Item label='' {...formTailLayout}>
                      <Button type='primary' style={{ marginRight: '50px' }} onClick={this.getDraw}>
                        画结构
                      </Button>
                      <div className={draw ? 'bottom-div' : 'bottom-div isShowDiv'}>
                        <Radio.Group onChange={this.onChangeRadio3} value={this.state.searchData.finger}>
                          <Radio value={0}>Substructure</Radio>
                          {/* <Radio value={1}>有GPCR相关PDB结构</Radio> */}
                          <Radio value={1}>Similar Structure</Radio>
                        </Radio.Group>
                      </div>

                      <div className={draw ? 'bottom-div' : 'bottom-div isShowDiv'} onClick={this.getJsme}>
                        <Jsme height='500px' width='700px' options='oldlook,star' onChange={this.logSmiles} />
                      </div>
                    </Form.Item>
                    <Form.Item label='使用PDB查询' {...formItemLayout}>
                      <Radio.Group onChange={this.onChangeRadio} value={this.state.searchData.pdb_status}>
                        <Radio value={0}>不使用</Radio>
                        {/* <Radio value={1}>有GPCR相关PDB结构</Radio> */}
                        <Radio value={2}>有PDB结构</Radio>
                      </Radio.Group>
                    </Form.Item>
                    <Form.Item label='' {...formTailLayout}>
                      <Button type='primary' style={{ width: 150 }} size='large' loading={this.state.loading} onClick={this.getDrugsList}>
                        Search
                      </Button>
                    </Form.Item>
                    <Form.Item label='Tips' {...formTailLayout2}>
                      <div className='Tips-div'>
                        <div>1.本页面提供Kinase相关药物的搜索。</div>
                        <div>2.您可以直接使用药物名称或者ChEMBL ID进行搜索。</div>
                        <div>3.您可以使用所需结构或者结构片段以及对应的Smiles进行搜索。</div>
                        <div> 4.您可以筛选其中有PDB结构的配体进行搜索。</div>
                        <div> 5.您可以根据所查看靶点来检索配体。</div>
                      </div>
                    </Form.Item>
                  </Form>
                </div>
              </div>
            </TabPane>
            <TabPane tab='Target' key='Target'>
              <div className='top-div'>
                <div>
                  <Form layout='horizontal'>
                    <Form.Item label='请选择Class' {...formItemLayout}>
                      <Select value={this.state.TargetSearchData.target_class} style={{ width: 250 }} size='large' placeholder='请选择' onChange={this.getTargetClassData}>
                        {TargetClassList &&
                          TargetClassList.length > 0 &&
                          TargetClassList.map((item, index) => {
                            return (
                              <Option key={index} value={item.class}>
                                {item.class}
                                {/* {item.name}({item.number}) */}
                              </Option>
                            );
                          })}
                      </Select>
                      <Select value={this.state.TargetSearchData.target_family} style={{ width: 250 }} size='large' placeholder='请选择' onChange={this.childTargetClassChange}>
                        {TargetChildClassList &&
                          TargetChildClassList.length > 0 &&
                          TargetChildClassList.map((item, index) => {
                            return (
                              <Option key={index} value={item.name}>
                                {item.name} ({item.number})
                              </Option>
                            );
                          })}
                      </Select>
                    </Form.Item>
                    <Form.Item label='请输入Target name或Uniprot id' {...formItemLayout}>
                      <Input placeholder='请输入' style={{ width: 250 }} size='large' onChange={this.getTargetData2} value={this.state.TargetSearchData.target_data} />
                    </Form.Item>
                    <Form.Item label='使用PDB查询' {...formItemLayout}>
                      <Radio.Group onChange={this.onChangeRadio2} value={this.state.TargetSearchData.pdb_status}>
                        <Radio value={0}>不使用</Radio>
                        {/* <Radio value={1}>有GPCR相关PDB结构</Radio> */}
                        <Radio value={2}>有PDB结构</Radio>
                      </Radio.Group>
                    </Form.Item>

                    <Form.Item label='' {...formTailLayout}>
                      <Button type='primary' style={{ width: 150 }} size='large' loading={this.state.loading} onClick={this.getTargetList}>
                        Search
                      </Button>
                    </Form.Item>
                    <Form.Item label='Tips' {...formTailLayout2}>
                      <div className='Tips-div'>
                        <div>1. 本页面提供Kinase相关靶点数据检索功能。</div>
                        <div>2. 您可以直接检索靶点Uniprot名称或者ID，精准搜索您所需要的靶点。</div>
                        <div>3. 您可以选择查看有PDB结构的靶点。</div>
                      </div>
                    </Form.Item>
                  </Form>
                </div>
              </div>
            </TabPane>
            <TabPane tab='Ligand' key='Ligand'>
              <div className='top-div'>
                <div>
                  <Form layout='horizontal'>
                    <Form.Item label='输入Chemblid或Smiles' {...formItemLayout}>
                      <Input placeholder='请输入' style={{ width: 250 }} size='large' onChange={this.getLigendValue} value={this.state.ligandSearchData.ligand_data} />
                    </Form.Item>
                    <Form.Item label='' {...formTailLayout}>
                      <Button type='primary' style={{ marginRight: '50px' }} onClick={this.getDraw}>
                        画结构
                      </Button>
                      <Button type='primary' style={{ marginLeft: 50, width: 150 }} size='large' loading={this.state.loading} onClick={this.getLigendList}>
                        Search
                      </Button>
                      <div className={draw ? 'bottom-div' : 'bottom-div isShowDiv'}>
                        <Radio.Group onChange={this.onChangeRadio4} value={this.state.ligandSearchData.finger}>
                          <Radio value={0}>Substructure</Radio>
                          {/* <Radio value={1}>有GPCR相关PDB结构</Radio> */}
                          <Radio value={1}>Similar Structure</Radio>
                        </Radio.Group>
                      </div>
                      <div className={draw ? 'bottom-div' : 'bottom-div isShowDiv'} onClick={this.getJsme}>
                        <Jsme height='500px' width='700px' options='oldlook,star' onChange={this.logSmiles2} />
                      </div>
                    </Form.Item>
                  </Form>
                </div>
              </div>
            </TabPane>
            <TabPane tab='Indication' key='Indication'>
              <div className='top-div'>
                <div>
                  <Form layout='horizontal'>
                    <Form.Item label='输入适应症进行检索' {...formItemLayout}>
                      <Input placeholder='请输入适应症' style={{ width: 250 }} size='large' onChange={this.getIndicationValue} value={this.state.IndicationSearchData.indication} />
                      <Button type='primary' style={{ marginLeft: 50, width: 150 }} size='large' loading={this.state.loading} onClick={this.getIndicationList}>
                        Search
                      </Button>
                    </Form.Item>
                  </Form>
                </div>
              </div>
            </TabPane>
          </Tabs>
        </div>
      </div>
    );
  }
}

export default Home;
