<template>
  <div>
    <at-table
      @load="loadData"
      :data="data"
      :page-result="pageResult"
      ref="AtTable"
      :default-sorter="[{direction: 'DESC', property: 'portDate'}]"
      :dialogTitle="dialogTitle"
      :dialogVisible="dialogVisible"
      :cellClassNameFn="cellClassNameFn"
      @select-all="handleSelect"
      @select="handleSelect"
      @handleDialogCancel="handleDialogCancel"
      @handleDialogClose="handleDialogClose"
      @handleDialogSubmit="handleDialogSubmit"
      @row-dblclick="handleRowDBClick"
    >
      <template v-slot:searchInput>
        <el-form
          ref="searchForm"
          :model="searchForm"
          label-width="80px"
          class="search-top"
          @submit.native.prevent
        >
          <project-info-selector
            v-if="isShow"
            v-model="searchForm.projectId"
            @keyup.enter.native="handleSearch"
          ></project-info-selector>
          <!-- <dict-select
            v-model="searchForm.portType"
            dictCode="gcyb-tblx"
            validate="required"
            placeholder="请选择填报类型"
            :clearable="true"
            @keyup.enter.native="handleSearch"
          /> -->
          <el-date-picker
            v-model="searchForm.portDate"
            type="date"
            placeholder="选择审定日期"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            @keyup.enter.native="handleSearch"
          >
          </el-date-picker>

          <!-- <el-input
            v-model="searchForm.portUserName"
            placeholder="请输入经办人"
            clearable
            @keyup.enter.native="handleSearch"
          ></el-input> -->
          <el-button
            size="small"
            @click="handleSearch"
            type="primary"
            class="el-icon-search search-btn"
            >查询</el-button
          >
          <el-button
            size="small"
            @click="handleReset"
            type="primary"
            plain
            class="el-icon-refresh"
            >重置</el-button
          >
        </el-form>
      </template>
      <template v-slot:operationBtn>
        <el-button
          size="small"
          type="primary"
          @click="handleAdd"
          icon="el-icon-plus"
          >添加</el-button
        >
        <ht-delete-button
          :url="deleteUrl"
          :htTable="$refs.AtTable"
          :requestMethod="'POST'"
          >删除</ht-delete-button
        >
        <import-file :importData="importData" @load="load"></import-file>
        <el-button
          size="small"
          type="primary"
          @click="handleExport"
          icon="el-icon-download"
          plain
          style="margin-left: 10px"
          >导出</el-button
        >
      </template>
      <template>
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column type="index" label="序号" width="55" align="center">
        </el-table-column>
        <!-- <el-table-column
          prop="projectInfo.projectCode"
          label="项目代码"
          align="center"
          min-width="150"
          :show-overflow-tooltip="true"
        >
        </el-table-column> -->
        <el-table-column
          prop="projectInfo.projectName"
          label="项目名称"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="portDate"
          label="审定日期"
          align="center"
          min-width="100"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <!-- <el-table-column
          prop="portType"
          label="填报类型"
          align="center"
        >
          <template slot-scope="scope">
            {{ getDictName('gcyb-tblx', scope.row.portType) }}
          </template>
        </el-table-column> -->
        <!-- <el-table-column
          prop="portUnit"
          label="填报方"
          align="center"
          width="150"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{ getDictName('gcyb-tbf', scope.row.portUnit) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="portUserName"
          label="经办人"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="imageProgress"
          label="工程进度(%)"
          align="center"
          :show-overflow-tooltip="true"
          min-width="120"
        >
        </el-table-column> -->
        <el-table-column
          prop="progressDescription"
          label="审定支付金额(元)"
          align="center"
          min-width="150"
          :show-overflow-tooltip="true"
        >
          <template v-slot="{row}">
            <span v-if="row.progressDescription !== '----'">
              {{ moneyFormatter(row.progressDescription) }}
            </span>
            <span v-else>----</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="imageProgress"
          label="工程进度"
          align="center"
          :show-overflow-tooltip="true"
          min-width="120"
        >
          <template slot-scope="scope">
            <div>
              {{ scope.row.imageProgress }}%
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="engineeringChange"
          label="本月产值(元)"
          align="center"
          min-width="120"
          :show-overflow-tooltip="true"
        >
          <template v-slot="{row}">
            {{ moneyFormatter(row.engineeringChange) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="projectOvervie"
          label="累计总投资(元)"
          align="center"
          min-width="130"
          :show-overflow-tooltip="true"
        >
          <template v-slot="{row}">
            <span v-if="row.projectOvervie !== '----'">
              {{ moneyFormatter(row.projectOvervie) }}
            </span>
            <span v-else>----</span>
          </template>
        </el-table-column>
        <!-- <el-table-column
          prop="weeklyMouthlyWork"
          label="形象进度"
          align="center"
          min-width="100"
          :show-overflow-tooltip="true"
        >
        </el-table-column> -->
        <el-table-column
          prop="remarks"
          label="备注"
          align="center"
          :show-overflow-tooltip="true"
          width="120"
        >
        </el-table-column>
      </template>
      <!-- 弹框 -->
      <template v-slot:dialogContent>
        <el-form
          ref="ruleForm"
          v-form
          data-vv-scope="form"
          status-icon
          :model="form"
          :rules="rules"
          label-width="120px"
          v-if="dialogVisible"
          :label-position="labelPosition"
          v-loading.fullscreen="loading"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目名称" prop="projectInfo.projectName">
                <el-input
                  v-model="form.projectInfo.projectName"
                  placeholder="请选择项目名称"
                  @focus="handleFocusProject"
                  clearable
                  :disabled="!isShow"
                >
                  <el-button
                    type="primary"
                    @click="handleFocusProject"
                    slot="append"
                    icon="el-icon-search"
                  ></el-button>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="合同名称" prop="contractInfo.name">
                <el-input
                  v-model="form.contractInfo.name"
                  placeholder="请选择合同名称"
                  @focus="handleFocusContract"
                  clearable
                >
              <el-button
                type="primary"
                @click="handleFocusContract"
                slot="append"
                icon="el-icon-search"
              ></el-button>
            </el-input>
              </el-form-item>
            </el-col>
            
          </el-row>
          <el-row>
            <!-- <el-col :span="12">
              <el-form-item label="填报类型" prop="portType">
                <dict-select
                  dictCode="gcyb-tblx"
                  :hasDefaultOption="true"
                  clearable
                  placeholder="请选择填报类型"
                  v-model="form.portType"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col> -->
            <el-col :span="12">
              <el-form-item label="审定日期" prop="portDate">
                <el-date-picker
                  v-model="form.portDate"
                  type="date"
                  placeholder="请输入审定日期"
                  class="basisType"
                  value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="审定支付金额" prop="progressDescription">
                <el-input
                  v-model="form.progressDescription"
                  placeholder="请输入审定支付金额"
                  type="number"
                  @input="handleInput"
                  clearable
                >
                  <i slot="suffix">元</i>
                </el-input>
              </el-form-item>
              <!--  <el-form-item label="经办人" prop="portUserName">
               <eip-user-selector
                  :appendToBody="true"
                  :placeholder="'请选择用户'"
                  ref="eipSelector"
                  v-model="form.portUserName"
                  single
                  permission="b"
                  :config="{
                    id: 'form.portUserId',
                    name: 'form.portUserName',
                  }"
                /> 
                <el-input
                  v-model="form.portUserName"
                  placeholder="请输入经办人"
                  clearable
                >
                </el-input>
              </el-form-item>-->
            </el-col>
            
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="工程进度" prop="imageProgress">
                <el-input
                  placeholder="自动计算"
                  v-model="form.imageProgress"
                  clearable
                >
                  <!-- <i slot="suffix">%</i> -->
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="累计总投资" prop="projectOvervie">
                <el-input
                  placeholder="自动计算"
                  v-model="form.projectOvervie"
                  clearable
                >
                  <i slot="suffix">元</i>
                </el-input>
              </el-form-item>
            </el-col>
            
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="投资计算比例" prop="investmentRatio">
                <dict-select
                  dictCode="gcyb-tzjsbl"
                  :hasDefaultOption="true"
                  clearable
                  placeholder="请选择投资计算比例"
                  v-model="form.investmentRatio"
                  @input="handleInvestmentRatio"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="本月产值" prop="engineeringChange">
                <el-input
                  placeholder="自动计算"
                  v-model="form.engineeringChange"
                  clearable
                >
                  <i slot="suffix">元</i>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-form-item label="本月计划" prop="weeklyMouthlyWork">
            <el-input
              type="textarea"
              v-model="form.weeklyMouthlyWork"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="本月完成" prop="projectOvervie">
            <el-input
              type="textarea"
              v-model="form.projectOvervie"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
           <el-form-item label="存在问题" prop="engineeringChange">
            <el-input
              type="textarea"
              v-model="form.engineeringChange"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
         -->
          <el-row>
            <el-col :span="24">
              <el-form-item label="形象进度" prop="weeklyMouthlyWork">
                <el-input
                  type="textarea"
                  row="2"
                  v-model="form.weeklyMouthlyWork"
                  placeholder="请输入形象进度"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="备注" prop="remarks">
            <el-input
              type="textarea"
              v-model="form.remarks"
              placeholder="请输入备注"
              :rows="2"
            ></el-input>
          </el-form-item>
          <file-table
            @handleFile="accessoryInfo"
            :file="form.accessoryInfo"
            :info="'上传工程月报文件'"
          />
        </el-form>
      </template>
      <template v-slot:showTotalData>
        <span class="show-slected">
          已选中<i>{{ selected.numbers }}</i>
          条数据， 产值金额合计
          <i>{{ moneyFormatter(selected.money) }} </i>元
        </span>
      </template>
      <project-info-dialog
        ref="project"
        :single="true"
        @select="handleSelectProject"
      ></project-info-dialog>
      <project-contract-dialog
      ref="contract"
      :single="true"
      @select="handleSelectContract"
    ></project-contract-dialog>
    </at-table>
  </div>
</template>
<script>
import ProjectInfoDialog from '@/components/dialog/ProjectInfoDialog.vue'
import ProjectContractDialog from '@/components/dialog/ProjectContractDialog.vue'
import req from '@/request.js'
import utils from '@/utils.js'
const BigNumber = require('bignumber.js')
export default {
  name: 'MonthlyEngineeringReport',
  components: {
    ProjectInfoDialog,
    ProjectContractDialog
  },
  props: {
    pid: {
      type: String,
      default: '',
    },
    isShow: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      total: {
        projectInfo: {
          projectName: '合计',
        },
        portDate: '----',
        progressDescription: '----',
        imageProgress: '----',
        engineeringChange: 0,
        projectOvervie: '----',
        remarks: '----',
      },
      selected: {
        numbers: 0,
        money: '0',
      },
      importData: {
        title: '工程月报导入',
        templateName: '工程月报模板.xls',
        downloadUrl: '/../fvue/static/excel/MonthlyEngineeringReport.xlsx',
        importUrl:
          window.context.cqxy +
          '/biz/processManagement/progressManageReport/v1/importExcelData',
        showProject: true,
        projectId: '',
        showContract:true
        // params: {
        //   portDate: '',
        //   portType: '',
        //   portUnit: '',
        //   // portUserId: '',
        //   portUserName: '',
        // },
      },
      loading: false,
      projectObj: [], //获取的所有项目数组
      labelPosition: 'right', //表单对齐方式
      searchForm: {
        projectId: '',
        portDate: '',
        portType: '',
        portUserName: '',
      }, //顶部搜索参数
      data: [], //表格数据
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0,
      },
      form: {
        portUserId: '',
        portUserName: '',
        imageProgress: 0,
        investmentRatio: '0.75',
        engineeringChange: 0,
        projectOvervie: 0,
        projectInfo: {
          projectName: '',
          projectCode: '',
        },
        contractInfo: {
          name: '',
          id: '',
        },
        accessoryInfo: [],
      },
      projectMoney: '',
      totalMoney: '',
      dialogVisible: false,
      dialogTitle: '添加',
      //表单验证
      rules: {
        'projectInfo.projectName': {
          required: true,
          message: '项目名称不能为空',
          trigger: 'blur',
        },
        'contractInfo.name': {
          required: true,
          message: '请选择合同',
          trigger: 'blur',
        },
        // 'projectInfo.projectCode': {
        //   required: true,
        //   message: '项目代码不能为空',
        //   trigger: 'blur',
        // },
        portDate: {
          required: true,
          message: '审定日期不能为空',
          trigger: 'blur',
        },
        progressDescription: {
          required: true,
          message: '审定支付金额不能为空',
          trigger: 'blur',
        },
        // portUnit: {
        //   required: true,
        //   message: '填报方不能为空',
        //   trigger: 'blur',
        // },
        // portUserName: {
        //   required: true,
        //   message: '经办人不能为空',
        //   trigger: 'blur',
        // },
      },
    }
  },
  computed: {
    deleteUrl: function () {
      return (
        window.context.cqxy +
        '/biz/processManagement/progressManageReport/v1/delete'
      )
    },
    //计算工程进度的值
    // "form.imageProgress": {
    //   get:function() {
    //     let num =
    //       Math.round(
    //         (this.form.progressDescription / this.projectMoney) * 100
    //      ) / 100
    //     return Number.isNaN(num) ? 0 : num == 'Infinity' ? 100 : num
    //   },
    //   set: function(newValue) {
    //     this.form.imageProgress = newValue + '%'
    //   }
    // },
    //计算累计总投资的值
    // 'form.projectOvervie': {
    //   get: () => {
    //     let num =
    //       Number(this.form.progressDescription) + Number(this.totalMoney)
    //     return Number.isNaN(num) ? 0 : num !== 'undefined' ? num : 0
    //   },
    //   set: newValue => {
    //     this.form.projectOvervie = newValue
    //   }
    // },
    // //计算本月产值的值
    // 'form.engineeringChange': {
    //   get: () => {
    //     let num = Math.round((this.form.progressDescription / 75) * 100) / 100
    //     return Number.isNaN(num) ? 0 : num
    //   },
    //   set: newValue => {
    //     this.form.engineeringChange = newValue + '%'
    //   }
    // }
  },
  watch: {
    // 'form.progressDescription': function(newValue, oldValue) {
    //   newValue = newValue ? newValue : 0
    //   let num1 = FormMath.toNumber(newValue)
    //   let num2 = FormMath.toNumber(this.projectMoney) * 10000
    //   num2 = num2 == 0 ? 1 : num2 //解决可能出现的inifity,例2/0 = inifity
    //   let num3 = FormMath.toNumber(this.totalMoney)
    //   this.form.imageProgress = FormMath.tofixed((num1 / num2) * 100, 2)
    //   let ary = [num1, num3]
    //   this.form.projectOvervie = FormMath.tofixed(FormMath.sum(ary), 2)
    //   this.form.engineeringChange = FormMath.tofixed(num1 / 0.75, 2)
    // },
    projectMoney: function (newValue, oldValue) {
      newValue = newValue == 0 ? 1 : newValue
      let num1 = FormMath.toNumber(this.form.progressDescription)
      num1 = num1 ? num1 : 0
      let num2 = FormMath.toNumber(newValue) * 10000
      let num3 = FormMath.toNumber(this.totalMoney)
      // this.form.imageProgress = FormMath.tofixed((num1 / num2) * 100, 2)
      // let ary = [num1, num3]
      // this.form.projectOvervie = FormMath.tofixed(FormMath.sum(ary), 2)
      this.form.projectOvervie = num3
      // this.form.engineeringChange = FormMath.tofixed(num1 / 0.75, 2)
      let num4 = this.form.investmentRatio
      this.form.engineeringChange = FormMath.tofixed(num1 / num4, 2)
    },
    totalMoney: function (newValue, oldValue) {
      let num1 = FormMath.toNumber(this.form.progressDescription)
      num1 = num1 ? num1 : 0
      let num2 = FormMath.toNumber(this.projectMoney) * 10000
      num2 = num2 == 0 ? 1 : num2
      let num3 = FormMath.toNumber(newValue)
      // this.form.imageProgress = FormMath.tofixed((num1 / num2) * 100, 2)
      // let ary = [num1, num3]
      // this.form.projectOvervie = FormMath.tofixed(FormMath.sum(ary), 2)
      this.form.projectOvervie = num3
      let num4 = this.form.investmentRatio
      this.form.engineeringChange = FormMath.tofixed(num1 / num4, 2)
    },
  },
  mounted() {
    this.initProjectAll()
  },

  methods: {
    //获取投资总和
    gettotalMoney(projectId) {
      let params = {
        projectId: projectId,
        // registerDate: this.getDate(),
        registerDate: this.form.portDate,
      }
      this.$http
        .post(
          '${cqxy}/biz/processManagement/progressManageReport/v1/getInvestmentTotal',
          params
        )
        .then((res) => {
          this.totalMoney = res.data.value
        })
    },
    //重置
    handleReset() {
      this.searchForm.projectId = ''
      this.searchForm.portDate = ''
      this.searchForm.portType = ''
      this.searchForm.portUserName = ''
      this.$refs['searchForm'].resetFields()
      this.pageResult = {
        page: 1,
        pageSize: 20,
        total: 0,
      }
      this.$refs['searchForm'].resetFields()
      this.$nextTick(() => {
        this.$refs.AtTable.load()
      })
    },
    handleSelectProject(data) {
      this.form.portUserName = data.projectManager
      this.form.projectInfo.projectName = data.projectName
      this.form.projectInfo.projectCode = data.projectCode
      this.form.projectId = data.id
      this.gettotalMoney(this.form.projectId)
      this.projectMoney = data.projectMoney
      this.$refs.ruleForm.clearValidate()
    },
    //打开选择项目弹框
    handleFocusProject() {
      this.$refs.project.open()
    },
    //导入后重新加载数据
    load() {
      this.$refs.AtTable.load()
    },
    //导出
    handleExport() {
      let query = []
      let search = {
        'pm.ID_': this.searchForm.projectId,
        port_type_: this.searchForm.portType,
        port_date_: this.searchForm.portDate,
      }
      if (!this.isShow) {
        search['pm.ID_'] = this.pid
      }
      for (let i in search) {
        if (search[i]) {
          query.push({
            group: 'advance',
            operation: 'LIKE',
            property: i,
            relation: 'AND',
            value: search[i],
          })
        }
      }
      let param = {
        pageBean: {
          page: 1,
          pageSize: -1,
          showTotal: true,
        },

        querys: query,
        sorter: [],
      }
      req
        .downloadPostCopy(
          '${cqxy}/biz/processManagement/progressManageReport/v1/exportDataToExcel',
          param
        )
        .then((res) => {
          if (res.status == 200) {
            this.$message.success('导出成功')
          } else {
            this.$message.error('导出失败')
          }
        })
    },
    //获取附件信息
    accessoryInfo(info) {
      this.form.accessoryInfo = info
    },
    initProjectAll() {
      //初始获取所有项目数据
      if (this.isShow) {
        this.$http.post('${cqxy}/biz/projectManagement/pm/v1/All').then(
          (res) => {
            this.projectObj = res.data.value
          },
          (error) => {
            reject(error)
          }
        )
      } else {
        let params = {
          pageBean: {
            page: 1,
            pageSize: 500,
            total: 0,
          },
          querys: [
            {
              group: 'main',
              operation: 'EQUAL',
              property: 'id',
              relation: 'AND',
              value: this.pid,
            },
          ],
        }
        this.$http
          .post('${cqxy}/biz/projectManagement/pm/v1/page', params)
          .then((res) => {
            if (res.data.state) {
              let resp = res.data.value
              this.projectObj = resp.rows
            }
          })
      }
    },
    //查询
    handleSearch() {
      this.$refs.AtTable.load()
    },
    afterSaveData() {
      setTimeout(() => {
        this.form = {
          projectId: '',
          portDate: '',
          portType: '',
          // portUserId: '',
          portUserName: '',
          projectInfo: {
            projectName: '',
            projectCode: '',
          },
          accessoryInfo: [],
        }
        this.dialogVisible = false
        this.$refs.AtTable.load()
      }, 500)
    },
    handleAdd() {
      this.isEdit = false
      this.dialogTitle = '添加'
      this.dialogVisible = true
      let date = new Date()
      // let currentUser = JSON.parse(window.sessionStorage.currentUser)
      this.form = {
        projectId: '',
        portDate: utils.formatDateYear(date),
        portType: 2,
        // portUserId: currentUser.userId,
        // portUserName: currentUser.username,
        portUserName: '',
        imageProgress: 0,
        investmentRatio: '0.75',
        engineeringChange: 0,
        projectOvervie: 0,
        progressDescription:'0',
        contractId:'',
        contractName:'',
        projectInfo: {
          projectName: '',
          projectCode: '',
        },
        contractInfo: {
          name: '',
          id: '',
        },
        accessoryInfo: [],
      }
      if (this.isShow) {
        let selectedProject = this.projectObj.find((item) => {
          if (item.currentProject && item.currentProject == 1) {
            return item.currentProject == 1
          }
        })
        this.form.portUserName = selectedProject.portUserName
        this.form.projectInfo.projectName = selectedProject.projectName
        this.form.projectInfo.projectCode = selectedProject.projectCode
        this.form.projectId = selectedProject.id
        this.form.portUserName = selectedProject.projectManager
        this.projectMoney = selectedProject.projectMoney
        this.gettotalMoney(this.form.projectId)
      } else {
        //根据项目id查询
        this.form.portUserName = this.projectObj[0].portUserName
        this.form.projectInfo.projectName = this.projectObj[0].projectName
        this.form.projectInfo.projectCode = this.projectObj[0].projectCode
        this.form.projectId = this.projectObj[0].id
        this.form.portUserName = this.projectObj[0].projectManager
        this.projectMoney = this.projectObj[0].projectMoney
        this.gettotalMoney(this.form.projectId)
      }
    },
    handleRowDBClick(data) {
      this.isEdit = true
      if (this.isShow) {
        let selectedProject = this.projectObj.find((item) => {
          if (item.currentProject && item.currentProject == 1) {
            return item.currentProject == 1
          }
        })
        this.form.portUserName = selectedProject.portUserName
        this.form.projectInfo.projectName = selectedProject.projectName
        this.form.projectInfo.projectCode = selectedProject.projectCode
        this.form.projectId = selectedProject.id
        this.form.portUserName = selectedProject.projectManager
        this.projectMoney = selectedProject.projectMoney
        this.gettotalMoney(this.form.projectId)
      } else {
        //根据项目id查询
        this.form.portUserName = this.projectObj[0].portUserName
        this.form.projectInfo.projectName = this.projectObj[0].projectName
        this.form.projectInfo.projectCode = this.projectObj[0].projectCode
        this.form.projectId = this.projectObj[0].id
        this.form.portUserName = this.projectObj[0].projectManager
        this.projectMoney = this.projectObj[0].projectMoney
        this.gettotalMoney(this.form.projectId)
      }
      this.dialogTitle = '修改'
      this.$http
        .post(
          '${cqxy}/biz/processManagement/progressManageReport/v1/detail/' +
            data.id
        )
        .then((res) => {
          this.dialogVisible = true
          let response = res.data.value
          if (!response.contractInfo) {
            let obj = {
              contractInfo: {
                name:'',
                id:''
              },
            }
            this.form = {...response, ...obj}
          } else {
            this.form = response
          }
          if( this.form.contractInfo.contractAmount){
            this.form.totalInvestment = this.form.contractInfo.contractAmount
          }
          if( this.form.contractInfo.name ==undefined){
            this.form.contractInfo.name =''
          }
          console.log(this.form);
        })
    },

    handleSelect(selection) {
      selection.forEach((item, index) => {
        if (item.projectInfo.projectName === '合计') {
          selection.splice(index, 1)
        }
      })
      this.selected.numbers = selection.length
      let total = this.calculationTotal(selection, 'engineeringChange')
      this.selected.money = total
    },
    //打开合同信息
    handleFocusContract() {
      this.$refs.contract.open(this.form.projectId)
    },
    //选择合同信息
    handleSelectContract(data) {
      // console.log(data,'data')
      this.form.totalInvestment = data.contractAmount

      if (!data.payee) {
        this.form.registerPersonName = data.payeeAccountPhone
      } else {
        this.form.registerPersonName = data.payee
      }
      if(this.form.progressDescription == ''){
        this.form.progressDescription = '0'
      }
      
      this.form.contractId = data.id
      this.form.contractName =data.name
      this.form.contractInfo.name = data.name
      this.form.contractInfo.id = data.id
      this.form.handlerPerson = data.payeeAccount
      this.form.registerPersonId = data.payeeContactPhone

      let params = {
        projectId: this.form.projectId,
        registerDate: this.form.portDate,
        contractId: this.form.contractId,
      }
      this.$http
        .post(
          '${cqxy}/biz/processManagement/progressManageReport/v1/getInvestmentTotal',
          params
        )
        .then((res) => {
          if (res.data.state) {
            this.totalMoney = res.data.value
            this.form.projectOvervie = res.data.value
            this.form.imageProgress = (FormMath.toNumber((this.form.projectOvervie) +FormMath.toNumber(this.form.progressDescription))/(FormMath.toNumber(this.form.totalInvestment)*100)).toFixed(2)
          }
        })
        delete this.$refs.ruleForm.contractInfo.name
        this.$nextTick(()=>{
          this.$refs.ruleForm.clearValidate('contractInfo.name')
        })
    },
    //取消
    handleDialogCancel() {
      this.dialogVisible = false
      this.form = {
        projectId: '',
        portDate: '',
        portType: '',
        portUserId: '',
        investmentRatio: '0.75',
        portUserName: '',
        projectInfo: {
          projectName: '',
          projectCode: '',
        },
        contractInfo:{
          name:'',
          id:''
        },
        accessoryInfo: [],
      }
    },
    //关闭窗口
    handleDialogClose(val) {
      this.dialogVisible = false
      this.form = {
        projectId: '',
        portDate: '',
        portType: '',
        portUserId: '',
        portUserName: '',
        projectInfo: {
          projectName: '',
          projectCode: '',
        },
        accessoryInfo: [],
      }
    },
    //提交保存
    handleDialogSubmit(formName) {
      console.log(this.form);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.accessoryInfo && this.form.accessoryInfo.length > 0) {
            this.loading = true
            this.form.decisionBasisType = this.form.decisionBasisType + ''
            this.form.accessoryInfo.forEach((item) => {
              if (item.id) {
                delete item.id
              }
            })
          }
          this.form.imageProgress = this.form.imageProgress + '%'
          this.$http
            .post(
              '${cqxy}/biz/processManagement/progressManageReport/v1/save',
              this.form
            )
            .then((res) => {
              if (res.status == 200) {
                setTimeout(() => {
                  this.$message.success(res.data.message)
                  this.afterSaveData()
                }, 500)
              } else {
                this.$message.error(res.data.message)
              }
              this.loading = false
            })
        }
        // else if (this.form.accessoryInfo.length === 0) {
        //   this.$message.error('至少上传一个附件')
        // }
        else {
          this.$message({
            message: '*号必填项,不能为空!',
            type: 'warning',
          })
          return false
        }
      })
    },
    loadData(param, cb) {
      let query = []
      let search = {
        'pm.ID_': this.searchForm.projectId,
        port_type_: this.searchForm.portType,
        port_date_: this.searchForm.portDate,
      }
      if (!this.isShow) {
        search['pm.ID_'] = this.pid
        this.importData.projectId = this.pid
      }
      for (let i in search) {
        if (search[i]) {
          query.push({
            group: 'advance',
            operation: 'LIKE',
            property: i,
            relation: 'AND',
            value: search[i],
          })
        }
      }
      if (param.querys && param.querys.length) {
        param.querys.push(...query)
      } else {
        param.querys = query
      }
      this.$http
        .post(
          '${cqxy}/biz/processManagement/progressManageReport/v1/page',
          param
        )
        .then(
          (resp) => {
            let response = resp.data.value
            this.selected = {
              numbers: 0,
              money: '0',
            }
            let data = response.rows
            let total = this.calculationTotal(data, 'engineeringChange')
            this.total.engineeringChange = total
            if (data.length !== 0) {
              data.push(this.total)
            }
            this.data = data
            this.pageResult = {
              page: response.page,
              pageSize: response.pageSize,
              total: response.total,
            }
          },
          (error) => {
            reject(error)
          }
        )
        .finally(() => {
          if (cb) {
            cb()
          }
        })
    },
    handleInput(val) {
      val = val ? val : 0
      let num1 = (FormMath.toNumber(val)+ FormMath.toNumber(this.form.projectOvervie))
      let num2 = FormMath.toNumber(this.projectMoney) * 10000
      num2 = num2 == 0 ? 1 : num2 //解决可能出现的inifity,例2/0 = inifity
      let num3 = FormMath.toNumber(this.totalMoney)
      this.form.imageProgress = FormMath.tofixed((num1 / this.form.totalInvestment) * 100, 2)
      // let ary = [num1, num3]
      // this.form.projectOvervie = FormMath.tofixed(FormMath.sum(ary), 2)
      let num5 =  FormMath.toNumber(this.form.progressDescription)
      this.form.projectOvervie = num3
      let num4 = this.form.investmentRatio
      this.form.engineeringChange = FormMath.tofixed(num5 / num4, 2)
      delete this.$refs.ruleForm.progressDescription
      this.$nextTick(()=>{
          this.$refs.ruleForm.clearValidate('progressDescription')
          delete this.$refs.ruleForm.progressDescription
        })
    },
    handleInvestmentRatio(val) {
      let num1 = FormMath.toNumber(this.form.progressDescription)
      num1 = num1 ? num1 : 0
      this.form.engineeringChange = FormMath.tofixed(num1 / val, 2)
    },
    // 计算列表合同金额合计金额
    calculationTotal(data, key) {
      let total = new BigNumber(0)
      if (key && data && data.length !== 0) {
        data.forEach((item) => {
          if (item[key]) {
            total = total.plus(item[key])
          } else {
            total = total.plus(0)
          }
        })
      }
      return this.moneyFormatter(total)
    },
    cellClassNameFn(row) {
      // 列表项目名称为【合计】隐藏复选框
      if (row.row.projectInfo.projectName === '合计') {
        return 'table-column-hidden'
      }
    },
  },
}
</script>
<style lang="scss" scoped>
@import '@/assets/css/common.scss';
.decision-type {
  display: flex;
  justify-content: center;
  align-items: center;
}
/deep/ .el-input__suffix > .el-input__icon {
  display: none;
}
.show-slected {
  font-size: 18px;
  i {
    font-style: normal;
    font-weight: 700;
    margin: 0 5px;
    color: #f40;
  }
}
/deep/ .table-column-hidden .el-checkbox__input {
  display: none;
}
/deep/ .table-column-hidden .cell > div {
  display: none;
}
</style>
