import { message, Checkbox, Radio } from "antd/lib/index";
import { getPropertyData, extendInclude, extendRule, getDataFieldValue, setDataFieldValue, initFilter, empty, guid } from "../../utils/common";
import { connect } from 'dva';
import moment from 'moment';
import config from "../../config.js"
import { Button, Row, Col, Form, Input, Select, Upload, Icon, Modal, Spin } from 'antd';
import storage from '../../utils/storage'
/**
 * @return {string}
 */
// 编辑  项目经费管理
export default {
  onBeforeSaveHandleRecord(params) {
    params.record.STATUS = params.customParams;
    params.record.TaskID = params.data.TaskID;
  },

  onBeforeEdit(params) {
    if (params.isNew != undefined && !params.isNew && params.stateData != undefined && params.stateData != null && params.stateData.PROJECT_ID != undefined && params.stateData.PROJECT_ID.length > 0) {
      params.setFieldEditable("Nav_Project.NAME", false)
    }
    const { stateData } = params ? params : {};
    if (stateData.Nav_Details != null && stateData.Nav_Details.length > 0) {
      //头部赋值
      var allTOTAL = 0
      var allUSE = 0
      var allLEFT = 0
      if (stateData.Nav_Details != null && stateData.Nav_Details.length > 0) {
        for (let i = 0; i < stateData.Nav_Details.length; i++) {
          allTOTAL += stateData.Nav_Details[i].TOTAL
          allUSE += stateData.Nav_Details[i].USE
          allLEFT += stateData.Nav_Details[i].LEFT
        }
      }
      // const temps = [];
      // temps.push({ field: 'ALLTOTAL', value: allTOTAL });
      // temps.push({ field: 'ALLUSE', value: allUSE });
      // temps.push({ field: 'ALLLEFT', value: allLEFT })
      // setFieldValueByBatch(temps);

      // 字表重写
      let details = [];
      stateData.Nav_Details.forEach(t => {
        let detail = {
          Files: [],
          Nav_Files: t.Nav_Files,
          ID: t.ID,
          MONEY_ID: t.MONEY_ID,
          NAME: t.NAME,
          TOTAL: t.TOTAL,
          USE: t.USE,
          LEFT: t.LEFT,
          ROW_NO: t.ROW_NO,
          ENTITY_ORG_TPYE: t.ENTITY_ORG_TPYE,
          ORG_ID: t.ORG_ID,
          CREATE_TIME: t.CREATE_TIME,
          IS_DELETED: t.IS_DELETED
        }
        if (t.Nav_Files) {
          t.Nav_Files.forEach(x => {
            //附件信息赋值给数据库对象，多个附件需要遍历附件信息
            detail.Files.push({
              uid: x.ID,
              name: x.name ? x.name : (x.Nav_ImgFile ? x.Nav_ImgFile.FILE_NAME : '未知文件'),
              status: x.status ? x.status : 'done',
              response: { Data: { imgFileID: x.IMG_FILE_ID }, file: x },
              url: x.Nav_ImgFile ? (config.picServerHost + x.Nav_ImgFile.Nav_File.FILE_PATH) : '',
            })
          })
        }
        details.push(detail);
      });
      details = details.map((item, i) => {
        return item;
      });
      stateData.Nav_Details = details;
    }
    stateData.ALLTOTAL = allTOTAL
    stateData.ALLUSE = allUSE
    stateData.ALLLEFT = allLEFT
    params['record'] = stateData;
  },

  // 金额修改 修改同行数据 和头部数据
  onSubChange: ({ row, setDataState, pageCode, record, colInfo, setFieldValue, dispatch, stateData, props }) => {
    if (colInfo.FIELD_NAME == "TOTAL" || colInfo.FIELD_NAME == "USE") {
      var allTotal = 0
      var allUse = 0
      var allLeft = 0

      for (let i = 0; i < stateData.Nav_Details.length; i++) {
        if (stateData.IS_DELETED)
          continue;
        if (record.ROW_NO == i + 1) {
          if (stateData.Nav_Details[i].TOTAL != undefined && stateData.Nav_Details[i].USE != undefined) {
            stateData.Nav_Details[i].LEFT = stateData.Nav_Details[i].TOTAL - stateData.Nav_Details[i].USE
          }
        }
        //专项经费
        if (stateData.Nav_Details[i].TOTAL != undefined) {
          allTotal += stateData.Nav_Details[i].TOTAL
        }
        //支出经费
        if (stateData.Nav_Details[i].USE != undefined) {
          allUse += stateData.Nav_Details[i].USE
        }
        //剩余经费
        if (stateData.Nav_Details[i].LEFT != undefined) {
          allLeft += stateData.Nav_Details[i].LEFT
        }

        //页面属性赋值
        stateData.ALLTOTAL = allTotal
        stateData.ALLUSE = allUse
        stateData.ALLLEFT = allLeft
      }
    }
  },

  handleRenderRowControl(params) {
    if (params.colConfig.field === 'Nav_Files') {
      const Tenant = storage('lacal').getItem('Tenant').val;
      const that = this;
      const uploadSopProps = {
        name: 'file',
        action: config.serviceHost('api/PF/File/UploadFile'),
        data: { OrgId: params.login.OrgId },
        fileList: params.record['Files'],
        headers: {
          Tenant: Tenant,
        },
        onChange(info) {
          params.record['Files'] = [...info.fileList];
          // params.saveRowRecord(params.record);
          if (info.file.status !== 'uploading') {

          }
          if (info.file.status === 'done') {
            let uploadFiles = [...info.fileList]
            if (uploadFiles) {
              let navFiles = [];
              uploadFiles.forEach(t => {
                if (t.response) {
                  //附件信息赋值给数据库对象，多个附件需要遍历附件信息
                  navFiles.push({
                    IMG_FILE_ID: t.response.Data.imgFileID,   //附件表文件名称
                    ID: t.response.file ? t.uid : guid()
                  })
                }
              });
              params.record['Nav_Files'] = navFiles;
            }
            message.success(`${info.file.name} 上传成功`);
          } else if (info.file.status === 'error') {
            message.error(`${info.file.name} 上传失败！`);
          }
          params.saveRowRecord(params.record);
        },
      };
      return <>
        <Upload {...uploadSopProps}>
          <Button>
            <Icon type="upload" /> 上传附件
          </Button>
        </Upload>
      </>
    }
  },

}
