<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>无</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" @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">
            <el-tab-pane label="项目立项申请表">
              <el-form
                v-if="formLXApply.id"
                class="special-el-form public-padded-t-20"
                ref="formLXApply"
                :model="formLXApply"
                :rules="rulesLXApply"
                label-width="120px"
              >
                <el-form-item
                  label="项目概况"
                  prop="projectOverview"
                  class="special-el-form-item"
                >
                  <el-input
                    rows="4"
                    type="textarea"
                    maxlength="200"
                    v-model="formLXApply.projectOverview"
                  />
                </el-form-item>
                <el-form-item
                  label="项目亮点"
                  prop="projectSparkle"
                  class="special-el-form-item"
                >
                  <el-input
                    rows="4"
                    type="textarea"
                    maxlength="200"
                    v-model="formLXApply.projectSparkle"
                  />
                </el-form-item>
                <el-form-item
                  label="立项资料"
                  prop="listFile"
                  class="special-el-form-item"
                >
                  <fileItem
                    ref="fileItemLXApply"
                    :id="formLXApply.id"
                    @getFileList="getFileListLXApply"
                  ></fileItem>
                </el-form-item>
              </el-form>
              <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 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>
  </div>
</template>
<script>
import poolForm from "../../invest/components/poolForm";
import fileItem from "../../invest/components/fileItem";

import {getPool} from "@/api/invest/pool";
import {
  listMeeting,
  listProjectPoolIdNewApproval
} from "@/api/invest/meeting";
import{
  approvalComplete
} from "@/api/project/approval/pool"

import { mapGetters } from "vuex";

export default {
  name: "approvalEdit",
  components: {
    poolForm,
    fileItem
  },
  data() {
    const validateLogo = (rule, value, callback) => {
      if (this.fileListLXApply.length <= 0) {
        callback(new Error("请上传文件"));
      } else {
        callback();
      }
    };
    return {
      showDueApply: false,
      meetingId: "0",
      type: "2",
      id: "",
      active: 0,
      detailInfo: {
        projectName: "",
      },
      activeName: "0",
      formLXApply: {
        id: null,
        procInstId:null,
        projectPoolId: null,
        projectName: null,
        projectOverview: null,
        projectSparkle: null,
        listFile: null,
        participants: null,
        participantsId: null,
      },
      fileListLXApply: [],
      rulesLXApply: {
        projectOverview: [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        projectSparkle: [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        listFile: [{ required: true, validator: validateLogo }],
      },
      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.getlistProjectPoolIdNewApproval();
    this.getDetail();
    if (this.$route.query.activeName) {
      this.activeName = this.$route.query.activeName;
    }
  },
  methods: {
    getMettingId(type) {
      let queryParams = {
        pageNum: 1,
        pageSize: 10,
        meetingType: type,
        orderByColumn: "createTime",
        isAsc: "desc",
        projectPoolId: this.id,
      };
      listMeeting(queryParams).then((response) => {
        let meetingList = response.rows;
        if (meetingList.length > 0) {
          let meetingId = meetingList[0].id;
          if (type === "LX") {
            this.$refs.approvalTableForm.initialize(meetingId);
          } else if (type === "TJ") {
            this.$refs.voteTableForm.initialize(meetingId);
          }
        } else {
          if (type === "LX") {
            this.$refs.approvalTableForm.showNodata();
          } else if (type === "TJ") {
            this.$refs.voteTableForm.showNodata();
          }
        }
      });
    },
    handleClick(tab, event) {
      let that = this;
      if (this.activeName === "0") {
        // 项目立项
        this.getMettingId("LX");
        this.getlistProjectPoolIdNewApproval();
      } else if (this.activeName === "1") {
        // 项目报备
        this.getDetail();
      }
    },
    // 获取立项申请
    getlistProjectPoolIdNewApproval() {
      let that = this;
      listProjectPoolIdNewApproval(this.id).then((response) => {
        if (response && response.data) {
          that.formLXApply = response.data;
          setTimeout(() => {
            //that.$refs.fileItemLXApply.handleButton();
            that.$refs.fileItemLXApply.fileList = [];
            that.$refs.fileItemLXApply.getListFileBusinessId(
              that.formLXApply.id
            );
          }, 300);
        }
      });
    },
    // 立项申请附件回显
    getFileListLXApply(fileList) {
      this.fileListLXApply = fileList;
      if (fileList && fileList.length > 0) {
        this.$refs.formLXApply.clearValidate(["listFile"]);
      } else {
        this.fileListLXApply = [];
      }
    },
    //项目报备
    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) {
      this.taskComplete(flag);
    },
    /** 用户审批任务 */
    taskComplete(flag) {
      this.formLXApply.flag=flag;
      this.formLXApply.listFile = this.fileListLXApply;
      const params={
        projectApproval:this.formLXApply,
        flowBaseInfo: this.flowBaseInfo
      }
      this.$refs["formLXApply"].validate((valid) => {
        if(valid) {
          try{
            approvalComplete(params).then(response => {
              this.$modal.msgSuccess(response.msg);
              this.$store.dispatch("getMyTaskApprovalNumber"); //更新代办数量
              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/approval/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 {
    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>
