import {
  Row,
  Col,
  Button,
} from 'antd';
import {
  WeaSwitch,
} from 'comsMobx';
import {
  WeaFormItem,
  WeaLocaleProvider,
} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

const downloadCvs2Img = (params) => {
  const {
    cvs,
    title
  } = params;

  let $a = document.createElement('a');
  $a.download = title + '.jpg';
  $a.target = '_blank';
  let url;
  if (cvs) {
    url = cvs.toDataURL("image/jpg");
  }

  if (url) $a.href = url;

  if (typeof MouseEvent === 'function') {
    var evt = new MouseEvent('click', {
      view: window,
      bubbles: true,
      cancelable: false
    });
    $a.dispatchEvent(evt);
  } else {
    if (window.navigator.msSaveOrOpenBlob) {
      var bstr = atob(url.split(',')[1]);
      var n = bstr.length;
      var u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      var blob = new Blob([u8arr]);
      window.navigator.msSaveOrOpenBlob(blob, title + '.jpg');
    } else {
      var html = '' +
        '<body style="margin:0;">' +
        '<img src="' + url + '" style="max-width:100%;" title="" />' +
        '</body>';
      var tab = window.open();
      tab.document.write(html);
    }
  }
}

const format = (fmt) => {
  const date = new Date();
  var o = {
    "M+": date.getMonth() + 1, //月份   
    "d+": date.getDate(), //日   
    "h+": date.getHours(), //小时   
    "m+": date.getMinutes(), //分   
    "s+": date.getSeconds(), //秒   
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
    "S": date.getMilliseconds() //毫秒   
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

const getSearchsAd = (params) => {
  const {
    isFormInit,
    conditions,
    form
  } = params;

  let arr = [];
  isFormInit && conditions.map((c, i) => {
    c.items.map((field, index) => {
      arr.push(<Col ecId={`${this && this.props && this.props.ecId || ''}_Col@d7e446@${index}`} span={10} offset={1}>
                  <div style={{marginTop: 20}}>
                      <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@p1po25@${index}`}
                          label={`${field.label}`}
                          labelCol={{span: 6}}
                          wrapperCol={{span: 18}}>
                              {<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@xeusul@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} />}
                      </WeaFormItem>
                  </div>
              </Col>)
    })
  })

  return <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@c0cjzg`}>{arr}</Row>;
}

const getButtonsAd = (params) => {
  const {
    form,
    search,
    cancel,
  } = params;

  let buttonsAd = [];
  [0, 1, 2].map((v, i) => {
    const name = (v == 0) ? getLabel(82529, '搜索') : (v == 1) ? getLabel(27088, '重置') : getLabel(32694, '取消');
    buttonsAd.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@h4x0ik@${i}`} type={v==0 ? "primary" : ''} onClick={() => {v==1 ? form.reset() : v== 0 ? search() : cancel()}}>{name}</Button>);
  });

  return buttonsAd;
}

const tabs = {
  getSearchsAd,
  getButtonsAd,
}


import {
  message,
} from 'antd';

const fetch = (params) => {
  const {
    name,
    fetchParams = {},
    logic,
    Api,
  } = params;

  Api[name](fetchParams).then(cb => {
    const {
      api_status,
    } = cb;

    if (api_status) {
      logic(cb);
    } else {
      message.error();
    }
  }).catch(error => {
    message.error();
  });
}

export {
  downloadCvs2Img,
  format,
  tabs,
  fetch,
}