<template>
  <div class="app-container">
    <el-form
      v-if="type !== '2'"
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="70px"
    >
      <el-form-item label="会议主题" prop="meetingTheme">
        <el-input
          v-model="queryParams.meetingTheme"
          placeholder="请输入会议主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会议编号" prop="meetingCode">
        <el-input
          v-model="queryParams.meetingCode"
          placeholder="请输入会议编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会议类别" prop="meetingType">
        <el-select
          v-model="queryParams.meetingType"
          placeholder="全部"
          clearable
        >
          <el-option
            v-for="dict in dict.type.meeting_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          style="width: 205px"
          clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择会议开始时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          style="width: 205px"
          clearable
          v-model="queryParams.endTime"
          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">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['invest:meeting: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:meeting:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['invest:meeting: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:meeting:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="meetingList"
      @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="meetingTheme" />
      <el-table-column
        label="项目名称"
        align="center"
        prop="tProjectPool.projectName"
      />
      <el-table-column label="会议编号" align="center" prop="meetingCode" />
      <el-table-column label="会议类别" align="center" prop="meetingType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.meeting_type"
            :value="scope.row.meetingType"
          />
        </template>
      </el-table-column>
      <el-table-column label="会议开始时间" align="center" prop="startTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会议结束时间" align="center" prop="endTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会议地点" align="center" prop="place" />
      <el-table-column label="会议发起人" align="center" prop="promoter" />
      <el-table-column label="会议参与人" align="center" prop="participants" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['invest:meeting:edit']"
            >修改</el-button
          >
          <el-button
            class="custom-red-color"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['invest:meeting:remove']"
            >删除</el-button
          >

          <el-dropdown
            trigger="click"
            @command="handleCommand($event, scope.row)"
          >
            <!-- class="custom-blue-color"  -->
            <span class="el-dropdown-link" @click="getButtonItem(scope.row)">
              <el-button size="mini" type="text" icon="el-icon-d-arrow-right"
                >更多</el-button
              >
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-if="!showLookMark && showMarkBtn"
                command="1"
                icon="el-icon-document-remove"
                @click="handleMark(scope.row, '1', false)"
                >去打分</el-dropdown-item
              >
              <el-dropdown-item
                v-if="showLookMark && showMarkBtn"
                icon="el-icon-search"
                command="2"
                @click="handleMark(scope.row, '1', true)"
                >查看打分表</el-dropdown-item
              >
              <el-dropdown-item
                v-if="isInitiator"
                icon="el-icon-search"
                command="3"
                @click="handleLookMark(scope.row, '2', true)"
                >查看打分汇总表</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-document-remove"
            @click="handleMark(scope.row, '1',false)"
            >去打分</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleMark(scope.row, '1',true)"
            >查看打分表</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleLookMark(scope.row, '2',true)"
            >查看打分汇总表</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="1000px"
      append-to-body
    >
      <el-form
        class="special-el-form"
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="会议主题" prop="meetingTheme">
          <el-input v-model="form.meetingTheme" placeholder="请输入会议主题" />
        </el-form-item>

        <!-- <el-form-item label="会议编号" prop="meetingCode">
          <el-input v-model="form.meetingCode" placeholder="请输入会议编号" />
        </el-form-item> -->
        <el-form-item label="会议类别" prop="meetingType">
          <el-select
            v-model="form.meetingType"
            placeholder="请选择会议类别"
            :disabled="pageType === '1' && meetingType != null"
          >
            <el-option
              v-for="dict in dict.type.meeting_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="项目名称"
          prop="projectName"
          :rules="[
            { required: verifyProjectName, trigger: 'blur', message: '请选择' },
          ]"
        >
          <div
            class="el-input__inner inputSimulation"
            @click="handleProjectItem"
            :class="{ show_disabled: pageType === '1' }"
          >
            {{ form.projectName ? form.projectName : "请选择 " }}
          </div>
          <projectItem
            ref="projectItem"
            @getProjectInfo="getProjectInfo"
          ></projectItem>
        </el-form-item>
        <el-form-item label="项目阶段" prop="projectStage">
          <el-select v-model="form.projectStage" disabled placeholder="">
            <el-option
              v-for="dict in dict.type.project_stage"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="组别" prop="projectGroup">
          <el-select v-model="form.projectGroup" disabled placeholder="">
            <el-option
              v-for="dict in dict.type.project_group"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="会议开始时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            placeholder="请选择会议开始时间"
          >
          </el-date-picker>
          <!-- <el-date-picker
            clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择会议开始时间"
          >
          </el-date-picker> -->
        </el-form-item>
        <el-form-item label="会议结束时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            placeholder="请选择会议结束时间"
          >
          </el-date-picker>
          <!-- <el-date-picker
            clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择会议结束时间"
          >
          </el-date-picker> -->
        </el-form-item>
        <el-form-item label="会议地点" prop="place">
          <el-input v-model="form.place" placeholder="请输入会议地点" />
        </el-form-item>
        <el-form-item label="会议发起人" prop="promoter">
          <div
            class="el-input__inner inputSimulation"
            @click="openSelectDept(1)"
          >
            {{ form.promoter ? form.promoter : "请选择会议发起人" }}
          </div>
          <selecDept
            ref="selecDepts"
            @getDeptUserInfo="getDeptUserInfo"
            :deptId="deptId"
          ></selecDept>
        </el-form-item>
        <el-form-item label="会议参与人" prop="participants">
          <div
            class="el-input__inner inputSimulation"
            @click="userVisible = true"
          >
            <!-- @click="openSelectDept(2)" -->
            {{ form.participants ? form.participants : "请选择会议参与人" }}
          </div>
          <!-- <selecDept
            ref="selecDepts1"
            @getDeptMoreUserInfo="getDeptMoreUserInfo"
            :deptId="deptId"
            :isSolo="false"
          ></selecDept> -->
        </el-form-item>
        <el-form-item label="附件" prop="file">
          <fileItem
            ref="fileItems"
            :id="form.id"
            @getFileList="getFileList"
          ></fileItem>
        </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>
    <!--选择人员-->
    <el-dialog
      title="选择人员"
      :visible.sync="userVisible"
      width="60%"
      :show-close="false"
    >
      <flow-user
        :checkType="checkType"
        :selectValues="selectValues"
        @handleUserSelect="handleUserSelect"
      ></flow-user>
      <span slot="footer" class="dialog-footer">
        <el-button @click="userVisible = false">取 消</el-button>
        <el-button type="primary" @click="checkUserComplete">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMeeting,
  getMeeting,
  delMeeting,
  addMeeting,
  updateMeeting,
  getCanScoringMeetingId,
} from "@/api/invest/meeting";
import projectItem from "./projectItem";
import fileItem from "./fileItem";
import selecDept from "./selecDept";
import FlowUser from "@/components/flow/User";

export default {
  props: {
    type: {
      type: String,
      default: "1", //1=显示全部列表  2=显示某项目下的详情列表
    },
    projectId: {
      type: String,
    },
  },
  dicts: ["meeting_type", "project_stage", "project_group"],
  components: { projectItem, selecDept, fileItem, FlowUser },
  watch: {
    "form.meetingType": function (newValue, oldValue) {
      // console.log(newValue,"this.form.meetingType", this.form.meetingType);
      if (this.form.meetingType == "LX" || this.form.meetingType == "TJ") {
        this.verifyProjectName = true;
      } else {
        this.verifyProjectName = false;
      }
    },
    open: function (newValue, oldValue) {
      if (newValue === false) {
        this.queryParams.projectPoolId = null;
        this.meetingType = null;
        this.pageType = null;
      }
    },
  },
  data() {
    return {
      // 人员选择器
      checkType: "multiple",
      // 数据回显
      selectValues: null,
      userVisible: false,
      pageType: null,
      meetingType: null,
      verifyProjectName: false,
      fileList: [],
      showProjectItem: false,
      deptId: "",
      // 遮罩层
      loading: true,

      // 选中数组
      ids: [],
      idsName: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会议记录表格数据
      meetingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        meetingTheme: null,
        meetingCode: null,
        meetingType: null,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      form: {
        id: null,
        meetingTheme: null,
        meetingCode: null,
        meetingType: null,
        projectPoolId: null,
        projectName: null,
        projectStage: null,
        projectGroup: null,
        startTime: null,
        endTime: null,
        place: null,
        promoter: null,
        promoterId: null,
        participants: null,
        participantsId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        projectGroup: null,
      },
      // 表单校验
      rules: {
        meetingTheme: [{ required: true, trigger: "blur", message: "请输入" }],
        meetingType: [{ required: true, trigger: "change", message: "请选择" }],
        // projectGroup: [{ required: true, trigger: "change", message: "请选择" }],
        // projectName: [{ required: true, trigger: "blur", message: "请选择" }],
        startTime: [{ required: true, trigger: "change", message: "请选择" }],
        endTime: [{ required: true, trigger: "blur", message: "请选择" }],
        place: [{ required: true, trigger: "blur", message: "请输入" }],
        promoter: [{ required: true, trigger: "blur", message: "请选择" }],
        participants: [{ required: true, trigger: "blur", message: "请选择" }],
      },
      user: {
        userId: null,
        nickName: null,
      },
      isInitiator: false, //是否是发起人
      showLookMark: false, //是否查看打分表
      showMarkBtn: false,
    };
  },
  created() {
    this.user = this.$store.getters.user;
    this.user.userId = this.user.userId.toString();
    // 查询该id下的列表信息
    if (this.$route.query.id) {
      this.queryParams.projectPoolId = this.$route.query.id;
    }
    // 会议类型
    if (this.$route.query.meetingType) {
      this.meetingType = this.$route.query.meetingType;
    }
    // 直接弹出新增
    if (this.$route.query.type === "1") {
      this.pageType = this.$route.query.type;
      this.handleAdd();
    }
    if (this.projectId) {
      this.queryParams.projectPoolId = this.projectId;
    }
    this.getList();
  },
  methods: {
    /*用户选中赋值*/
    checkUserComplete() {
      this.userVisible = false;
      this.checkType = "";
    },
    // 用户选中数据
    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.form.participants = nickName.join(",");
          this.form.participantsId = userIds.join(",");
        } else {
          this.form.participants = selection.nickName;
          this.form.participantsId = selection.userId;
        }
      }
    },
    getButtonItem(row) {
      this.showMarkBtn = false;
      // 是否是发起人
      this.isInitiator = row.promoterId == this.user.userId;
      // 参与人数组
      let participantsIdList = row.participantsId.split(",");
      // console.log("是否是发起人",this.isInitiator)
      // console.log("参与人数组",participantsIdList,"this.user.userId=",this.user.userId,participantsIdList.indexOf(this.user.userId) > -1)
      if (this.isInitiator) {
        // 参与人里面包含发起人
        if (participantsIdList.indexOf(this.user.userId) > -1) {
          this.showMarkBtn = true;
        }
      } else {
        this.showMarkBtn = true;
      }
      // console.log("this.showMarkBtn=",this.showMarkBtn)
      getCanScoringMeetingId(row.id).then((response) => {
        // 查看打分
        if (response.tProjectScoring) {
          this.showLookMark = true;
          this.$store.commit("SET_TPROJECTSCORING", response.tProjectScoring);
        } else {
          // 去打分
          this.showLookMark = false;
        }
      });
    },
    // getCanScoringMeetingId
    handleCommand(command, row) {
      // console.log("handleCommand", command);
      // console.log("row", row);
      let type = "1",
        readonly = true;
      if (command === "1") {
        readonly = false;
      }
      if (command === "3") {
        type = "2";
      }
      this.handleMark(row, type, readonly);
    },
    // 去打分
    handleMark(row, type, readonly) {
      console.log("000000000");

      this.$router.push({
        path: "/invest/meeting/mark",
        query: { id: row.id, formType: type, readonly: readonly },
      });
    },
    // 查看打分表
    handleLookMark(row, type) {
      this.$router.push({
        path: "/invest/meeting/mark",
        query: { id: row.id, formType: type },
      });
    },
    // 选择部门人
    openSelectDept(type) {
      if (type == 2) {
        // this.$refs.selecDepts1.show(type);
      } else {
        this.$refs.selecDepts.show(type);
      }
    },
    getDeptUserInfo(info) {
      this.form.promoter = info[0].nickName;
      this.form.promoterId = info[0].userId.toString();
    },
    // 多选人
    getDeptMoreUserInfo(info) {
      let peoList = [],
        peoIdList = [];
      for (let i in info) {
        peoList.push(info[i].nickName);
        peoIdList.push(info[i].userId.toString());
      }
      let peopleStr = peoList.join(",");
      let peopleIdStr = peoIdList.join(",");
      this.form.participants = peopleStr;
      this.form.participantsId = peopleIdStr;
    },
    // 获取fileList
    getFileList(fileList) {
      this.fileList = fileList;
    },
    // 获取公司信息
    getProjectInfo(info) {
      this.form.projectPoolId = info[0].id;
      this.form.projectName = info[0].projectName;
      this.form.projectStage = info[0].projectStage;
      this.form.projectGroup = info[0].projectGroup;
      this.form.projectState = info[0].projectState;
    },
    //展示人员
    handleProjectItem() {
      this.$refs.projectItem.showProjectItem = true;
    },
    /** 查询会议记录列表 */
    getList() {
      this.loading = true;
      listMeeting(this.queryParams).then((response) => {
        this.meetingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        meetingTheme: null,
        meetingCode: null,
        meetingType: null,
        projectPoolId: null,
        projectName: null,
        projectStage: null,
        projectGroup: null,
        startTime: null,
        endTime: null,
        place: null,
        promoter: null,
        promoterId: null,
        participants: null,
        participantsId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        projectGroup: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.idsName = selection.map((item) => item.meetingTheme);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(projectId) {
      let that = this;
      this.reset();
      if (projectId && typeof projectId === "string") {
        this.pageType = "1";
      }
      // 直接绑定好项目
      if (this.pageType === "1") {
        let projectItemMessage = this.$store.getters.projectItemMessage;
        this.form.projectPoolId = projectItemMessage.id;
        this.form.projectName = projectItemMessage.projectName;
        this.form.projectStage = projectItemMessage.projectStage;
        this.form.meetingType = this.meetingType;
      }
      this.open = true;
      this.title = "添加会议";
      setTimeout(() => {
        that.$refs.fileItems.fileList = [];
      }, 200);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMeeting(id).then((response) => {
        this.form = response.data;
        this.form.projectName = response.data.tProjectPool.projectName;
        this.form.projectStage = response.data.tProjectPool.projectStage;
        this.form.projectGroup = response.data.tProjectPool.projectGroup;
        this.open = true;
        this.title = "修改会议";
        setTimeout(() => {
          this.$refs.fileItems.getListFileBusinessId(id);
        }, 200);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (
            new Date(this.form.endTime).getTime() <
            new Date(this.form.startTime).getTime()
          ) {
            this.$confirm("会议结束时间不能小于开始时间,请重新选择!", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {})
              .catch(() => {});
            return false;
          }
          this.form.listFile = this.fileList;
          if (this.form.id != null) {
            updateMeeting(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMeeting(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const idsName = row.meetingTheme ? row.meetingTheme : this.idsName;
      this.$modal
        .confirm('是否确认删除"' + idsName + '"？')
        .then(function () {
          return delMeeting(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "invest/meeting/export",
        {
          ...this.queryParams,
        },
        `meeting_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
