import React from "react";
import {inject, observer} from "mobx-react";
import {Button, Modal} from "antd";
import {toJS} from "mobx";
import {WeaDialog, WeaLocaleProvider, WeaRightMenu,WeaFormItem,WeaSearchGroup, WeaTableEditable } from "ecCom";
import { WeaSwitch} from "comsMobx";

const getLabel = WeaLocaleProvider.getLabel;
const {confirm} = Modal;
const store = ["todoClientStore"];

@inject(...store)
@observer
export default class EditFormDialog extends React.Component {
  closeDialog() {
    const {todoClientStore} = this.props;
    todoClientStore.setState({showNewFormDialog: false})
  }

  getBtns = () => {
    let btns = []
    btns = [
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@qakarz@integrationTodoclientButtonSave`} type='primary' onClick={() => this.onRightMenuClick('BTN_SAVE')}>{getLabel(86,'保存')}</Button>
    ];

    return btns;
  }
  getRightMenu() {
    let btnArr = [
      {
        key: 'BTN_SAVE',
        icon: <i className='icon-coms-Preservation'/>,
        content : `${getLabel(86,'保存')}`,
        onClick : () => this.onRightMenuClick('BTN_SAVE')
      },
    ]
    return btnArr;
  }
  onRightMenuClick = (type,recordId) => {
    const {todoClientStore} = this.props;
    const {newForm, status} = todoClientStore;
    const { newFormHiddenFields,paramsData } = toJS(todoClientStore.status);
    if(type == 'BTN_SAVE'){
      newForm.validateForm().then(f=>{
        if(f.isValid){

          let params = Object.assign(newFormHiddenFields, newForm.getFormParams());
          params.dtinfo_paramsData = JSON.stringify(paramsData);
          todoClientStore.getOfsSendOperation(params,(result)=>{
            if(result&&result.errorMsg){
              message.error(result.errorMsg);
              return;
            }else{

            }
          });


        }else{
          f.showErrors();
          this.setState({date: new Date()});
        }
      });

    }
  }

  willDelete = (datas, keys) => new Promise((resolve, reject) => {
    confirm({
      content: getLabel(385625, '确定要删除选择的记录吗？'),
      onOk: () => resolve(true),
      onCancel: () => resolve(false),
    })
  })

  render() {
    const {todoClientStore} = this.props;
    const status = toJS(todoClientStore.status);
    const {paramsColumnsSet,paramsData, newFormHiddenFields} = toJS(status);

    let title = newFormHiddenFields.sysid ? getLabel('382193', '推送信息维护') : getLabel('382189', '推送信息注册');
    return <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@aqh9bg`}
      title={title}
      onCancel={() => this.closeDialog()}
      visible={status.showNewFormDialog}
      style={{width: 800, height: 600}}
      hasScroll
      moduleName="integration"
      buttons={[
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@zhjdej@integrationTodoclientButtonSave`} type="primary" onClick={() => this.onRightMenuClick('BTN_SAVE')}>{getLabel('86', '保存')}</Button>
      ]}
      moreBtn={{
        datas: this.getRightMenu(),
        collectParams: []
      }}
    >
      <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@glg0y3`}
        datas={this.getRightMenu()}
        collectParams={[]}>
        {todoClientStore.newForm.render({col: 1, center: true}, {
          onChange: (el, value) => {
          },
          isHide: (key, formParams)=>{
          }
        },true,(c, idx) => {
          if(idx==0||idx==2){
            let items = [];
            let form = todoClientStore.newForm;
            let formParams = form.getFormParams();
            c.items.map(fields => {
              items.push({
                com: (<div style={{marginLeft: idx == 0 ? 0 : -20}}>
                  <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@iu91mq@integrationTodoclientForm${fields.domkey[0]}`}
                    label={`${fields.label}`}
                    labelCol={{span: `${fields.labelcol}`}}
                    wrapperCol={{span: idx == 0 ? `${fields.fieldcol}` : 24}}
                    error={form.getError(fields)}
                    tipPosition="bottom"
                  >
                    {/* <nobr> */}
                        <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@f4s0g1@integrationTodoclientForm${fields.domkey[0]}`} fieldConfig={fields} form={form} formParams={formParams} />
                    {/* </nobr> */}

                  </WeaFormItem></div>),
                colSpan: 1,
                hide: form.isHide(fields, (key, formParams) => {
                  let hide = false;
                  return hide;
                }),
                onChange: (v)=> {
                  // console.log(v);
                }
              })
            });

            return <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@khd6hw`} center={idx==0?true:false}  col={1}  needTigger={true} title={c.title} showGroup={c.defaultshow} items={items}/>
          }
          if(idx==1){
            return <div style={{marginLeft: 25, marginRight: 25}}>
              <WeaTableEditable ecId={`${this && this.props && this.props.ecId || ''}_WeaTableEditable@03k7z4`}
                title={<label>{`${getLabel(17632, "参数设置")}`}</label>}
                tableProps={{showEmptyText: false}}
                showCollapse={true}
                columns={paramsColumnsSet}
                datas={paramsData}
                draggable={false}
                showAdd={true}
                showDelete={true}
                showCopy={false}
                // getRowSelection={a => {return false} }
                willAdd={(datas, ds) => todoClientStore.setState({paramsData:datas})}
                willDelete={this.willDelete}
                willCopy={(datas, ds) => {}}
                onChange={(datas) =>todoClientStore.setState({paramsData:datas})}
              />
            </div>
          }

        })}
      </WeaRightMenu>
  
    </WeaDialog>
  }
}
