<template>
  <div class="detailWrapper">
    <section class="section1 public-flex">
      <div class="public-margin-r-20">
        <h4>
          投资项目
          <span class="alreadyStop" v-if="detailInfo.delFlag === '1'"
            >已终止</span
          >
        </h4>
        <p class="projectNameTit">{{ detailInfo.projectName }}</p>
      </div>
    </section>
    <el-divider></el-divider>
    <section>
      <!-- :space="200" -->
      <el-divider></el-divider>
      <div
        class="public-flex-between basicsInfo"
        v-if="detailInfo.projectState != '1'"
      >
        <div class="public-flex-center-column">
          <div>项目公司名称</div>
          <p
            style="width: 250px; text-align: center"
            class="yichu1"
            v-if="
              detailInfo.tProjectCompany &&
              detailInfo.tProjectCompany.companyName
            "
          >
            {{ detailInfo.tProjectCompany.companyName }}
          </p>
        </div>
        <div class="public-flex-center-column">
          <div>渠道</div>
          <p
            v-if="
              detailInfo.tProjectChannel &&
              detailInfo.tProjectChannel.channelName
            "
          >
            {{ detailInfo.tProjectChannel.channelName }}
          </p>
          <p v-else-if="scope.row.channel=='1'" >
            直接触达
          </p>
          <p v-else="scope.row.channel=='2'" >
            内部推荐
          </p>
        </div>
        <div class="public-flex-center-column">
          <div>项目负责人</div>
          <p>{{ detailInfo.investHead }}</p>
        </div>
        <div class="public-flex-center-column" v-if="detailInfo.projectDate">
          <div>立项通过日期</div>
          <p>{{ detailInfo.projectDate }}</p>
        </div>
        <div class="public-flex-center-column" v-if="detailInfo.decisionDate">
          <div>投决通过日期</div>
          <p>{{ detailInfo.decisionDate }}</p>
        </div>
        <div class="public-flex-center-column">
          <div>投前估值（万元）</div>
          <p>{{ detailInfo.investValuation }}</p>
        </div>
        <div class="public-flex-center-column">
          <div>预计投资金额（万元）</div>
          <p>{{ detailInfo.investMoney }}</p>
        </div>
      </div>
    </section>
    <section class="section3">
      <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
        <el-tab-pane label="尽职背调信息" name="0"></el-tab-pane>
        <el-tab-pane label="项目基本信息" name="1"></el-tab-pane>
      </el-tabs>
      <section>
        <div v-if="activeName === '0'" class="public-padded-20 detail-meeting">
          <el-tabs type="border-card" v-model="activeTab">
            <el-tab-pane label="尽职背调申请表" name="apply">
              <el-form
                v-if="dueForm.id"
                ref="dueForm"
                class="special-el-form public-padded-t-20"
                :model="dueForm"
                :rules="dueRules"
                label-width="170px"
              >
                <el-form-item label="尽调名称" class="special-el-form-item">
                  <el-input
                    disabled
                    v-model="dueForm.investigateName"
                    placeholder="请输入尽调名称"
                  />
                </el-form-item>
                <el-form-item
                  label="尽调地点"
                  prop="investigatePlace"
                  class="special-el-form-item"
                >
                  <el-input
                    disabled
                    maxlength="100"
                    v-model="dueForm.investigatePlace"
                  />
                </el-form-item>
                <el-form-item label="期望尽调开始时间" prop="startTime">
                  <el-date-picker
                    disabled
                    clearable
                    v-model="dueForm.startTime"
                    type="date"
                    value-format="yyyy-MM-dd"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="期望尽调结束时间" prop="endTime">
                  <el-date-picker
                    disabled
                    clearable
                    v-model="dueForm.endTime"
                    type="date"
                    value-format="yyyy-MM-dd"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="尽调资料是否齐全" prop="readiness">
                  <el-radio-group v-model="dueForm.readiness" disabled>
                    <el-radio label="1">是</el-radio>
                    <el-radio label="0">否</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="是否有第三方投资机构" prop="thirdParty">
                  <el-radio-group v-model="dueForm.thirdParty" disabled>
                    <el-radio label="1">是</el-radio>
                    <el-radio label="0">否</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="尽调费用(元)">
                  <el-input disabled v-model="dueForm.investigateCost" />
                </el-form-item>

                <el-form-item label="尽调财务数据截止日期" prop="deadDate">
                  <el-date-picker
                    disabled
                    clearable
                    v-model="dueForm.deadDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="尽调人员" class="special-el-form-item">
                  <el-input
                    type="textarea"
                    rows="2"
                    disabled
                    v-model="dueForm.investigatePerson"
                  />
                </el-form-item>
                <el-form-item label="尽调资料" class="special-el-form-item">
                  <fileItem
                    ref="fileItems1"
                    :id="dueForm.id"
                    :is-detail="true"
                  ></fileItem>
                </el-form-item>
                <el-form-item label="描述" class="special-el-form-item">
                  <el-input
                    disabled
                    rows="4"
                    v-model="dueForm.describe"
                    type="textarea"
                  />
                </el-form-item>
                <el-form-item label="备注" class="special-el-form-item">
                  <el-input
                    disabled
                    rows="4"
                    type="textarea"
                    v-model="dueForm.remark"
                  />
                </el-form-item>
                <el-form-item label="财务评估报告" class="special-el-form-item" v-if="dueForm.taskDefKey=='president'">
                  <fileItem ref="financeFileItem" :id="dueForm.id" :is-detail="true"></fileItem>
                </el-form-item>
                <el-form-item label="法务评估报告"  class="special-el-form-item" v-if="dueForm.taskDefKey=='president'">
                  <fileItem ref="legalFileItem" :id="dueForm.id" :is-detail="true"></fileItem>
                </el-form-item>
                <el-form-item label="其他" class="special-el-form-item" v-if="dueForm.taskDefKey=='president'">
                  <fileItem ref="otherFileItem" :id="dueForm.id" :is-detail="true"></fileItem>
                </el-form-item>
<!--                <el-form-item label="财务评估报告" prop="financeListFile" class="special-el-form-item" v-show="user.dept.deptName.indexOf('财务') > -1">
                  <fileItem ref="financeFileItem" :id="dueForm.id" @getFileList="getFinanceFileList"></fileItem>
                </el-form-item>
                <el-form-item label="法务评估报告" prop="legalListFile" class="special-el-form-item" v-show="user.dept.deptName.indexOf('法务') > -1">
                  <fileItem ref="legalFileItem" :id="dueForm.id" @getFileList="getLegalFileList"></fileItem>
                </el-form-item>-->
                <el-form-item label="评估报告" prop="assessListFile" class="special-el-form-item" v-if="dueForm.taskDefKey==='approve'">
                  <fileItem ref="assessFileItem" :id="dueForm.id" @getFileList="getAssessFileList"></fileItem>
                </el-form-item>
              </el-form>
            </el-tab-pane>
<!--            <el-tab-pane label="项目投决评估评审汇总表"  v-if="dueForm.taskDefKey==='confirm'||dueForm.taskDefKey=='president'" name="review">
              <div v-if="dueAllPeportList.length > 0">
                <el-form
                  v-for="(item, index) in dueAllPeportList"
                  :key="index"
                  class="special-el-form public-padded-t-20"
                  :model="item"
                  label-width="100px"
                >
                  <el-form-item label="尽调人员" class="special-el-form-item">
                    <el-input disabled v-model="item.investigatePerson" />
                  </el-form-item>
                  <el-form-item label="报告" class="special-el-form-item">
                    <fileItem
                      ref="dueAllreportFileItems"
                      :id="item.id"
                      @getFileList="getreportFileList"
                    ></fileItem>
                  </el-form-item>
&lt;!&ndash;                  <el-form-item label="备注" class="special-el-form-item">
                    <el-input
                      disabled
                      rows="4"
                      type="textarea"
                      v-model="item.remark"
                      placeholder="请输入备注"
                    />
                  </el-form-item>&ndash;&gt;
                  <el-divider></el-divider>
                </el-form>
              </div>
              <div
                v-else
                class="public-flex-center public-padded-20"
                style="color: #909399"
              >
                暂无数据
              </div>
            </el-tab-pane>-->
          </el-tabs>
        </div>
        <div v-show="activeName === '1'">
          <poolForm :type="type" :id="id"></poolForm>
        </div>
      </section>
    </section>
    <section v-if="dueForm.taskDefKey==='upload'">
      <el-form
        ref="duePeportForm"
        class="special-el-form"
        :model="duePeportForm"
        :rules="dueRules1"
        label-width="170px"
      >
        <el-form-item label="上传尽调报告" prop="listFile" class="special-el-form-item">
          <fileItem
            ref="reportFileItems"
            :id="duePeportForm.id"
            @getFileList="getreportFileList"
          ></fileItem>
        </el-form-item>
      </el-form>
    </section>
    <section>
      <flowBase :procInstId="dueForm.procInstId" :id="id" v-if="dueForm.procInstId "></flowBase>
    </section>
    <section>

      <el-form
        ref="flowBaseInfo"
        :model="flowBaseInfo"
        label-width="120px"
      >
        <el-form-item label="审批意见" prop="comment">
          <el-input
            maxlength="1000"
            rows="4"
            type="textarea"
            v-model="flowBaseInfo.comment"
            placeholder="请填写审批意见"
          >
          </el-input>
        </el-form-item>
        <el-form-item class="btnList">
          <el-button @click="goBack">返 回</el-button>
          <el-button type="success" class="editButton" @click="handleComplete(true)">同意</el-button>
          <el-button v-if="dueForm.taskDefKey!='upload'&&dueForm.taskDefKey!='approve'&&dueForm.taskDefKey!='president'" type="warning" @click="handleComplete(false)">驳回</el-button>
          <el-button v-if="dueForm.taskDefKey=='president'" type="warning" @click="handleComplete(false)">驳回（修改）</el-button>
          <el-button v-if="dueForm.taskDefKey=='president'" type="danger" @click="handleComplete(false,1)">驳回（终止）</el-button>
          <el-button v-if="dueForm.taskDefKey=='president'" type="warning" @click="handleComplete(false,0)">观望</el-button>
        </el-form-item>
      </el-form>
    </section>
  </div>
</template>
<script>
import poolForm from "../../invest/components/poolForm";
import fileItem from "../../invest/components/fileItem";
import flowBase from "../flowBase.vue";

import {
  getPool,
  getDueAllPeportList} from "@/api/invest/pool";
import {listMeeting} from "@/api/invest/meeting";
import{
  investigateComplete,
  getInvestigateInfo,
} from "@/api/project/investigate/pool"

import { mapGetters } from "vuex";
import selecDept from "../../invest/components/selecDept.vue";
import projectItem from "../../invest/components/projectItem.vue";
import selecUser from "../../invest/components/selecUser.vue";


export default {
  name: "decisionAudit",
  props: {
    readonly: {
      type: Boolean,
    },
  },
  dicts: ["meeting_type", "project_stage", "project_group"],
  components: {
    selecUser,
    projectItem, selecDept,
    flowBase,
    poolForm,
    fileItem,
  },
  data() {
    const validateLogo = (rule, value, callback) => {
      if (this.reportFileList.length <= 0) {
        callback(new Error("请上传文件"));
      } else {
        callback();
      }
    };
   /* const financeValidateLogo = (rule, value, callback) => {
      if (this.financeFileList.length <= 0) {
        callback(new Error("请上传财务评估报告"));
      } else {
        callback();
      }
    };
    const legalValidateLogo = (rule, value, callback) => {
      if (this.legalFileList.length <= 0) {
        callback(new Error("请上传法务评估报告"));
      } else {
        callback();
      }
    };*/
    const assessValidateLogo = (rule, value, callback) => {
      if (this.assessFileList.length <= 0) {
        callback(new Error("请上传评估报告"));
      } else {
        callback();
      }
    };
    return {
      // 人员选择器
      checkType: "multiple",
      // 数据回显
      selectValues: null,
      selectNameValues: null,
      type: "2",
      id: "",
      active: 0,
      detailInfo: {
        projectName: "",
      },
      activeName: "0",
      activeTab: 'apply', // 默认激活“尽职背调申请表”
      formType:3,
      dueForm: {
        id: null,
        investigateName: null,
        investigateCode: null,
        projectPoolId: null,
        investigatePerson: null,
        investigatePersonId: null,
        investigateCost: null,
        //
        startTime: null,
        endTime: null,
        investigatePlace: null,
        readiness: null,
        thirdParty: null,
        deadDate: null,
        //
        describe: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        projectName: null,
        flag:null,
        taskDefKey:null,
        financeListFile:null,
        /*legalListFile: null,
        reportListFile:null,*/
        assessListFile:null,
      },
      dueRules:{
       /* financeListFile: [{ required: true, validator: financeValidateLogo }],
        legalListFile: [{ required: true, validator: legalValidateLogo }],*/
        assessListFile: [{ required: true, validator: assessValidateLogo }],
      },
      duePeportForm: {
        createBy: null,
        createTime: null,
        deptId: null,
        deptName: null,
        id: null,
        investigatePerson: null,
        investigatePersonId: null,
        listFile: null,
        projectInvestigateId: null,
        remark: null,
        updateBy: null,
        updateTime: null,
      },
      dueRules1: {
        listFile: [{ required: true, validator: validateLogo }],
      },
      reportFileList: [],
      /*financeFileList: [],
      legalFileList: [],*/
      assessFileList: [],
      dueAllPeportList: [],
      //打分-结束
      flowBaseInfo:{
        comment:null,
        taskId:null
      },
      rulesFlow:{
        comment: [{ required: true, trigger: "blur", message: "请输入" }],
      }
    };
  },
  computed: {
    ...mapGetters(["user"]),
  },
  created() {
    this.id = this.$route.query.id;
    this.flowBaseInfo.taskId=this.$route.query.taskId;
    this.getDetail();
    this.getInvestigateListProjectPoolIdNew(this.id);
    if (this.$route.query.activeName) {
      this.activeName = this.$route.query.activeName;
    }
  },
  methods: {

    handleClick(tab, event) {
      let that = this;
      if (this.activeName === "0") {
        // 尽职背调
        this.getInvestigateListProjectPoolIdNew(this.id);
      } else if (this.activeName === "1") {
        // 项目报备
        this.getDetail();
      }
    },
    // 根据项目ID获取最新尽调申请
    getInvestigateListProjectPoolIdNew(id) {
      let that = this;
      getInvestigateInfo(this.$route.query.pInstId).then((response) => {
        if (response && response.data) {
          that.dueForm = response.data;
          setTimeout(() => {
            this.$refs.fileItems1.listFile = [];
            this.$refs.fileItems1.handleButton();
            this.$refs.fileItems1.getListFileBusinessId(that.dueForm.id,"11");
            if(response.data.taskDefKey==='president') {
              this.$refs.financeFileItem.handleButton();
              this.$refs.legalFileItem.handleButton();
              //财务评估报告
              this.$refs.financeFileItem.fileList = [];
              this.$refs.financeFileItem.getListFileBusinessId(that.dueForm.id, "14");
              //法务评估报告
              this.$refs.legalFileItem.fileList = [];
              this.$refs.legalFileItem.getListFileBusinessId(that.dueForm.id, "15");
            }
            if(response.data.taskDefKey==='approve') {
              if (this.user.dept.deptName.indexOf('财务')>-1) {
                this.$refs.assessFileItem.fileList = [];
                this.$refs.assessFileItem.getListFileBusinessId(that.dueForm.id, "14");
              }else{
                this.$refs.assessFileItem.fileList = [];
                this.$refs.assessFileItem.getListFileBusinessId(that.dueForm.id, "15");
              }
            }
            //其他
            this.$refs.otherFileItem.handleButton();
            this.$refs.otherFileItem.fileList = [];
            this.$refs.otherFileItem.getListFileBusinessId(that.dueForm.id, "0");
          }, 300);

          if(response.data.taskDefKey==='confirm'||response.data.taskDefKey === "president"){
            this.handleLook(response.data);
          }
        }
      });
    },
    // 根据尽调申请id查下面所有的报告
    handleLook(row) {
      let that = this;
      getDueAllPeportList(row.id).then((response) => {
        let rows = response.rows;
        this.dueAllPeportList = rows;
        if (rows && rows.length > 0) {
          setTimeout(() => {
            for (let i in rows) {
              this.$refs.dueAllreportFileItems[i].handleButton();
              this.$refs.dueAllreportFileItems[i].getListFileBusinessId(
                rows[i].id
              );
            }
          }, 300);
        }
      });
    },
    //尽调报告
    getreportFileList(fileList) {
      if (fileList && fileList.length > 0) {
        this.reportFileList = fileList;
        this.$refs.duePeportForm.clearValidate(["listFile"]);
      } else {
        this.reportFileList = [];
      }
    },
    //财务评估报告
    /*getFinanceFileList(fileList) {
      if (fileList && fileList.length > 0) {
        this.financeFileList = fileList;
        this.$refs.dueForm.clearValidate(["financeListFile"]);
      } else {
        this.financeFileList = [];
      }
    },
    //法务评估报告
    getLegalFileList(fileList) {
      if (fileList && fileList.length > 0) {
        this.legalFileList = fileList;
        this.$refs.dueForm.clearValidate(["legalListFile"]);
      } else {
        this.legalFileList = [];
      }
    },*/
    //评估报告
    getAssessFileList(fileList) {
      if (fileList && fileList.length > 0) {
        this.assessFileList = fileList;
        this.$refs.dueForm.clearValidate(["assessListFile"]);
      } else {
        this.assessFileList = [];
      }
    },
    //项目报备
    getDetail() {
      getPool(this.id).then((response) => {
        this.detailInfo = response.data;
        // 项目退出完成n
        if (response.data.projectState === "n") {
          this.active = parseInt(response.data.projectStage);
        } else {
          this.active = parseInt(response.data.projectStage) - 1;
        }
      });
    },


    /** 加载审批任务弹框 */
    handleComplete(flag,isTerminate) {
      this.taskComplete(flag,isTerminate);
    },
    /** 用户审批任务 */
    taskComplete(flag,isTerminate) {
      let allValid = true;
      if(this.dueForm.taskDefKey==="upload") {//上传尽职背调报告
        this.duePeportForm.projectPoolId = this.dueForm.projectPoolId;
        this.duePeportForm.projectInvestigateId = this.dueForm.id;
        this.duePeportForm.listFile = this.reportFileList;
        //console.log(this.duePeportForm);
        this.$refs["duePeportForm"].validate((valid) => {
          if (!valid) {
            allValid = false;
            return;
          }
        })
      }else if(this.dueForm.taskDefKey==="approve"){
        this.dueForm.listFile =  this.assessFileList;
        this.$refs["dueForm"].validate((valid) => {
          if (!valid) {
            allValid = false;
            return;
          }
        })
      }
      if (!flag&&!this.flowBaseInfo.comment) {
        this.$modal.msgError("请输入审批意见!");
        return;
      }
      //会议人
      this.dueForm.flag=flag;
      this.dueForm.isTerminate=isTerminate;
      const params={
        projectInvestigate: this.dueForm,  // 拆分明确字段
        projectInvestigatePerson: this.duePeportForm,
        flowBaseInfo: this.flowBaseInfo
      }
      if(allValid) {
        try{
            investigateComplete(params).then(response => {
              this.$modal.msgSuccess(response.msg);
              this.$store.dispatch("getMyTaskInvestigateNumber"); //更新代办数量
              this.goMyTask();
            });
        } catch (err) {
          // 捕获重复提交错误
          this.$message.error(err.message);
        }
      }
    },
    /*返回*/
    goBack() {
      this.$router.go(-1);
    },
    //跳转到我的任务-项目机会
    goMyTask(){
      //this.$router.push({ path: "/myTask/project/decision/myTask"});
      // 关闭当前标签页并返回指定页面
      const obj = { path: "/myTask/project/investigate/myTask", query: { t: Date.now()} };
      this.$tab.closeOpenPage(obj);
    },

  },
};
</script>
<style lang="scss" scoped>
.detailWrapper {
  padding: 20px 0;
  width: 92%;
  margin: 0 auto;
  .section1 {
    p {
      font-size: 24px;
      font-weight: bold;
      margin: 0;
    }
    > div:first-child {
      // width: calc(100% - 700px);
      width: 100%;
    }
    .btnList {
      width: 700px;
      display: flex;
      justify-content: flex-end;
    }
  }
  .basicsInfo {
    font-size: 14px;
    background: #f1f1f1;
    margin-top: -24px;
    width: 100%;
    padding: 30px 20px 10px;
    div {
      color: #7e7d7d;
    }
    p {
      font-size: 16px;
      color: #333;
      font-weight: bold;
    }
  }
  .section3 {
    border: 1px solid #DCDFE6;
    margin-top: 40px;
  }
  .btnList {
    display: flex;
    justify-content: flex-end;
    /* 让内部元素（按钮）整体靠右 */
    gap: 10px; /* 替代 margin，给按钮之间加间距，可选 */
  }
}
.projectNameTit {
}
.alreadyStop {
  font-weight: normal;
  font-size: 12px;
  background: #ff0000;
  color: #fff;
  border-radius: 5px;
  padding: 5px 10px;
  margin-left: 10px;
}
/*表格样式开始*/
.ss_scoreTable {
  padding-bottom: 50px;
  ::v-deep .el-input.is-disabled .el-input__inner {
    background-color: #fff !important;
    color: #606266;
  }
  ::v-deep .el-input__inner {
    border: 1px solid transparent;
    text-align: center;
  }
  ::v-deep.el-textarea__inner {
    border: none;
    height: 50px;
    resize: none;
  }
  ::v-deep .el-textarea.is-disabled .el-textarea__inner {
    background-color: #fff !important;
  }
  textarea:disabled,
  input:disabled {
    background-color: #fff !important;
  }
  // ::v-deep .el-textarea__inner {
  //   // border: 1px solid transparent!important;
  // }
  table {
    // width:80%;
    // width: 600px;
    margin: 0 30px;
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    -khtml-user-selece: none;
    /*上面都是兼容性问题，具体看浏览器版本或什么浏览器*/
    user-select: none; /*内容禁止选中*/
  }
  tbody td {
    font-size: 14px;
  }
  thead th,
  .big-font {
    font-size: 16px !important;
    font-weight: bold;
  }
  .public-pedded-0 {
    padding: 0;
  }
  .text-center {
    text-align: center;
  }
  .text-left {
    text-align: left;
  }
  .table-textarea {
    width: 100%;
    // width: 180px;
    border: none;
    height: 50px;
    resize: none;
  }
  .table-input {
    width: 100%;
    // width: 100%;
    // height: 80px;
    border: none;
    font-size: 20px;
    font-weight: bold;
    text-align: center;
    margin-top: 20px;
  }
  .bg-blue,
  .specificTr > th:nth-child(2n-1) {
    background-color: #9bc2e6;
  }
  .specificTr > th:nth-child(2n-1) {
    white-space: nowrap;
    padding: 0 15px;
  }
  .specificTr .table-textarea {
    padding-top: 16px;
    background-color: #fff;
  }
  tr > th {
    height: 50px;
  }
  input:disabled {
    // background-color: #fff !important;
  }
  .btnList {
    width: 100%;
    display: flex;
    justify-content: center;
    padding-bottom: 40px;
    margin-top: 30px;
  }
}
</style>
