<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>
      <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'" >
          <el-tabs type="border-card" style="border: 1px solid #DCDFE6;">
            <el-tab-pane label="尽职背调申请表">
              <el-form
                ref="dueForm"
                class="special-el-form"
                :model="dueForm"
                :rules="dueRules"
                label-width="170px"
              >
                <el-form-item
                  label="项目名称"
                  prop="projectName"
                  class="special-el-form-item"
                >
                  <el-input
                    rows="2"
                    type="textarea"
                    disabled
                    v-model="dueForm.projectName"
                  />
                </el-form-item>
                <el-form-item
                  label="尽调名称"
                  prop="investigateName"
                  class="special-el-form-item"
                >
                  <el-input
                    maxlength="100"
                    v-model="dueForm.investigateName"
                    placeholder="请输入尽调名称"
                  />
                </el-form-item>
                <el-form-item
                  label="尽调地点"
                  prop="investigatePlace"
                  class="special-el-form-item"
                >
                  <el-input
                    maxlength="100"
                    v-model="dueForm.investigatePlace"
                    placeholder="请输入尽调地点"
                  />
                </el-form-item>

                <el-form-item label="期望尽调开始时间" prop="startTime">
                  <el-date-picker
                    clearable
                    v-model="dueForm.startTime"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择期望尽调开始时间"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="期望尽调结束时间" prop="endTime">
                  <el-date-picker
                    clearable
                    v-model="dueForm.endTime"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择期望尽调结束时间"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="尽调资料是否齐全" prop="readiness">
                  <el-radio-group v-model="dueForm.readiness">
                    <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">
                    <el-radio label="1">是</el-radio>
                    <el-radio label="0">否</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="尽调费用(元)" prop="investigateCost">
                  <el-input
                    maxlength="20"
                    v-model="dueForm.investigateCost"
                    placeholder="请输入尽调费用"
                    @input="
              (value) =>
                (dueForm.investigateCost = value
                  .replace(/[^0-9.]/g, '')
                  .replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
                  .replace(/-/g, ''))
            "
                  />
                </el-form-item>
                <el-form-item label="尽调财务数据截止日期" prop="deadDate">
                  <el-date-picker
                    clearable
                    v-model="dueForm.deadDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择尽调财务数据截止日期"
                  >
                  </el-date-picker>
                </el-form-item>

                <el-form-item
                  label="尽调人员"
                  prop="investigatePerson"
                  class="special-el-form-item public-input-height-2"
                >
                  <div
                    class="el-input__inner inputSimulation"
                    @click="handleSelectPeople"
                  >
                    {{
                      dueForm.investigatePerson
                        ? dueForm.investigatePerson
                        : "请选择尽调人员"
                    }}
                  </div>
                </el-form-item>
                <el-form-item
                  label="尽调背调资料"
                  prop="file"
                  class="special-el-form-item"
                >
                  <fileItem
                    ref="fileItems"
                    :id="dueForm.id"
                    @getFileList="getFileList"
                  ></fileItem>
                </el-form-item>
                <el-form-item label="描述" prop="describe" class="special-el-form-item">
                  <el-input
                    rows="4"
                    maxlength="200"
                    v-model="dueForm.describe"
                    type="textarea"
                    placeholder="请输入内容"
                  />
                </el-form-item>
                <el-form-item label="备注" prop="remark" class="special-el-form-item">
                  <el-input
                    rows="4"
                    maxlength="200"
                    type="textarea"
                    v-model="dueForm.remark"
                    placeholder="请输入备注"
                  />
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </div>
        <div v-show="activeName === '1'">
          <poolForm :type="type" :id="id"></poolForm>
        </div>
      </section>
    </section>

    <section class="approveBtn">
      <el-form
        ref="flowBaseInfo"
        :model="flowBaseInfo"
        :rules="rulesFlow"
        label-width="120px"
      >
        <el-form-item class="btnList">
          <el-button @click="goBack">返 回</el-button>
          <el-button type="primary" @click="handleComplete(true)">提交</el-button>
          <el-button type="warning" @click="handleComplete(false)">关闭</el-button>
        </el-form-item>
      </el-form>
    </section>
    <!--选择人员-->
    <selecUser
      ref="flowUser"
      :checkType="checkType"
      :selectValues="selectValues"
      :selectNameValues="selectNameValues"
      @handleUserSelect="handleUserSelect"
    ></selecUser>
  </div>
</template>
<script>
import poolForm from "../../invest/components/poolForm";
import fileItem from "../../invest/components/fileItem";

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

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

export default {
  name: "investigateEdit",
  components: {
    selecUser,
    poolForm,
    fileItem
  },
  data() {
    const validateLogo = (rule, value, callback) => {
      if (this.reportFileList.length <= 0) {
        callback(new Error("请上传文件"));
      } else {
        callback();
      }
    };
    return {
      // 人员选择器
      checkType: "multiple",
      // 数据回显
      selectValues: null,
      selectNameValues: null,
      showDueApply: false,
      meetingId: "0",
      type: "2",
      id: "",
      active: 0,
      detailInfo: {
        projectName: "",
      },
      activeName: "0",
      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,
      },
      dueRules: {
        investigateName: [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        projectName: [{ required: true, trigger: "blur", message: "请输入" }],
        investigatePerson: [
          { required: true, trigger: "blur", message: "请选择" },
        ],
        investigateCost: [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        describe: [{ required: true, trigger: "blur", message: "请输入" }],

        startTime: { required: true, trigger: "blur", message: "请选择" },
        endTime: { required: true, trigger: "blur", message: "请选择" },
        investigatePlace: [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        readiness: [{ required: true, trigger: "change", message: "请选择" }],
        thirdParty: [{ required: true, trigger: "change", message: "请选择" }],
        deadDate: { required: true, trigger: "blur", message: "请选择" },
      },
      flowBaseInfo:{
        comment:null,
        taskId:null
      },
      rulesFlow:{
        comment: [{ required: true, trigger: "blur", message: "请输入" }],
      }
    };
  },
  computed: {
    ...mapGetters(["user"]),
  },
  created() {
    this.id = this.$route.query.id;
    this.getInvestigateListProjectPoolIdNew(this.id);
    this.getDetail();
    this.flowBaseInfo.taskId=this.$route.query.taskId;
    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.data) {
          this.dueForm = response.data;
          setTimeout(() => {
            this.$refs.fileItems.listFile = [];
            //that.$refs.fileItems.handleButton();
            this.$refs.fileItems.getListFileBusinessId(that.dueForm.id,"11");
          }, 300);
          //this.handleLook(response.data);
        }
      });
    },
    // 获取fileList
    getFileList(fileList) {
      if (fileList && fileList.length > 0) {
        this.fileList = fileList;
      } else {
        this.fileList = [];
      }
    },
    //项目报备
    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;
        }
      });
    },
    //尽职背调人员展示
    handleSelectPeople() {
      this.$refs.flowUser.handleUserVisible(true);
    },
    // 用户选中数据
    handleUserSelect(selection) {
      const that = this;
      if (selection) {
        if (selection instanceof Array) {
          const userIds = selection.map((item) => item.userId);
          const nickName = selection.map((item) => item.nickName);
          this.dueForm.investigatePerson = nickName.join(",");
          this.dueForm.investigatePersonId = userIds.join(",");
          this.selectNameValues = nickName.join(",");
          this.selectValues = userIds.join(",");
        } else {
          this.dueForm.investigatePerson = selection.nickName;
          this.dueForm.investigatePersonId = selection.userId;
          this.selectNameValues = selection.nickName;
          this.selectValues = selection.userId;
        }
        this.$refs.dueForm.clearValidate(["investigatePerson"]);
      }
    },

    /** 加载审批任务弹框 */
    handleComplete(flag) {
      this.taskComplete(flag);
    },
    /** 用户审批任务 */
    taskComplete(flag) {
      this.dueForm.flag = flag;
      this.dueForm.listFile = this.fileList;
      this.dueForm.investigateCost = this.dueForm.investigateCost
        .replace(/[^0-9.]/g, "")
        .replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3")
        .replace(/-/g, "");
      if (
        new Date(this.dueForm.endTime).getTime() <
        new Date(this.dueForm.startTime).getTime()
      ) {
        this.$confirm(
          "期望尽调结束时间不能小于期望尽调开始时间,请重新选择!",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(() => {})
          .catch(() => {});
        return false;
      }
      const params={
        projectInvestigate:this.dueForm,
        flowBaseInfo: this.flowBaseInfo
      }
      this.$refs["dueForm"].validate((valid) => {
        if (valid) {
          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/approval/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;
  }
  .approveBtn {
    margin-top: 30px;
    .btnList {
      display: flex;
      justify-content: flex-end;
      /* 让内部元素（按钮）整体靠右 */
      gap: 10px; /* 替代 margin，给按钮之间加间距，可选 */
      padding-right: 50px;
    }
  }
}
.projectNameTit {
}
.alreadyStop {
  font-weight: normal;
  font-size: 12px;
  background: #ff0000;
  color: #fff;
  border-radius: 5px;
  padding: 5px 10px;
  margin-left: 10px;
}

</style>
