//新床位模态框
import React from 'react';
import { Modal,Select,Button,Table,message,Input,Row,Popconfirm} from 'antd';
import { $http } from '../../../containers/config/https';

const Option = Select.Option;
let timeout;
let user;
export default class DepartmentFindModel extends React.Component {
    constructor(props){
        super(props);
        this.state={
            visible: false,
            datasource: [],
            hbBedDr:'',
            medData:[],
            ordDr:'',
           // value:'',
        };
        user= window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        this.columnr = [{
          title: '项目代码',
          dataIndex: 'bedFeeCode',
          key: '1',
          width: 150,
          align: "center"
      }, {
          title: '项目名称',
          dataIndex: 'bedFeeDesc',
          key: '2',
          width: 120,
          align: "center"
      }, {
          title: '项目单价',
          dataIndex: 'bedFeePrice',
          key: '3',
          width: 120,
          align: "center"
      }, {
          title: '操作',
          dataIndex: '',
          key: '4',
          width: 100,
          align: "center",
          render: (text, record) => (
              <div>
                  <div>
                      {
                          <Popconfirm title="删除不可恢复，你确定要删除吗?"  onConfirm={() => this.handleDelete(record)} >
                              <span className="span" style={{color: 'rgba(240,65,52,1)'}}>删除</span>
                          </Popconfirm>
                      }
                  </div>

              </div>
          )
      }
      ];
    }
    componentDidMount() {
    //  this.getSelectDwonData();
    }
    componentWillReceiveProps(nextprops){
      //console.log(nextprops)
      // this.getSelectDwonData();
     if(nextprops.visible===true)
     {
      this.handleQuery(nextprops.record.bedID);
     }
    }
    // handleChange=(value)=> {
    //  //console.log(`selected ${value}`);
    //   this.setState({
    //     ordDr:value
    //   });
    // }
  
		handleChange = (id)=>{
			this.state.medData.forEach((item)=>{
				if(item.id==id){
						this.setState({
						ordDr:item.id,
					
					})
				}
			})
		
		}
	  
    handleQuery = (hbBedDr) => {
      // console.log(hbBedDr)
      let data = {
        params: [{
          bedID:hbBedDr,
        }]
      }
      $http.post('urlS', this, {
        code: "01050026",
        data: data,
        success: function (res) {
          // console.log(res)
          if (+res.errorCode === 0) {
            this.setState({
               datasource: res.result,
               hbBedDr:hbBedDr,
              })
          } else {
            message.error('查询失败：' + res.errorMessage);
          }
        }
      })
    }
    //获取下拉框数据
    GetMedItem=(value,callback)=>{
			if (timeout) {
				clearTimeout(timeout);
				timeout = null;
			}
			function getMedItemData(){
					if(value===""){
						this.setState({ medData:[]})
						return 
					}
					let data = {
						params: [{
              hospID:user.hospID,
							findCode:value,
							// detailFlag:"1"
						}]
					}
					$http.post('urlS', this, {
						code: "01040006",
						data: data,
						success: function (res) {
							if (+res.errorCode === 0) {
								callback(res.result.Data)

							} 
						}
					})
				}
				timeout = setTimeout(getMedItemData, 1000);
      }
      handleSerach=(value)=>{
			var reg = /\s/g;
			if(reg.test(value)) {
				message.error('不能输入空字符')
				return 
			}
 			this.GetMedItem(value,data => this.setState({ medData:data}))
		
	 } 
		
    // getSelectDwonData = () =>	{
    //   let user = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
    //   let data = {
    //     params: [{
    //              hospitalId:user.hospDr
    //           }]
    //   }
    //   $http.post('urlS', this, {
    //     code: "01010002",
    //     data: data,
    //     success: function (res) {
    //       if (+res.errorCode === 0) {
    //           this.setState({ medData: res.result.Data})
    //       } 
    //     }
    //   })
    // }   

    onChange = (e) => {
      const { value } = e.target;
      const reg =/^[1-9]\d*$/;
      if ((!Number.isNaN(value) && reg.test(value)) || value === '' || value === '') {
        this.setState({value:value})
      }
    }
      // 新增固定费用
      handelSave = () => {
        let user = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        let data = {
            params: [{
              bedID:this.state.hbBedDr,
              bedFeeQty:this.state.value,
              bedMedID:this.state.ordDr,
              updateUserID:user.userID
            }]
        }
        $http.post('urlS', this, {
            code: "01050027",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    message.success('新增成功');
                    //刷新列表数据
                    this.handleQuery(this.props.record.bedID);
                   
                }
            }
        })
    }
    // 删除
		handleDelete = (record) => {
			let user = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
			let data = {
				params: [{
				  bedFeeID: record.bedFeeID,
					updateUserID: user.userID
				}]
			}
			$http.post('urlADS', this, {
				code: '01050028',
				data: data,
				success: function (res) {
					if (+res.errorCode === 0) {
						message.success('删除成功');
						// 刷新列表数据
            this.handleQuery(this.props.record.bedID);
					} else {
						message.error('删除失败：' + res.errorMessage);
					}
				}
			})
		}
    showModal = () => {
        this.setState({
          visible: true,
        });
      }
    
      handleOk = (e) => {
       // console.log(e);
        this.setState({
          visible: false,
        });
      }
    
      handleCancel = (e) => {
        //console.log(e);
        this.setState({
          visible: false,
        });
      }
    render() {
        return(
            <Modal
            title="设置床位固定费用"
            visible={this.props.visible}
            onOk={this.props.hideModal}
            onCancel={this.props.hideModal}
            width="50%"
            height="600px"
          >
          <Row>
            项目名称：<Select
                	allowClear
                  showSearch
                  optionFilterProp="title"
                  style={{ width: 200 }}
                  placeholder="请输入"
                  onChange={this.handleChange}
                  onSearch={this.handleSerach}
                  filterOption={false}
                >
                   {
                          this.state.medData.map(
                              (item)=>{
                                return <Option key={item.id} value={item.id} title={item.descripts + "^"+item.descriptsSPCode}>{item.descripts}</Option>
                              }
                          )

                  }
            </Select>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 数量：<Input placeholder="请输入"  allowClear  value={this.state.value} onChange={this.onChange} style={{width:'220px',marginLeft:'10px'}} />
            <Button type="primary"  onClick={this.handelSave} style={{ marginBottom: '12px', marginLeft: '24px', display: 'inline-block' }}>增加</Button>
            </Row>
            <Table
                    bordered
                    style={{ height: '400px', maxHeight: '400px', overflow: 'scroll' }}
                    columns={this.columnr}
                    dataSource={this.state.datasource}
                    rowKey={(row) => {
                      return row.id
                    }}
                    //onRow={this.onClickRow1}
                    pagination={false}
                />
          </Modal>
        )
    }
}