/*
 * Author: 黎永顺
 * Description: 起始编号设置
 * Date: 2022-06-07 15:27:43
 * LastEditTime: 2022-06-15 10:47:55
 */
import React, { Component, Fragment } from "react";
import { WeaTableEdit, WeaInputNumber, WeaDatePicker, WeaBrowser, WeaError } from "ecCom";
import moment from 'moment';

const enumStr = {
  YEAR: '年',
  MONTH: '月份',
  DAY: '日期'
}
const browserProps = {
  iconBgcolor: '#217346',
  icon: 'icon-coms-hrm',
  isSingle: false,
  inputStyle: { width: 200 },
  viewAttr: 3
}
class StartNumberSetting extends Component {
  constructor() {
    super();
    this.state = {
      startDate: '',
      endDate: '',
      subCompanyId: '',
      jobtitlesId: ""
    }
  }
  handleDatePickerChange = (val, type) => {
    this.setState({
      [type]: val
    }, () => {
      const { startDate, endDate, subCompanyId, jobtitlesId } = this.state;
      const { onSet } = this.props;
      onSet('start', { startDate, endDate, subCompanyId, jobtitlesId })
    })
  }
  handleBrowserChange = (val, type) => {
    this.setState({
      [type]: val
    }, () => {
      const { onSet } = this.props;
      onSet('start', { ...this.state, [type]: val })
    })
  }
  handleChangeTable = (datas) => {
    const { onChange, startNumberInfo } = this.props;
    const newColumns = _.map(startNumberInfo.columns, (it) => ({
      ...it,
      com: [{ type: "INPUTNUMBER", key: it.dataIndex, viewAttr: 3, min: 0 }],
    }));
    onChange && onChange(newColumns, datas);
  };
  getRowSelection = (rowSelection) => {
    return null;
  };

  render() {
    const { startDate, endDate } = this.state;
    const { startNumberInfo, companyInfo } = this.props;
    const { deptSerial = {}, dateSerial = {}, jobtitlesSerial = {} } = companyInfo;
    const newColumns = _.map(startNumberInfo.columns, (it) => ({
      ...it,
      com: [{ type: it.dataIndex === 'startnum' ? "INPUTNUMBER" : "TEXT", key: it.dataIndex, viewAttr: 3, min: 0 }],
    }));
    const format = dateSerial.key === "YEAR" ? "YYYY" : dateSerial.key === "MONTH" ? "YYYY-MM" : "yyyy-MM-dd";
    const formatVal = dateSerial.key === "YEAR" ? "YYYY" : dateSerial.key === "MONTH" ? "YYYY-MM" : "yyyy-MM-DD";
    return (
      <Fragment>
        {dateSerial.enable == '1' && (
          <div style={{ padding: "20px 30px" }}>
            <span style={{ width: 100, display: "inline-block" }}>{enumStr[dateSerial.key]}</span>
            <WeaDatePicker format={format} value={moment(startDate ? startDate : new Date()).format(formatVal)} onChange={val => this.handleDatePickerChange(val, 'startDate')} />
            <span> — </span>
            <WeaDatePicker format={format} value={moment(endDate ? endDate : new Date()).format(formatVal)} onChange={val => this.handleDatePickerChange(val, 'endDate')} />
          </div>
        )}
        {
          (deptSerial.enable == '1' && (deptSerial.key === "SUBCOMPANY" || deptSerial.key === "DEPARTMENT")) && (
            <div style={{ paddingLeft: 30, marginTop: 20, display: "flex", alignItems: "center" }}>
              <span style={{ display: "inline-block", width: 100 }}>分部名称</span>
              <WeaError name='dept' tipPosition='bottom' error={'分部名称必填！'}>
                <WeaBrowser
                  {...browserProps}
                  title={"分部名称"}
                  type={194}
                  onChange={ids => this.handleBrowserChange(ids, 'subCompanyId')}
                />
              </WeaError>
            </div>
          )
        }
        {
          (deptSerial.enable == '1' && deptSerial.key === "DEPARTMENT") && (
            <div style={{ paddingLeft: 30, marginTop: 20, display: "flex", alignItems: "center" }}>
              <span style={{ display: "inline-block", width: 100 }}>部门名称</span>
              <WeaError name='dept' tipPosition='bottom' error={'部门名称必填！'}>
                <WeaBrowser
                  {...browserProps}
                  title={"部门名称"}
                  type={57}
                  onChange={ids => this.handleBrowserChange(ids, 'deptId')}
                />
              </WeaError>
            </div>
          )
        }
        {
          (jobtitlesSerial.enable == '1' && jobtitlesSerial.key === "JOBTITLES") && (
            <div style={{ paddingLeft: 30, marginTop: 20, display: "flex", alignItems: "center" }}>
              <span style={{ display: "inline-block", width: 100 }}>岗位名称</span>
              <WeaError name='dept' tipPosition='bottom' error={'岗位名称必填！'}>
                <WeaBrowser
                  {...browserProps}
                  title={"岗位名称"}
                  type={278}
                  onChange={ids => this.handleBrowserChange(ids, 'jobtitlesId')}
                />
              </WeaError>
            </div>
          )
        }
        <WeaTableEdit
          draggable={false}
          showAdd={false}
          showDelete={false}
          showCopy={false}
          getRowSelection={this.getRowSelection}
          columns={newColumns}
          datas={startNumberInfo.dataSource}
          onChange={this.handleChangeTable}
        />
      </Fragment>
    );
  }
}

export default StartNumberSetting;
