/*
 * Creator:     WRX
 * CreatDate:   2022-09-06
 * descripts：  查询历史单据【弃用】
 * */

/**
 需要传入参数如下：
 1、compontName --- table的表头代码
 2、interfaceCode -- table表格数据接口代码
 3、params --- table表格数据请求自定义入参
 */

import React, {Component} from 'react';
import moment from "moment";
import store from 'store';
import {$http} from "../../../containers/config/https";
import {Button, message, Select} from 'antd';
import RangeDatePicker from 'components/rangePicker/index';
import PubilcTablePagination from "../../common/PubilcTablePagination";


const Option = Select.Option;

class HistoryPriceAdjustList extends Component {
  constructor(props) {
    super(props);
    this.userData = React.$getSessionData();
    this.patData = React.$getSessionData('titleData');
    this.state = {
      page: 1,
      pageSize: 20,
      total: 0,// 预约人数
      startDate: moment(),// 制单日期
      endDate: moment(),// 制单日期
      billStatus: 'NO',// 单据状态
      totalWidth: 0,// table(Scroll-X)轴长
      tableData: [], // table数据源
      adjStatus: []
    }
  }

  componentDidMount() {
    this.initDataQuery(); // 根据父组件数据默认查询
    this.getTableColumns(); // 获取列表头数据 -- 01040073
    this.defaultSelectList() // 通用下拉
  }

  // +----------------接口相关---------------+

  // 获取列表头数据 -- 01040073
  getTableColumns = async () => {
    const {compontName} = this.props
    try {
      let data = {
        params: [{
          compontName: compontName,
          type: 'C',
        }]
      }
      let res = await React.$asyncPost(this, '01040073', data);
      let filterArr = res.result && res.result.C && res.result.C.length > 0 ? res.result.C : [];
      if (filterArr && filterArr.length > 0) {
        filterArr = filterArr && filterArr.length > 0 ? [...filterArr] : []
        this.setState({
          columns: filterArr,
          totalWidth: res.totalWidth
        })
      }
    } catch (error) {
      console.log(error)
    }
  }

  // 通用下拉列表 -- 17033006
  defaultSelectList = () => {
    let data = {
      params: [
        {
          adjStatus: 'Y',
          reason: 'Y',
          subCat: 'Y'
        }
      ]
    };
    $http.post('urlS', this, {
      code: '17033006',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({adjStatus: res.result.adjStatus})
        }
      }
    })
  };

  initDataQuery = () =>{
    const {interfaceCode,params} = this.props
    const {pageSize, page} = this.state
    let data = {
      params: [
        {
          locID:this.userData.locID,
          state:'',
          ...params
        }
      ],
      pagination: [
        {
          pageSize: pageSize,   //页面大小
          currentPage: page, //当前页
        }
      ]
    };
    $http.post('urlS', this, {
      code: interfaceCode,
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          let tableData = res.result;
          if (tableData && tableData.length > 0) {
            this.setState({tableData, total: res.result.total})
          }else{
            message.error('未查询到数据');
            this.setState({
              tableData: [],
              total: 0
            })
          }
        }
      }
    })
  }

  // 历史单据列表
  historyListQuery = () => {
    const {interfaceCode} = this.props
    const {billStatus, startDate, endDate, pageSize, page} = this.state
    let data = {
      params: [
        {
          startDate: startDate.format('YYYY-MM-DD'),  //查询开始日期
          endDate: endDate.format('YYYY-MM-DD'),    //查询结束日期
          locID:this.userData.locID,
          state: billStatus
        }
      ],
      pagination: [
        {
          pageSize: pageSize,   //页面大小
          currentPage: page, //当前页
        }
      ]
    };
    $http.post('urlS', this, {
      code: interfaceCode,
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          let tableData = res.result;
          if (tableData && tableData.length > 0) {
            this.setState({tableData, total: res.result.total})
          }else{
            message.error('未查询到数据');
            this.setState({
              tableData: [],
              total: 0
            })
          }
        }
      }
    })
  };

  // 开始时间更换
  startDateChange = (e) => {
    this.setState({startDate: moment(e)})
  }

  // 结束时间更换
  endDateChange = (e) => {
    this.setState({endDate: moment(e)})
  }

  // 分页操作
  compilePage = (page, pageSize) => {
    this.setState({page, pageSize}, () => {
      this.historyListQuery();
    })
  }

  statusChange = (option) => {
    this.setState({billStatus: option.props.code})
  }

  // 行操作
  onClickRowPublic = (record) => {
    return {
      onDoubleClick: () => {
        this.props.rowFill(record)
      }
    }
  }

  render() {
    const {compontName} = this.props
    const {total, tableData, columns, totalWidth, page, loading, startDate, endDate, adjStatus} = this.state;
    return (
      <div className="history-price-adjust-list" style={{padding: 12}}>
        <div className="gmaf-header flex-wrap-align-items" style={{marginBottom: 10}}>
          <div style={{marginRight: 15}}>
            <RangeDatePicker
              width="300px"
              label="制单日期："
              isChange={true}
              format='YYYY-MM-DD'
              className='express-select-border'
              startDate={startDate}
              endDate={endDate}
              startChange={this.startDateChange}
              endChange={this.endDateChange}
            />
          </div>
          <div style={{marginRight: 15}}>
            单据状态：
            <Select style={{width: 180, marginLeft: 5}} placeholder="请选择"
                    onChange={(value, option) => this.statusChange(option)}
            >
              {adjStatus.map(item => {
                return <Option key={item.id} code={item.code}>{item.descripts}</Option>
              })}
            </Select>
          </div>
          <div style={{float: "right"}}>
            <Button type='primary' onClick={() => {this.historyListQuery()}}>查询</Button>
          </div>
        </div>
        <PubilcTablePagination
          param={{
            page,
            total,
            loading,
            data: tableData, // 表格数据
            x: totalWidth, // 表格的宽度
            y: store.getState().tableHeight.y * 0.55,
            componentName: compontName,
            height: store.getState().tableHeight.y * 0.7,
            columns: columns && columns.length > 0 ? columns : [], // 表头配置
          }}
          compilePage={this.compilePage}
          getColumns={this.getTableColumns}
          onRef={ref => this.commonTableRef = ref}
          onClickRowPublic={this.onClickRowPublic}
        />
      </div>
    );
  }
}

export default HistoryPriceAdjustList;