<template>
  <div class="app-container">
    <el-form
      v-if="type !== '2'"
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="渠道" prop="channel">
        <el-select v-model="queryParams.channel" placeholder="全部" clearable>
          <el-option
            v-for="item in channelList"
            :key="item.id"
            :label="item.channelName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属组别" prop="projectGroup">
        <el-select
          v-model="queryParams.projectGroup"
          placeholder="全部"
          clearable
        >
          <el-option
            v-for="dict in dict.type.project_group"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="投资负责人" prop="investHead">
        <el-input
          v-model="queryParams.investHead"
          placeholder="请输入投资负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目联系人" prop="projectContacts">
        <el-input
          v-model="queryParams.projectContacts"
          placeholder="请输入项目联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!-- <el-form-item label="初次接触日期" prop="contactDate">
        <el-date-picker
          clearable
          v-model="queryParams.contactDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择初次接触日期"
        >
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="立项通过日期" prop="projectDate">
        <el-date-picker
          clearable
          v-model="queryParams.projectDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择立项通过日期"
        >
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="投决通过日期" prop="decisionDate">
        <el-date-picker
          clearable
          v-model="queryParams.decisionDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择投决通过日期"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="项目公司" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入项目所属公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="项目编号" prop="projectCode">
        <el-input
          v-model="queryParams.projectCode"
          placeholder="请输入项目编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="历次融资" prop="previousFinancing">
        <el-input
          v-model="queryParams.previousFinancing"
          placeholder="请输入历次融资"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="项目融资阶段" prop="financingStage">
        <el-input
          v-model="queryParams.financingStage"
          placeholder="请输入项目融资阶段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="预期融资金额" prop="financingMoney">
        <el-input
          v-model="queryParams.financingMoney"
          placeholder="请输入预期融资金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="投前估值" prop="investValuation">
        <el-input
          v-model="queryParams.investValuation"
          placeholder="请输入投前估值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="预计投资金额" prop="investMoney">
        <el-input
          v-model="queryParams.investMoney"
          placeholder="请输入预计投资金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="创建日期" prop="createTime">
        <el-date-picker
          style="width: 205px"
          clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row
      :gutter="10"
      class="mb8"
      v-if="type === '2' && stage == '4' ? false : true"
    >
      <el-col :span="1.5">
        <el-button
          v-if="type === '1' && (stage == '0' || stage == '1')"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['invest:pool:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['invest:pool:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-switch-button"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['invest:pool:remove']"
          >终止</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['invest:pool:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="poolList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column
        label="项目名称"
        align="center"
        prop="projectName"
        width="150"
      />
      <el-table-column
        label="投资负责人"
        align="center"
        prop="investHead"
        width="100px"
      />
      <!-- <el-table-column label="项目编号" align="center" prop="id" /> -->
      <el-table-column
        label="渠道"
        align="center"
        prop="tProjectChannel.channelName"
      />
      <el-table-column label="所属组别" align="center" prop="projectGroup">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.project_group"
            :value="scope.row.projectGroup"
          />
        </template>
      </el-table-column>

      <el-table-column label="项目阶段" align="center" prop="projectStage">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.project_stage"
            :value="scope.row.projectStage"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="项目状态"
        align="center"
        prop="projectState"
        width="150"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.project_state"
            :value="scope.row.projectState"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="初次接触日期"
        align="center"
        prop="contactDate"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contactDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="立项通过日期"
        align="center"
        prop="projectDate"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.projectDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="投决通过日期"
        align="center"
        prop="decisionDate"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.decisionDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目所属行业" align="center" prop="industry" />
      <el-table-column
        label="项目所属公司"
        align="center"
        prop="tProjectCompany.companyName"
      />
      <el-table-column
        label="项目联系人"
        align="center"
        prop="tProjectContacts.name"
      />

      <el-table-column
        label="历次融资"
        align="center"
        prop="previousFinancing"
      />
      <el-table-column
        label="项目融资阶段"
        align="center"
        prop="financingStage"
      />
      <el-table-column
        label="预期融资金额"
        align="center"
        prop="financingMoney"
      />
      <el-table-column
        label="预期融资时间"
        align="center"
        prop="financingDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.financingDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="投前估值" align="center" prop="investValuation" />
      <el-table-column label="预计投资金额" align="center" prop="investMoney" />
      <el-table-column label="投资类型" align="center" prop="investType" />
      <el-table-column label="投资策略" align="center" prop="investPloy" />
      <el-table-column label="投资价值" align="center" prop="investWorth" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="100"
      />
      <el-table-column
        width="300"
        fixed="right"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- stage//0=全部 1=项目报备  2=评估考察 3=项目立项 4=尽职背调 5=项目投决 -->
          <el-button
            v-if="stage == '1'"
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handlefollowRecord(scope.row)"
            >上传跟进记录</el-button
          >
          <el-button
            v-if="stage == '3'"
            size="mini"
            type="text"
            icon="el-icon-chat-line-round"
            @click="handleMeeting(scope.row, 'LX')"
            >发起立项会议</el-button
          >
         
          <el-button
            v-if="stage == '4'"
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleSurvey(scope.row)"
            >发起尽调申请</el-button
          >
          <el-button
            v-if="stage == '5'"
            size="mini"
            type="text"
            icon="el-icon-chat-line-square"
            @click="handleMeeting(scope.row, 'TJ')"
            >发起投决会议</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleDetail(scope.row)"
            v-hasPermi="['invest:pool:query']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['invest:pool:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="handleBusinessUpdate(scope.row)"
            >设置项目阶段</el-button
          >

          <el-button
            size="mini"
            type="text"
            icon="el-icon-switch-button"
            @click="handleDelete(scope.row)"
            v-hasPermi="['invest:pool:remove']"
            >终止</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改文件资料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目阶段" prop="projectStage">
          <el-select v-model="form.projectStage" placeholder="请选择项目阶段">
            <el-option
              @click.native="handleProjectSelect(index, dict)"
              v-for="(dict, index) in dict.type.project_stage"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目状态" prop="projectState">
          <el-select v-model="form.projectState" placeholder="请选择项目状态">
            <el-option
              v-for="dict in project_state"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPool,
  getPool,
  delPool,
  addPool,
  updatePool,
  editStage,
} from "@/api/invest/pool";

import { listChannel } from "@/api/invest/channel";
export default {
  props: {
    type: {
      type: String,
      default: "1", //1=显示全部列表  2=显示某项目下的详情列表
    },
    stage: {
      type: String,
      default: "0", //0=全部 1=项目报备  2=评估考察 3=项目立项 4=尽职背调 5=项目投决
    },
    projectId: {
      type: String,
    },
  },
  dicts: ["project_group", "project_stage", "project_state"],
  data() {
    return {
      project_state: [],
      projectState: [
        [
          {
            dictValue: "a",
            dictLabel: "项目报备完成",
          },
        ],
        [
          {
            dictValue: "b",
            dictLabel: "评估考察中",
          },
        ],
        [
          {
            dictValue: "c",
            dictLabel: "准备立项材料",
          },
          {
            dictValue: "d",
            dictLabel: "项目立项",
          },
          {
            dictValue: "e",
            dictLabel: "立项通过",
          },
        ],
        [
          {
            dictValue: "f",
            dictLabel: "尽调背调",
          },
          {
            dictValue: "g",
            dictLabel: "尽调背调通过",
          },
        ],
        [
          {
            dictValue: "h",
            dictLabel: "准备项目投决材料",
          },
          {
            dictValue: "i",
            dictLabel: "项目投决通过",
          },
        ],
        [
          {
            dictValue: "j",
            dictLabel: "签约协议沟通中",
          },
          {
            dictValue: "k",
            dictLabel: "协议签约完成",
          },
        ],
        [
          {
            dictValue: "l",
            dictLabel: "投后管理",
          },
          {
            dictValue: "m",
            dictLabel: "准备投后退出材料",
          },
          {
            dictValue: "n",
            dictLabel: "项目退出完成",
          },
        ],
      ],
      rules: {
        projectStage: [
          { required: true, trigger: "change", message: "请选择项目阶段" },
        ],
        projectState: [
          { required: true, trigger: "change", message: "请选择项目状态" },
        ],
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目池表格数据
      poolList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        id: null,
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        projectGroup: null,
        projectCode: null,
        channel: null,
        contactDate: null,
        projectDate: null,
        decisionDate: null,
        industry: null,
        company: null,
        projectContacts: null,
        investHead: null,
        previousFinancing: null,
        financingStage: null,
        financingMoney: null,
        financingDate: null,
        investValuation: null,
        investMoney: null,
        investType: null,
        investPloy: null,
        investWorth: null,
        projectStage: null,
        projectState: null,
      },
      form: {
        id: null,
        projectStage: "",
        projectState: "",
        stageName: "",
      },
      channelList: [],
    };
  },
  mounted() {
    // 项目阶段
    if (this.stage != "0") {
      this.queryParams.projectStage = this.stage;
    }
    if (this.projectId) {
      this.queryParams.id = this.projectId;
    }

    this.getList();
    // 渠道
    listChannel({
      pageNum: 1,
      pageSize: 100,
    }).then((response) => {
      this.channelList = response.rows;
    });
  },
  activated() {
    // 项目阶段
    if (this.stage != "0") {
      this.queryParams.projectStage = this.stage;
    }
    this.getList();
  },
  methods: {
    // 下拉框改变
    handleProjectSelect(index, dict) {
      this.form.projectState = null;
      this.project_state = this.projectState[index];
      if (dict && dict.label) {
        this.form.stageName = dict.label;
      }
    },

    /** 查询项目池列表 */
    getList() {
      this.loading = true;
      listPool(this.queryParams).then((response) => {
        this.poolList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: "/invest/pool/add" });
    },
    handleDetail(row) {
      const id = row.id || this.ids;
      this.$router.push({ path: "/invest/pool/detail", query: { id: id } });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids;
      this.$router.push({ path: "/invest/pool/add", query: { id: id } });
    },
    // 修改业务节点
    handleBusinessUpdate(row) {
      this.reset();
      let index = parseInt(row.projectStage) - 1;
      this.handleProjectSelect(index, row);
      this.form = row;
      this.open = true;
      this.title = "设置项目阶段";
    },
    // 发起会议
    handleMeeting(row, meetingType) {
      this.$store.commit("SET_PROJECTITEMMESSAGE", row);
      this.$router.push({
        path: "/resource/meeting",
        query: { id: row.id, type: "1", meetingType: meetingType },
      });
    },
    // 发起尽调申请
    handleSurvey(row) {},

    // 上传跟进记录
    handlefollowRecord(row) {
      this.$store.commit("SET_PROJECTITEMMESSAGE", row);
      this.$router.push({
        path: "/resource/record",
        query: { id: row.id, type: "1" },
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectStage: null,
        projectState: null,
        stageName: null,
      };
      this.resetForm("form");
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否终止"' + row.projectName + '"项目？')
        .then(function () {
          return delPool(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("已终止");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "invest/pool/export",
        {
          ...this.queryParams,
        },
        `pool_${new Date().getTime()}.xlsx`
      );
    },
    /** 提交按钮 */
    submitForm() {
      let that = this;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          editStage(this.form).then((response) => {
            that.$modal.msgSuccess("修改成功");
            that.open = false;
            that.getList();
          });
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
  },
};
</script>
