/**
 * 事务页面。 根据一站式服务大厅的事务接口。达到增删发起事务流程
 * 2019年10月23日 09:48:13
 * 钟是志
 *
 * */

import { message } from 'antd';
import React, {  } from 'react';
import * as service from '../publicApiService';
import AuditPage from './AuditPage';
import * as destructionFunc from '../destruction';
import { Link } from 'dva/router';

export default class Index extends React.Component {
  constructor(props) {
    super(props);
    let pathname = this.props.location.pathname;
    this.state = {
      columns: [],
      dataList: [],
      headerInfo: [],
      workId: '',
      searchCondition: [],
      pathname,
      dataBaseId: '',
      addCondition: [],
      addFields: [], // 新增时填写的字段。
      tableInfo: {}, // 表格配置属性
      allConfigSetInfo: {}, // getFormDetail 返回的数据
    };
  };

  getFormDetail = (workId) => {
    const { dataBaseId } = this.state;
    service.getFormDetail(workId).then((response) => {
      if (typeof response.unifiedServicePatternModel === 'undefined') {
        return false;
      }
      destructionFunc.destructionGetDetail(response).then((x) => {
        const { addFields, tableInfo, allConfigSetInfo } = x;
        this.setState({
          addFields,
          tableInfo,
          allConfigSetInfo,
        },() => {
          this.giveDetailColumns();
        });
      });
    });
  };

  giveDetailColumns = () =>{
    const { columns, workId, dataBaseId, addFields, tableInfo } = this.state;
    columns.push(
      {
        dataIndex: 'operation',
        title: '操作',
        fixed: columns.length > 12 ? 'right' : false,
        render: (text, record) => {
          return ( <Link to={
            {
              pathname: "./Detail",
              state: {
                workId,
                dataBaseId,
                record,
                addFields,
                tableInfo
              },
            }}>
            详情
          </Link> );
        },
      },
    );
    this.setState({
      columns,
    })
  };

  getColumn = () => {
    const { workId } = this.state;
    service.getColumns(workId).then((response) => {
      if (response && response.length) {
        this.setState({
          columns: response, // 表头
        },()=>{
          this.getFormDetail(workId);
        });
      }
    });
  };

  componentDidMount() {
    const {  pathname } = this.state;
    service.getId(pathname).then((x)=>{
      this.setState({
        workId: x.workId,
        dataBaseId: x.dataBaseId,
      },()=>{
        this.getColumn();
      })
    });
  }

  render() {
    const { workId, dataBaseId, columns, searchCondition, addFields, allConfigSetInfo, tableInfo } = this.state;
    return (
      <AuditPage
        workId={workId}
        dataBaseId={dataBaseId}
        columns={columns}
        addFields={addFields}
        tableInfo={tableInfo}
        allConfigSetInfo={allConfigSetInfo}
        searchCondition={searchCondition}
      />
    );
  }
}