/*
 * Creator:     WRX
 * CreatDate:   2022-09-08
 * descripts：  视光 - 库存 - 调价单审核【弃用】
 * */
import React, {Component} from 'react';
import moment from "moment";
import {$http} from "../../../containers/config/https";
import {Button, Card, Icon, message, Input, Divider, Radio} from 'antd';
import RangeDatePicker from "../../../components/rangePicker";
import PubilcTablePagination from "../../common/PubilcTablePagination";
import store from 'store';

class PriceAdjustBillReview 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(),// 结束日期
      tableData: [],
      columns: [],
      totalWidth: '100%',
      loading: false,
      adjStatus: [],
      status: 'No',
      detailSelectedRowKeys: [],
      adjIDs: [],
      stockAlias: ''
    }
  }

  componentDidMount() {
    this.getTableColumns(); // 获取列表头数据 -- 01040073
    this.defaultSelectList();
  }

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

  // 获取列表头数据 -- 01040073
  getTableColumns = async () => {
    let operationObj = {
      width: 80,
      title: '操作',
      fixed: 'right',
      align: 'center',
      key: 'operation',
      render: (text, record) => (
        record.stateDesc === '未审核'
          ? <Button type="link" style={{color: "rgb(24, 141, 244)"}} size="small" onClick={() => {
            this.priceAdjustHandle(record.adjID)
          }}>审核</Button>
          : <Button type="link" style={{color: "red"}} size="small" onClick={() => {
            this.priceAdjustCancel(record.adjID)
          }}>撤销审核</Button>
      ),
    };
    try {
      let data = {
        params: [{
          compontName: 'PriceAdjustBillReviewList',
          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) {
        let columns = []
        columns = filterArr && filterArr.length > 0 ? [...filterArr, operationObj] : []
        this.setState({
          columns: columns,
          totalWidth: res.totalWidth
        })
      }
    } catch (error) {
      console.log(error)
    }
  }

  // 调价单审核列表 -- 17033004
  priceAdjustReviewQuery = () => {
    let data = {
      params: [
        {
          stockAlias: this.state.stockAlias,
          startDate: this.state.startDate.format('YYYY-MM-DD'),  //查询开始日期
          endDate: this.state.endDate.format('YYYY-MM-DD'),    //查询结束日期
          state: this.state.status,   //关键词
        }
      ],
      pagination: [
        {
          pageSize: this.state.pageSize,   //页面大小
          currentPage: this.state.page, //当前页
        }
      ]
    };
    $http.post('urlS', this, {
      code: '17033004',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({tableData: res.result, total: res.result.total})
        }
      }
    })
  };

  // 通用下拉列表 -- 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})
        }
      }
    })
  };

  // +---------------查询条件相关-----------------+
  // 开始时间更换
  startDateChange = (e) => {
    this.setState({startDate: moment(e)})
  }

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

  // 生效状态
  onChange = (checkedValues) => {
    this.setState({status: checkedValues.target.value, detailSelectedRowKeys: [], adjIDs: []})
  }

  // 调价审核 -- 17033008
  priceAdjustHandle = (adjID) => {
    const {startDate, endDate} = this.state
    let data = {
      params: [
        {
          adjID
        }
      ],
      searchParams: [
        {
          startDate: startDate,
          endDate: endDate,
          state: "No"
        }
      ]
    }
    $http.post('urlS', this, {
      code: '17033008',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          message.success('审核成功')
          this.priceAdjustReviewQuery()
        }
      }
    })
  };

  // 调价批量审核 -- 17033008
  priceAdjustHandleMass = () => {
    const {startDate, endDate, adjIDs} = this.state
    let data = {
      params: adjIDs,
      searchParams: [
        {
          startDate: startDate,
          endDate: endDate,
          state: "No"
        }
      ]
    }
    $http.post('urlS', this, {
      code: '17033008',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          message.success('审核成功')
          this.priceAdjustReviewQuery()
        }
      }
    })
  };

  // 调价取消审核 -- 17030001
  priceAdjustCancel = (adjID) => {
    const {startDate, endDate} = this.state
    let data = {
      params: [
        {
          adjID
        }
      ],
      searchParams: [
        {
          startDate: startDate,
          endDate: endDate,
          state: "No"
        }
      ]
    }
    $http.post('urlS', this, {
      code: '17033009',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          message.success('撤销成功')
          this.priceAdjustReviewQuery()
        }
      }
    })
  };

  // 调价批量取消审核 -- 17030001
  priceAdjustCancelMass = () => {
    const {startDate, endDate, adjIDs} = this.state
    let data = {
      params: adjIDs,
      searchParams: [
        {
          startDate: startDate,
          endDate: endDate,
          state: "No"
        }
      ]
    }
    $http.post('urlS', this, {
      code: '17033009',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          message.success('撤销成功')
          this.priceAdjustReviewQuery()
        }
      }
    })
  };

  // table复选项
  handleDetailSelectedRowKeysChange = (selectedRowKeys, selectedRows) => {
    let objArr = []
    let massArr = []
    objArr = selectedRows
    objArr.map(item => {
      massArr.push({adjID: item.adjID})
    })
    this.setState({
      detailSelectedRowKeys: selectedRowKeys,
      adjIDs: massArr
    })
  }

  // 行类名
  setRowClassNamePublic = (record) => {
    let rowStyle = ''
    let rowIdArr = []
    rowIdArr = this.state.adjIDs
    rowIdArr.map(item => {
      if (item.adjID === record.adjID) {
        rowStyle = 'clickRowStyle'
      }
    })
    return rowStyle
  }

  // 分页操作
  compilePage = (page, pageSize) => {
    this.setState({page, pageSize}, () => {
      //this.appointmentListQuery();
    })
  }
  stockAliasChange = (value) => {
    this.setState({stockAlias: value.target.value})
  }

  render() {
    const {total, tableData, columns, totalWidth, page, loading, startDate, endDate, detailSelectedRowKeys} = this.state;
    const options = [
      {label: '未审核', value: 'No'},
      {label: '已审核', value: 'Audit'}
    ];
    const detailRowSelection = {
      selectedRowKeys: detailSelectedRowKeys,
      onChange: this.handleDetailSelectedRowKeysChange
    };
    return (
      <div className="price-adjust-bill-review" style={{background: 'rgb(245,247,254)'}}>
        <Card title={<div><Icon type="minus-square" theme="twoTone" style={{marginRight: 5}}/>调价产品列表</div>}
              bordered={true} size='small'
              bodyStyle={{padding: 20}}
              style={{marginBottom: 10}}
        >
          <div className="gmaf-header flex-wrap-align-items">
            <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: 30}}>
              产品名称：
              <Input value={this.state.stockAlias} onChange={this.stockAliasChange} placeholder="请输入名称"
                     style={{width: 250}}/>
            </div>
            <div style={{marginRight: 30}}>
              审核状态：
              <Radio.Group options={options} onChange={this.onChange} value={this.state.status}/>
              {/*<Checkbox.Group options={options} onChange={this.onChange} />*/}
            </div>
            <div style={{marginRight: 30}}>
              <Button style={{width: 80, padding: '0px 5px'}} type="primary"
                      onClick={this.priceAdjustReviewQuery}>查询</Button>
            </div>
          </div>
        </Card>
        <Card title={<div><Icon type="minus-square" theme="twoTone" style={{marginRight: 5}}/>查询条件</div>}
              bordered={true} size='small'
              bodyStyle={{padding: 20}}
        >
          <div style={{marginTop: -10, marginBottom: 10}}>
            <Button type="link" style={{color: "black", padding: 0}} onClick={() => {
              this.priceAdjustHandleMass()
            }}>
              <Icon type="snippets" style={{color: "rgb(250, 140, 23)"}}/>批量审核
            </Button>
            <Divider type="vertical"/>
            <Button type="link" style={{color: "black", padding: 0}} onClick={() => {
              this.priceAdjustCancelMass()
            }}>
              <Icon type="undo" style={{color: "rgb(255, 61, 0)"}}/>批量撤销审核
            </Button>
          </div>
          <PubilcTablePagination
            param={{
              page,
              total,
              loading,
              data: tableData, // 表格数据
              x: totalWidth, // 表格的宽度
              y: store.getState().tableHeight.y - 50,
              componentName: 'PriceAdjustBillReviewList', // 待处理表头
              height: store.getState().tableHeight.y,
              columns: columns && columns.length > 0 ? columns : this.staticColumns, // 表头配置
            }}
            compilePage={this.compilePage}
            getColumns={this.getTableColumns}
            onRef={ref => this.commonTableRef = ref}
            rowSelectionPublic={detailRowSelection}
            setRowClassNamePublic={this.setRowClassNamePublic}
          />
        </Card>
      </div>
    );
  }
}

export default PriceAdjustBillReview;