/*
 * Creator:     ML
 * CreatDate:   2019-09-19
 * descripts：卡类型添加/编辑
 * */
import React from 'react';
import { Form, Row, Col, Input, Select, Button, DatePicker, Modal, Table, Icon, Pagination, message, Divider, Tooltip,Checkbox } from 'antd';
import moment from 'moment';
import { $http } from '../../../containers/config/https';
import store from '../../../store';
import '../css/error.less';

const FormItem = Form.Item;
const Option = Select.Option;
const confirm = Modal.confirm;
const userData = sessionStorage.userData ? JSON.parse(sessionStorage.userData) : {}
const CheckboxGroup = Checkbox.Group

const formItemLayout = { // label 和input宽度
  labelCol: { span: 8 },
  wrapperCol: { span: 14 },
}
const formItemLayoutCheck = {
    labelCol: { span: 7 },
    wrapperCol: { span: 25 },
  }
const formItemLayoutCheckBox = { // label 和input宽度
  labelCol: { span: 8 },
  wrapperCol: { span: 24 },
}

const formItemLayoutMoney = {
  labelCol: { span: 12 },
  wrapperCol: { span: 10 },
}

const formItemNumber = {
  labelCol: { span: 9 },
  wrapperCol: { span: 14 },
}

const formItemMoney = {
  labelCol: { span: 11 },
  wrapperCol: { span: 11 },
}

const formItemCard = {
  labelCol: { span: 10 },
  wrapperCol: { span: 23 },
}

const formItemNewCard = {
  labelCol: { span: 8 },
  wrapperCol: { span: 16 },
}

const formItemNew = {
  labelCol: { span: 2 },
  wrapperCol: { span: 16 },
}

const formItemNewButton = {
  labelCol: { span: 2 },
  wrapperCol: { span: 22 },
}
const CardTypeModal = Form.create()(
  class CardTypeModal extends React.Component {
    constructor(props) {
      super(props)
      this.state = {
        visible:false,
        baseParms:{},
        edit:-1,
        params:{},
        cardId:"",
        isINVChecked:false,
        isValidCardChecked: false,
        isRetrieveCardChecked: false,
        isLossCardChecked: false,
        isChangeCardChecked: false,
        isRepairCardChecked: false,
        isWriteCardChecked: false,
        isRepairCardCostChecked: false,
        isDefalutCardTypeChecked: false,
        isAutoReplenishZeroChecked:false,
      }
    }
    componentDidMount() {
        this.props.OnChildrefs(this);
        
    }
    showModal=(record,num)=>{
        this.getCardTypeMan()
        this.setState({
            visible: true,
        })
        if(record&&num&&num=="1"){
            let arr = []
            this.setState({
                edit:num,
                params:record,
                cardId:record.id,
                isINVChecked:record.INVFlag=="Y"?true:false,
                isValidCardChecked: record.verifyFlag=="Y"?true:false,
                isRetrieveCardChecked: record.retrieveFlag=="Y"?true:false,
                isLossCardChecked: record.lossFlag=="Y"?true:false,
                isChangeCardChecked: record.changeFlag=="Y"?true:false,
                isRepairCardChecked: record.repairFlag=="Y"?true:false,
                isWriteCardChecked: record.writeFlag=="Y"?true:false,
                isRepairCardCostChecked: record.repariFeeFlag=="Y"?true:false,
                isDefalutCardTypeChecked: record.defaultFlag=="Y"?true:false,
                isAutoReplenishZeroChecked:record.autoReplenishZero=="Y"?true:false,
            })
        }
    }
    getCardTypeMan=()=>{
        let data = {}
        data = {
          params: [{
            userID:JSON.parse(sessionStorage.userData).userID || '',
            hospID:JSON.parse(sessionStorage.userData).hospID || '',
            groupID:JSON.parse(sessionStorage.userData).groupID || '',
            // "userID": "60",
            // "hospID": "1",
            // "groupID": "231",
          }],
        }
        $http.post('urlS', this, {
          code: '01040098',
          data: data,
          success: function (res) {
            if (+res.errorCode === 0 && res.result) {
              this.setState({ baseParms: res.result }, () => {
              })
            }
          }
        })
    }
    handleCancel = () => {
        this.props.form.resetFields();
        this.setState({visible: false,params:{},edit:-1,cardId:""});
        this.initCheck()
      }
      initCheck=()=>{
          this.setState({
            isINVChecked:false,
            isValidCardChecked: false,
            isRetrieveCardChecked: false,
            isLossCardChecked: false,
            isChangeCardChecked: false,
            isRepairCardChecked: false,
            isWriteCardChecked: false,
            isRepairCardCostChecked: false,
            isDefalutCardTypeChecked: false,
            isAutoReplenishZeroChecked:false,
          })
      }
      //checkBox事件
      changeChecked = (e, num) => {
        switch (num) {
            case 0:
            this.setState({
                isINVChecked: e.target.checked
            })
            break;
          case 1:
            this.setState({
                isValidCardChecked: e.target.checked
            })
            break;
          case 2:
            this.setState({
                isRetrieveCardChecked: e.target.checked
            })
            break;
          case 3:
            this.setState({
                isLossCardChecked: e.target.checked
            })
            break;
          case 4:
            this.setState({
                isChangeCardChecked: e.target.checked
            })
            break;
          case 5:
            this.setState({
                isRepairCardChecked: e.target.checked
            })
            break;
            case 6:
            this.setState({
                isWriteCardChecked: e.target.checked
            })
            break;
            case 7:
            this.setState({
                isRepairCardCostChecked: e.target.checked
            })
            break;
            case 8:
            this.setState({
                isDefalutCardTypeChecked: e.target.checked
            })
            break;
            case 9:
                this.setState({
                    isAutoReplenishZeroChecked: e.target.checked
                })
                break;
          default:
  
        }
      }
      //添加/编辑卡
      handleCreat=()=>{
        this.props.form.validateFields((err, values) => {
            if(!err){
                let data = {}
                if(this.state.edit=="1"){
                    data = {
                        params: [{
                            userID:JSON.parse(sessionStorage.userData).userID || '',
                            hospID:JSON.parse(sessionStorage.userData).hospID || '',
                            groupID:JSON.parse(sessionStorage.userData).groupID || '',
                            // "userID": "60",
                            // "hospID": "1",
                            // "groupID": "231",
                            id:this.state.cardId&&this.state.cardId?this.state.cardId:"",//修改
                            selectHospID: values.hospitalID,//所属医院
                            code: values.code,//代码
                            descripts: values.typeName,//类型名称
                            readMode: values.cardMode=="0"?"":values.cardMode,//卡号输入模式
                            cardLength: values.cardLength,//卡号长度
                            equipManagerID: values.readCard,//读卡设备
                            cardCost: values.cardMoney,//卡费用
                            INVTypeID: values.invoiceCategory,//发票类别
                            INVPrtName: values.invoiceTemplate,//发票模板
                            validAccType: values.cardMoneyMethods,//预交金收费验证方式
                            INVFlag:this.state.isINVChecked == true ? "Y" : "N",
                            verifyFlag: this.state.isValidCardChecked == true ? "Y" : "N",
                            retrieveFlag:this.state.isRetrieveCardChecked == true ? "Y" : "N",
                            lossFlag: this.state.isLossCardChecked == true ? "Y" : "N",
                            changeFlag: this.state.isChangeCardChecked == true ? "Y" : "N",
                            repairFlag:this.state.isRepairCardChecked == true ? "Y" : "N",
                            writeFlag:this.state.isWriteCardChecked == true ? "Y" : "N",
                            repariFeeFlag:this.state.isRepairCardCostChecked == true ? "Y" : "N",
                            autoReplenishZero:this.state.isAutoReplenishZeroChecked == true ? "Y" : "N",
                            defaultFlag: this.state.isDefalutCardTypeChecked == true ? "Y" : "N",
                            startDate: values.dateFrom ? moment(values.dateFrom).format('YYYY-MM-DD') : undefined,//生效日期
                            stopDate: values.dateTo ? moment(values.dateTo).format('YYYY-MM-DD') : undefined,//失效日期
                        }]
                    }
                }else{
                    data = {
                        params: [{
                            userID:JSON.parse(sessionStorage.userData).userID || '',
                            hospID:JSON.parse(sessionStorage.userData).hospID || '',
                            groupID:JSON.parse(sessionStorage.userData).groupID || '',
                            // "userID": "60",
                            // "hospID": "1",
                            // "groupID": "231",
                            selectHospID: values.hospitalID,//所属医院
                            code: values.code,//代码
                            descripts: values.typeName,//类型名称
                            readMode: values.cardMode=="0"?"":values.cardMode,//卡号输入模式
                            cardLength: values.cardLength,//卡号长度
                            equipManagerID: values.readCard,//读卡设备
                            cardCost: values.cardMoney,//卡费用
                            INVTypeID: values.invoiceCategory,//发票类别
                            INVPrtName: values.invoiceTemplate,//发票模板
                            validAccType: values.cardMoneyMethods,//预交金收费验证方式
                            INVFlag:this.state.isINVChecked == true ? "Y" : "N",
                            verifyFlag: this.state.isValidCardChecked == true ? "Y" : "N",
                            retrieveFlag:this.state.isRetrieveCardChecked == true ? "Y" : "N",
                            lossFlag: this.state.isLossCardChecked == true ? "Y" : "N",
                            changeFlag: this.state.isChangeCardChecked == true ? "Y" : "N",
                            repairFlag:this.state.isRepairCardChecked == true ? "Y" : "N",
                            writeFlag:this.state.isWriteCardChecked == true ? "Y" : "N",
                            repariFeeFlag:this.state.isRepairCardCostChecked == true ? "Y" : "N",
                            autoReplenishZero:this.state.isAutoReplenishZeroChecked == true ? "Y" : "N",
                            defaultFlag: this.state.isDefalutCardTypeChecked == true ? "Y" : "N",
                            startDate: values.dateFrom ? moment(values.dateFrom).format('YYYY-MM-DD') : undefined,//生效日期
                            stopDate: values.dateTo ? moment(values.dateTo).format('YYYY-MM-DD') : undefined,//失效日期
                        }]
                    }
                }
                $http.post('urlS', this, {
                    code: '01040095',
                    data: data,
                    success: function (res) {
                        if (+res.errorCode === 0) {
                            if(this.state.edit=="1"){
                                message.success('修改成功');
                                this.props.getNewCard();
                            }else{
                                message.success('添加成功');
                                this.props.getNewCard(res.result);
                            }
                            this.setState({
                                visible: false,
                                edit:-1,
                                params:{},
                                cardId:""
                            })
                            this.props.form.resetFields();
                            this.initCheck()
                        }  
                    }
                })
            }
        })
      }
    render() {
      const { getFieldDecorator, } = this.props.form;
      const {baseParms,params} = this.state
      //医院
      let hosptilOption = []
      baseParms&&baseParms.hosp && baseParms.hosp.map(item => {
        hosptilOption.push(<Option key={item.id}  value={item.id} title={item.descripts}>{item.descripts}</Option>)
      })
      //发票类型
      let invTypeOption = []
      baseParms&&baseParms&&baseParms.invType && baseParms.invType.map(item => {
      invTypeOption.push(<Option key={item.ID}  value={item.ID} title={item.Descripts}>{item.Descripts}</Option>)
      })
       //读卡设备
      let equipManagerOption = []
      baseParms&&baseParms&&baseParms.equipManager && baseParms.equipManager.map(item => {
          equipManagerOption.push(<Option key={item.id}  value={item.id} title={item.descripts}>{item.descripts}</Option>)
      })
      //卡号输入模式
      let readModeOption = []
      baseParms&&baseParms&&baseParms.readMode && baseParms.readMode.map(item => {
          readModeOption.push(<Option key={item.id}  value={item.id} title={item.descripts}>{item.descripts}</Option>)
      })
      
      return (
          <div style={{ padding: '4px 2px' }} className="cardListDom">
        <Modal
          className='findPatients'
          width='1000px'
          visible={this.state.visible}
          title={this.state.edit=="1"?"编辑卡类型":"添加卡类型"}
          onOk={this.handleOk}
          onCancel={this.handleCancel}
          footer={[
            <Button key="submit" type="primary" loading={this.setState.loading} disabled={this.state.rowId==""} onClick={this.handleCreat}>确定</Button>,
            <Button key="cancel" type="primary" loading={this.setState.loading} onClick={this.handleCancel}>取消</Button>,
          ]}>
          <div className="department-header">
            <Form>
              <Row gutter={60} style={{ margin: '0' }}>
                <Col span={9}>
                  <FormItem label='所属医院' {...formItemLayout}>
                    {getFieldDecorator('hospitalID', {
                      initialValue: params&&params.selectHospID?params.selectHospID:undefined,
                      rules: [{ required: false, message: '' }],
                    })(
                      <Select showSearch allowClear optionFilterProp="title" placeholder='请选择医院' style={{ width: '100%' }} >
                        {hosptilOption}
                      </Select>
                    )}
                  </FormItem>
                </Col>
                <Col span={9} style={{ marginLeft: '-60px' }}>
                  <FormItem label='代码' {...formItemNumber}>
                    {getFieldDecorator('code', {
                      initialValue:  params&&params.code?params.code:undefined,
                      rules: [{ required: true, message: '请输入代码' }],
                    })(
                      <Input allowClear onPressEnter={this.findCard} />
                    )}
                  </FormItem>
                </Col>
                <Col span={9} style={{ marginLeft: '-60px' }}>
                  <FormItem label='类型名称' {...formItemNumber}>
                    {getFieldDecorator('typeName', {
                      initialValue: params.descripts&&params.descripts?params.descripts:undefined,
                      rules: [{ required: true, message: '请选择类型名称' }],
                    })(
                      <Input allowClear onPressEnter={this.findCard} />
                    )}
                  </FormItem>
                </Col>
              </Row>
              <Row gutter={60} style={{ margin: '0' }}>
                <Col span={9}>
                  <FormItem label='卡号输入模式' {...formItemLayout}>
                    {getFieldDecorator('cardMode', {
                      initialValue:params.readMode&&params.readMode==""?"0":(params.readMode&&params.readMode?params.readMode:"0"),
                      rules: [{ required: false, message: '' }],
                    })(
                      <Select showSearch allowClear optionFilterProp="title" placeholder='请选择卡号输入模式' style={{ width: '100%' }} >
                        {readModeOption}
                      </Select>
                    )}
                  </FormItem>
                </Col>
                <Col span={9} style={{ marginLeft: '-60px' }}>
                  <FormItem label='卡号长度' {...formItemNumber}>
                    {getFieldDecorator("cardLength", {
                      initialValue:params.cardLength&&params.cardLength?params.cardLength:undefined,
                      rules: [{ required: false, message: '' }],
                    })(
                      <Input type="number" min={1} allowClear onPressEnter={this.findCard} />
                    )}
                  </FormItem>
                </Col>
                <Col span={9} style={{ marginLeft: '-60px' }}>
                  <FormItem label='读卡设备' {...formItemNumber}>
                    {getFieldDecorator("readCard", {
                      initialValue: params.equipManagerID&&params.equipManagerID?params.equipManagerID:undefined,
                      rules: [{ required: false, message: '' }],
                    })(
                        <Select showSearch allowClear optionFilterProp="title"  style={{ width: '100%' }} >
                        {equipManagerOption}
                      </Select>
                    )}
                  </FormItem>
                </Col>
              </Row>
              <Row gutter={60} style={{ margin: '0' }}>
                <Col span={9}>
                  <FormItem label='卡费用' {...formItemLayout}>
                    {getFieldDecorator('cardMoney', {
                      initialValue: params.cardCost&&params.cardCost?params.cardCost:undefined,
                      rules: [{ required: false, message: '' }],
                    })(
                        <Input type="number" allowClear onPressEnter={this.findCard} />
                    )}
                  </FormItem>
                </Col>
                <Col span={9} style={{ marginLeft: '-60px' }}>
                  <FormItem label='发票类别' {...formItemNumber}>
                    {getFieldDecorator("invoiceCategory", {
                      initialValue:params.INVTypeID&&params.INVTypeID?params.INVTypeID:undefined,
                      rules: [{ required: false, message: '' }],
                    })(
                        <Select showSearch allowClear optionFilterProp="title"  style={{ width: '100%' }} >
                        {invTypeOption}
                      </Select>
                    )}
                  </FormItem>
                </Col>
                <Col span={9} style={{ marginLeft: '-60px' }}>
                  <FormItem label='发票模板' {...formItemNumber}>
                    {getFieldDecorator("invoiceTemplate", {
                      initialValue: params.INVPrtName&&params.INVPrtName?params.INVPrtName:undefined,
                      rules: [{ required: false, message: '' }],
                    })(
                    //     <Select showSearch allowClear optionFilterProp="title"  style={{ width: '100%' }} >
                    //     {hosptilOption}
                    //   </Select>
                    <Input  allowClear maxLength={1} />
                    )}
                  </FormItem>
                </Col>
              </Row>
              <Row gutter={60} style={{ margin: '0' }}>
                <Col span={9}>
                  <FormItem label='预交金收费验证方式' {...formItemLayoutMoney}>
                    {getFieldDecorator('cardMoneyMethods', {
                      initialValue: params.validAccType&&params.validAccType?params.validAccType:undefined,
                      rules: [{ required: false, message: '' }],
                    })(
                    //     <Select showSearch allowClear optionFilterProp="title"  style={{ width: '100%' }} >
                    //     {hosptilOption}
                    //   </Select>
                    <Input  allowClear  maxLength={1}/>
                    )}
                  </FormItem>
                </Col>
                <Col span={9} style={{ marginLeft: '-46px' }}>
                <FormItem label='生效日期' {...formItemLayout}>
                      {getFieldDecorator('dateFrom', {
                        initialValue: params.startDate ? moment(params.startDate, "YYYY-MM-DD") :moment(),
                        rules: [{ required: true, message: '' }],
                    })(
                        <DatePicker style={{ width: '100%' }}  disabledDate={(curDate) => {
                          return curDate && curDate > moment().endOf('day');
                        }} />
                      )}
                    </FormItem>
                </Col>
                <Col span={9} style={{ marginLeft: '-74px' }}>
                  <FormItem label='失效日期' {...formItemNumber} >
                    {getFieldDecorator('dataTo', {
                      initialValue: params.stopDate ? moment(params.stopDate, "YYYY-MM-DD") :undefined,
                      rules: [{ required: false, message: '' }],
                    })(
                        <DatePicker style={{ width: '100%' }}  disabledDate={(curDate) => {
                          return curDate && curDate > moment().endOf('day');
                        }} />
                    )}
                  </FormItem>
                </Col>
              </Row>
               <Row gutter={12}>
                    <Col span={4} style={{ paddingLeft: "31px", boxSizing: "border-box" }}>
                      <FormItem  {...formItemLayoutCheck} >
                        {getFieldDecorator('isINV ', {
                          initialValue: undefined,
                          rules: [{ required: false, message: ' ' }],
                        })(
                          <Checkbox checked={this.state.isINVChecked} onChange={(e) => this.changeChecked(e, 0)}>打印发票</Checkbox>
                        )}
                      </FormItem>
                    </Col>
                    <Col span={3}>
                      <FormItem  {...formItemLayoutCheck} >
                        {getFieldDecorator('isValidCard', {
                          initialValue: undefined,
                          rules: [{ required: false, message: ' ' }],
                        })(
                          <Checkbox checked={this.state.isValidCardChecked} onChange={(e) => this.changeChecked(e, 1)}>卡校验</Checkbox>
                        )}
                      </FormItem>
                    </Col>
                    <Col span={2}>
                      <FormItem  {...formItemLayoutCheck}>
                        {getFieldDecorator('isRetrieveCard', {
                          initialValue: undefined,
                          rules: [{ required: false, message: ' ' }],
                        })(
                          <Checkbox checked={this.state.isRetrieveCardChecked} onChange={(e) => this.changeChecked(e, 2)}>回收</Checkbox>
                        )}
                      </FormItem>
                    </Col>
                    <Col span={2}>
                      <FormItem  {...formItemLayoutCheck} >
                        {getFieldDecorator('isLossCard', {
                          initialValue: undefined,
                          rules: [{ required: false, message: ' ' }],
                        })(
                          <Checkbox checked={this.state.isLossCardChecked} onChange={(e) => this.changeChecked(e, 3)}>挂失</Checkbox>
                        )}
                      </FormItem>
                    </Col>
                    <Col span={2}>
                      <FormItem  {...formItemLayoutCheck}>
                        {getFieldDecorator('isChangeCard', {
                          initialValue: undefined,
                          rules: [{ required: false, message: ' ' }],
                        })(
                          <Checkbox checked={this.state.isChangeCardChecked} onChange={(e) => this.changeChecked(e, 4)}>换卡</Checkbox>
                        )}
                      </FormItem>
                    </Col>
                    <Col span={2}>
                      <FormItem  {...formItemLayoutCheck}>
                        {getFieldDecorator('isRepairCard', {
                          initialValue: undefined,
                          rules: [{ required: false, message: ' ' }],
                        })(
                          <Checkbox checked={this.state.isRepairCardChecked} onChange={(e) => this.changeChecked(e, 5)}>补卡</Checkbox>
                        )}
                      </FormItem>
                    </Col>
                    <Col span={2}>
                      <FormItem  {...formItemLayoutCheck}>
                        {getFieldDecorator('isWriteCard', {
                          initialValue: undefined,
                          rules: [{ required: false, message: ' ' }],
                        })(
                          <Checkbox checked={this.state.isWriteCardChecked} onChange={(e) => this.changeChecked(e, 6)}>写卡</Checkbox>
                        )}
                      </FormItem>
                    </Col>
                    <Col span={3}>
                      <FormItem  {...formItemLayoutCheck}>
                        {getFieldDecorator('isRepairCardCost', {
                          initialValue: undefined,
                          rules: [{ required: false, message: ' ' }],
                        })(
                          <Checkbox checked={this.state.isRepairCardCostChecked} onChange={(e) => this.changeChecked(e, 7)}>补卡收费</Checkbox>
                        )}
                      </FormItem>
                    </Col>
                  </Row>
                  <Row gutter={12}>
                  <Col span={4} style={{paddingLeft: "31px"}}>
                      <FormItem  {...formItemLayoutCheck}>
                        {getFieldDecorator('isAutoReplenishZero', {
                          initialValue: undefined,
                          rules: [{ required: false, message: ' ' }],
                        })(
                          <Checkbox checked={this.state.isAutoReplenishZeroChecked} onChange={(e) => this.changeChecked(e, 9)}>自动补0</Checkbox>
                        )}
                      </FormItem>
                    </Col>
                    <Col span={4}>
                      <FormItem  {...formItemLayoutCheck}>
                        {getFieldDecorator('isDefalutCardType', {
                          initialValue: undefined,
                          rules: [{ required: false, message: ' ' }],
                        })(
                          <Checkbox checked={this.state.isDefalutCardTypeChecked} onChange={(e) => this.changeChecked(e, 8)}>默认卡型</Checkbox>
                        )}
                      </FormItem>
                    </Col>
                    </Row>   
            </Form>
            </div>
            </Modal>
          </div>
      )
    }
  })
export default CardTypeModal    