import { Button, Descriptions,Popconfirm, Row, Col, Form,Modal, Input, Select, Table,Progress } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude,extendIgnoreDataRule, setDataFieldValue, guid, initQueryFilter } from "../../../utils/common";
import ReactToPrint from "react-to-print";
import config from "../../../config";
import XLSX from 'xlsx';
import { connect } from 'dva';
import moment from 'moment';
import { message } from "antd/lib/index";
import FormPage from '../../FormPage'
import stylesStep from '../HI/StepForm.css';
class PT014ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            display:'none',
            BtnAgreeDisplay: 'none',
            tmpData: {},
        };
    };

    componentDidMount() {
        if (this.props.data?.id)
            this.loadData(this.props.data?.id);
    }

    componentWillReceiveProps(NextProps) {
        if (NextProps.data?.id && this.props.data?.id != NextProps.data?.id) {
            this.loadData(NextProps.data?.id);
        }
    }

    onTableBtnAgree() {
        const { data } = this.props
        const json = initFilter(this.props.login.OrgId)
        extendRule(json, 'ID', 1, data.id)
        extendRule(json, 'TASK_ID', 1, data.TaskID)
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'PT/PTSafetyPolicySurvey/IdentityUpdate',
            payload: json,
            onComplete: (ret) => {
                if (ret) {
                    message.success('确认成功！');
                    this.setState({ BtnAgreeDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }

    BtnClose = () => {
        if (typeof this.props.data.onCancel != "undefined"&&typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }

    loadData = (dataId) => {
        let that=this;
        let json = initFilter(that.props.login.OrgId);
        extendRule(json, 'ID', 1, dataId);
        extendIgnoreDataRule(json)
        // extendInclude(json, 'Nav_User')
        // extendInclude(json, 'Nav_User.Nav_Person')
        // extendInclude(json, 'Nav_User.Nav_Person.Nav_Post')
        // extendInclude(json, 'Nav_Department')
        // extendInclude(json, 'Nav_Department.Nav_User')
        // extendInclude(json, 'Nav_DepartmentDetail')
        // extendInclude(json, 'Nav_DepartmentDetail.Nav_Department')
        // extendInclude(json, 'Nav_DepartmentDetail.Nav_User')
        // extendInclude(json, 'Nav_DepartmentDetail.Nav_DetailContent')
       that.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'PT/PTSafetyPolicySurvey/GetCollectEdit',
            onComplete: (ret) => {
                if (ret) {
                    if(that.props.data && that.props.data.loadDataDeal)
                    {
                        that.props.data.loadDataDeal(1);
                    }
                    that.setState({ data: ret });
                    if (that.props.data.tableKey==="2" ||that.props.data.tableKey===undefined) {
                        that.setState({ BtnAgreeDisplay: 'none' })
                    }else{
                        that.setState({ BtnAgreeDisplay: 'inline' })
                    }
                    this.onClickApprove(false);
                    //归档才显示打印和导出按钮
                    // if(ret.PRE_MEETING_STATUS==5){
                    //     this.setState({ isFinished: 'inline' })
                    // }
                }
                else
                { message.error('请先尝试刷新，若仍然加载失败，请联系管理员排查!');}
            }
        });
    }

    onTableBtnExport() {
        const enums=this.props.data.enums ? this.props.data.enums :this.props.app.enums ;
        let TableWrap = document.getElementById('tableId' + this.props.data.id);
        let Table = TableWrap.getElementsByTagName('table')[0];
        const wb = XLSX.utils.table_to_book(Table);
        XLSX.writeFile(wb, "生产方针征集表.xlsx")
    }

    onClickApprove=(appVisible)=>{
        const newtmpData = {
            data: { id: this.props.data.id, isShow: true,key:guid(),param:this.props.data.param,appVisible:appVisible,BtnAgreeDisplay:'inline'},
            formCode: "PF123",
        }
        this.setState({
            tmpData: newtmpData,
        });
    }
    onClickCollect =(id,guidId,otherSuggestions) =>{
        const json = initFilter(this.props.login.OrgId, guidId,'',0,1,otherSuggestions)
          extendRule(json, 'ID', 1, id)
        //   extendRule(json, 'OTHER_SUGGESTIONS', 1, otherSuggestions)
          this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'PT/PTSafetyPolicySurvey/GetCollectEdit',
            payload: json,
            onComplete: (ret) => {
              if (ret) {
                this.setState({
                  data: ret,
                  display:'block'
                })
              }
            }
          })
      }
    render() {
        const { data } = this.state;
        const enums=this.props.data.enums ? this.props.data.enums :this.props.app.enums ;
        // let aaa = Math.random();
        return <div>
            <div style={{ padding: '10px' }}>
            <Button onClick={() => this.onClickApprove(true)} style={{ marginRight: '8px'}} icon="export">审批详情</Button>
                <ReactToPrint
                    trigger={() => <Button style={{ marginLeft: '8px', display: data && data.IS_PUBLISH === 5?"inline":"none" }} type={'default'} icon={'printer'} >打印</Button>}
                    content={() => this.componentRef}
                />
                <Button onClick={() => this.onTableBtnExport()} icon="export" style={{ marginLeft: '8px', display: data && data.IS_PUBLISH === 5?"inline":"none" }}>导出</Button>
                <Button type="primary" style={{marginLeft: '8px', display: this.state.BtnAgreeDisplay }} onClick={() => this.onTableBtnAgree()} icon="check" >已阅</Button>
            </div>
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px' }} id={'tableId' + this.props.data.id}>
                <h1 style={{ textAlign: 'center', margin: '15px' }}>生产方针征集表</h1>
                {
                     data ?
                     <div>
                        <Descriptions size="middle" bordered className={stylesStep.description}>
                            <Descriptions.Item label="发起时间">{data.CREATE_TIME}</Descriptions.Item>
                            <Descriptions.Item label="发起部门">{data.Nav_Department ? data.Nav_Department.NAME : ''}</Descriptions.Item>
                            <Descriptions.Item label="发起人员">{data.Nav_User ? data.Nav_User.NAME : ''}</Descriptions.Item>
                            <Descriptions.Item label="年度">{data.YEAR}</Descriptions.Item>
                            <Descriptions.Item label="征集截至时间">{data.COMPLETE_DATE}</Descriptions.Item>
                            <Descriptions.Item label="征集要求">{
                                data.Nav_Collects && data.Nav_Collects.map((item, i) => {
                                    return <tr><label> {item.Nav_Collect?.NAME}</label></tr>
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item label="意向方针">{data.Nav_Guidelines?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="满意度"> <Progress percent={data.RATE } status="active" /></Descriptions.Item>
                            <Descriptions.Item label="征集人数">{data.ALL_USER_COUNT}</Descriptions.Item>
                            <Descriptions.Item label="参与人数">{data.USER_COUNT}</Descriptions.Item>
                            <Descriptions.Item label="参与率">{data.USER_RATE}%</Descriptions.Item>
                        </Descriptions>
                     </div>:null
                }
                 
                 {/* {
                    data ?  <table style={{ width: '100%', textAlign: 'left'}} className={stylesStep.PrintForm1}>
                    <tr>
                    <td  style={{width:"150px"}} className={stylesStep.fontBold}>序号</td>
                    <td className={stylesStep.fontBold}>意向方针</td>
                    <td className={stylesStep.fontBold}>满意度</td>
                    </tr>
                    {
                                   data.IntentionalGuids && data.IntentionalGuids.map((item,i)=>{
                                        return <tr>
                                            <td>
                                                {i+1}
                                            </td>
                                             <td>
                                                {
                                                  item.GUIDELINES_NAME 
                                                }
                                            </td>
                                            <td>
                                              <Progress percent={item.RATE } status="active" onClick={() => this.onClickCollect(this.state.data.ID,item.GUIDELINES_ID,item.GUIDELINES_NAME)} />
                                            </td>
                                        </tr>
                                    })
                                }
                    </table>:null 
                } */}
                {/* display:this.state.display */}
                 <table style={{tableLayout:'fixed',width:'100%'}} className={stylesStep.PrintForm1}>
                    <tr>
                    <td style={{width:"150px"}}className={stylesStep.fontBold}>序号</td>
                    {/* <td width='10%' className={stylesStep.fontBold}>意向方针</td> */}
                    <td width='10%' className={stylesStep.fontBold}>填写人</td>
                    <td width='10%' className={stylesStep.fontBold}>填写人部门</td>
                    <td width='10%' className={stylesStep.fontBold}>填写时间</td>
                    <td width='10%' className={stylesStep.fontBold}>是否满意</td>
                    <td className={stylesStep.fontBold}>其他意见</td>
                    </tr>
                    {
                                   data?.Nav_Feedbacks && data.Nav_Feedbacks?.map((item,i)=>{
                                        return <tr>
                                             <td>
                                                {i+1}
                                            </td>
                                            {/* <td>
                                                {
                                                  item.Nav_Guidelines?.NAME
                                                }
                                            </td> */}
                                           
                                             <td>
                                                {
                                                  item.Nav_User?.NAME 
                                                }
                                            </td>
                                            <td>
                                                {
                                                  item.Nav_Department?.NAME
                                                }
                                            </td>
                                            <td>
                                                {
                                                  item.CREATE_TIME
                                                }
                                            </td>
                                            <td>
                                                {
                                                  item.IS_AGREE==1?"是":"否"
                                                }
                                            </td>
                                            <td>
                                                {
                                                  item.OTHER_SUGGESTIONS
                                                }
                                            </td>
                                        </tr>
                                    })
                                }
                    </table>
                <FormPage {...this.state.tmpData} />
            </div>
        </div>
    }

}

export default connect(({ login,app }) => ({ login,app }))(PT014ShowPrint) 