<template>
  <div>
    <!-- 选择项目 -->
    <el-dialog
      title="选择项目"
      :visible.sync="showProjectItem"
      width="800px"
      append-to-body
    >
      <!-- 查询表单区域 -->
      <el-form :model="projectQueryParams" ref="queryForm" inline>
        <el-form-item label="项目名称" prop="projectName">
          <el-input
            v-model.trim="projectQueryParams.projectName"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </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" type="primary" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        class="tableWrapper"
        @row-click="clickRow"
        ref="dataTable"
        :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="项目名称" width="150" align="center" prop="projectName">
          <template slot-scope="scope">
            <div :title="scope.row.projectName">
              {{ scope.row.projectName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="渠道"
          align="center"
          prop="tProjectChannel.channelName"
        >
          <template slot-scope="scope">
            <div :title="scope.row.tProjectChannel.channelName" v-if="scope.row.tProjectChannel && scope.row.tProjectChannel.channelName">
              {{ scope.row.tProjectChannel.channelName }}
            </div>
            <div v-else-if="scope.row.channel=='1'" >
              直接触达
            </div>
            <div v-else="scope.row.channel=='2'" >
              内部推荐
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="所属组别"
          align="center"
          prop="tProjectChannel.channelGroup"
        >
          <template slot-scope="scope">
            <dict-tag
              v-if="scope.row.tProjectChannel && scope.row.tProjectChannel.channelGroup"
              :options="dict.type.project_group"
              :value="scope.row.tProjectChannel.channelGroup"
            />
          </template>
        </el-table-column>
        <!-- <el-table-column label="项目编号" align="center" prop="projectCode" /> -->

        <el-table-column label="项目负责人" align="center" prop="investHead" />
        <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">
          <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="projectStatus">
          <template slot-scope="scope">
            <!-- 定义状态文本映射对象 -->
            <el-tag
              :type="{
              0: 'success',
              1: 'danger',
              2: 'warning'
            }[scope.row.projectStatus] || 'info'"
            >
              {{ {
              0: '正常',
              1: '终止',
              2: '观望'
            }[scope.row.projectStatus] || '未知状态' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="已发起尽调申请"
          align="center"
          prop="investigateFlag"
        >
          <template slot-scope="scope">
            <div>
              {{ scope.row.investigateFlag === "1" ? "是" : "否" }}
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="projectItemTotal > 0"
        :total="projectItemTotal"
        :page.sync="projectQueryParams.pageNum"
        :limit.sync="projectQueryParams.pageSize"
        @pagination="getList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit" v-preventReClick
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { applicableListInvestigate } from "@/api/project/investigate/pool";
import { mapGetters } from "vuex";
export default {
  props: {
    // showProjectItem: {
    //   type: Boolean,
    //   default: false,
    // },
  },
  dicts: ["project_group", "project_stage", "project_state"],
  data() {
    return {
      showProjectItem: false,
      // 总条数
      projectItemTotal: 0,
      poolList: [],
      projectQueryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "createTime",
        isAsc: "desc",
        projectStatus:null,
        projectName:null
      },

      // 选中数组
      ids: [],
      idsName: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      isWatch:false,
    };
  },
   computed: {
    ...mapGetters(["user"]),
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 刷新项目列表的方法
    refreshProjectList(isWatch) {
      // 清空现有列表
      this.poolList = [];
      this.projectItemTotal=0;
      // 重新请求接口获取最新数据
      this.getList(isWatch);
      this.isWatch=isWatch;
    },
    clickRow(row) {
      this.$refs.dataTable.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (selection.length > 1) {
        //移除上一次选中行数据
        selection.shift();
        //修改选中图标为未选中状态
        this.$refs.dataTable.clearSelection();
        //将当前选中行改为选中状态
        this.$refs.dataTable.toggleRowSelection(selection[0]);
      }
      this.ids = selection;
    },
    /** 查询项目池列表 */
    getList(isWatch) {
      if(isWatch){
        this.projectQueryParams.projectStatus=2;
      }else {
        this.projectQueryParams.projectStatus=0;
      }
      applicableListInvestigate(this.projectQueryParams).then((response) => {
        this.poolList = response.rows;
        this.projectItemTotal = response.total;
      });
    },
    submit() {
      // console.log("确定", this.ids);
      const row = this.ids[0];
        if (row.delFlag == "1") {
          this.$message({
            message: "项目已终止",
            duration: 1500,
            type: "error",
          });
        }
        if (row.investHead === this.user.nickName){
           if (row.investigateFlag == "0") {
              this.$emit("getProjectInfo", this.ids);
              this.showProjectItem = false;
            } else {
              this.$message({
                message: "您已发起尽调申请,无需重复操作",
                duration: 1500,
                type: "warning",
              });
            }
        }else {
          this.$message({
            message: "无权限",
            duration: 1500,
            type: "error",
          });
        }

    },
    cancel() {
      this.showProjectItem = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.projectQueryParams.pageNum = 1;
      this.getList(this.isWatch);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
