<template>
  <div class="project-list">
    <div class="search-bar">
      <el-form
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
        size="small"
      >
        <el-form-item label="任务名称" class="form-item">
          <el-input v-model="searchForm.name" placeholder="任务名称"></el-input>
        </el-form-item>
        <el-form-item label="任务状态" class="form-item">
          <el-select
            v-model="searchForm.projectState"
            placeholder="状态"
            clearable
          >
            <el-option
              v-for="item in statusList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="search-buttons">
          <el-button type="primary" @click="onSearch">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-actions mb10">
      <el-button type="primary" size="mini" @click="addTask"
        >+ 新增任务</el-button
      >
    </div>
    <div class="f1 df">
      <CustomTable
        :columns="columns"
        :tableData="tableData"
        :total="total"
        :show-selection="false"
        :show-index="true"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        tableHeight="495px"
      >
        <template slot="operation" slot-scope="{ row }">
          <div class="operation-buttons">
            <el-button
              @click="editTask(row)"
              type="text"
              size="mini"
              icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="mini"
              icon="el-icon-setting"
              @click="setTask(row)"
              >指标配置</el-button
            >
            <el-button
              type="text"
              size="mini"
              icon="el-icon-delete"
              @click="delTask(row)"
              >删除</el-button
            >
          </div>
        </template>
      </CustomTable>
    </div>
    <SelectUser
      :dialogVisible="userSelectDialogVisible"
      :currentSelectedUser="currentSelectedUser"
      :showSelection="true"
      :highligt="false"
      @confirm="handleUserConfirm"
      @close="handleUserClose"
    />
    <el-dialog
      :title="isEdit ? '编辑考核任务' : '新增考核任务'"
      :visible.sync="dialogVisible1"
      width="25%"
    >
      <div>
        <el-form
          :model="taskData"
          size="small"
          ref="taskFormRef"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="任务名称" class="form-item" prop="name">
            <el-input
              v-model="taskData.name"
              placeholder="0/20"
              style="width: 90%"
            ></el-input>
          </el-form-item>
          <el-form-item label="考核人员" class="form-item" prop="userName">
            <el-input
              v-model="taskData.userName"
              placeholder="考核人员"
              readonly
              style="width: 90%"
              @click.native="openUserSelectDialog"
              ><el-button slot="append" icon="el-icon-s-custom"></el-button
            ></el-input>
          </el-form-item>
          <el-form-item label="截止时间" class="form-item" prop="time">
            <el-date-picker
              v-model="taskData.time"
              type="date"
              style="width: 90%"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="saveTask">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="配置指标和权重" :visible.sync="dialogVisible2">
      <div class="modal">
        <div class="left">
          <div class="setText">累计权重</div>
          <el-collapse v-model="letfValue" :accordion="true" style="height: 300px;">
            <el-collapse-item
              v-for="(item, index) in scoreList"
              :key="index"
              :name="item.name"
            >
              <template #title>
                <div class="jcsb flex-row contentTitle">
                  <span class="setTitle">{{ item.title }}</span>
                  <span class="statusText">{{ item.score }}%</span>
                </div>
              </template>
              <div>
                <div
                  v-for="(item, index) in item.list"
                  :key="index"
                  class="flex-row jcsb leftSub"
                >
                  <div>{{ item.name }}</div>
                  <div class="statusText">{{ item.score }}%</div>
                </div>
              </div>
            </el-collapse-item>
          </el-collapse>
          <div class="flex-row jcsb" style="margin-top: 20px;padding-right: 15px;">
            <div class="setTitle">总计</div>
            <div class="statusText">
              {{
                scoreList.reduce((total, ele) => total + (ele.score || 0), 0)
              }}%
            </div>
          </div>
        </div>
        <div class="right">
          <div class="flex-row jcsb" style="margin-bottom: 10px">
            <div style="width: 50%">指标</div>
            <div class="center" style="width: 50%">权重占比</div>
          </div>
          <div>
            <div
              v-for="(item, index) in (
                scoreList.find((ele) => ele.name == letfValue) || {}
              ).list"
              :key="index"
              style="margin-bottom: 10px"
              class="flex-row jcsb aic"
            >
              <div style="width: 50%">{{ item.name }}</div>
              <div class="center" style="width: 50%">
                <el-slider v-model="item.score" :max="20"></el-slider>
                <div class="flex-row jcsb scoreBox" style="margin-left: 15px">
                  <div>0%</div>
                  <div class="scoreText">{{ item.score }}%</div>
                  <div>20%</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import CustomTable from "@/components/CustomTable.vue";
import SelectUser from "@/components/SelectUser.vue";
import { projectApi, systemApi } from "@/utils/api";

export default {
  components: {
    CustomTable,
    SelectUser,
  },
  data() {
    return {
      searchForm: {
        name: "",
        status: "",
      },
      columns: [
        { prop: "projectName", label: "任务名称" },
        { prop: "projectCode", label: "考核人数" },
        {
          prop: "projectState",
          label: "任务状态",
          type: "status",
          callback: (value) => {
            if (value == 1) var color = "#333";
            else var color = "#1686d68";

            return `<span style="color: ${color}">${value}</span>`;
          },
        },
        { prop: "projectCode", label: "创建时间" },
        { prop: "projectCode", label: "截至时间" },

        {
          prop: "operation",
          label: "操作",
          width: "250",
          className: "operation-column",
        },
      ],
      tableData: [],
      total: 0,
      userSelectDialogVisible: false,
      currentSelectedUser: [],
      pageNum: 1, // 当前页码
      pageSize: 10, // 每页条数
      statusList: [
        { label: "全部", value: "" },
        { label: "待评分", value: "0" },
        { label: "已完成", value: "1" },
      ],
      dialogVisible1: false,
      isEdit: false,
      taskData: {
        name: "",
        time: "",
        userName: "",
        userId: [],
      },
      rules: {
        name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 1, max: 20, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        userName: [
          { required: true, message: "请选择考核人员", trigger: "blur" },
        ],
        time: [{ required: true, message: "请选择截止时间", trigger: "blur" }],
      },
      dialogVisible2: false,
      letfValue: "",
      scoreList: [
        {
          name: "progress",
          title: "项目进度符合度",
          score: 0,
          list: [{ name: "1111" }, { name: "22" }],
        },
        {
          name: "achievement",
          title: "项目成果达成度",
          score: 0,
          list: [{ name: "33" }, { name: "44" }],
        },
        {
          name: "documentation",
          title: "项目文档完整性",
          score: 0,
          list: [{ name: "55" }, { name: "66" }],
        },
        {
          name: "innovation",
          title: "技术创新点及影响力",
          score: 0,
          list: [{ name: "77" }, { name: "88" }],
        },
      ],
    };
  },
  watch: {
    scoreList: {
      handler(newVal, oldVal) {
        this.scoreList.forEach((ele) => {
          ele.score = ele.list.reduce(
            (total, ele) => total + (ele.score || 0),
            0
          );
        });
      },
      deep: true,
    },
  },
  methods: {
    onSearch() {
      this.fetchProjectList();
    },
    onReset() {
      Object.keys(this.searchForm).forEach((key) => {
        this.searchForm[key] = "";
      });
      this.fetchProjectList();
    },
    fetchProjectList() {
      projectApi
        .listProject({
          ...this.searchForm,
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        })
        .then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
        });
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.pageNum = 1; // 重置为第一页
      this.fetchProjectList();
    },
    handleCurrentChange(page) {
      this.pageNum = page;
      this.fetchProjectList();
    },
    openUserSelectDialog() {
      this.userSelectDialogVisible = true;
    },
    handleUserConfirm(data) {
      this.taskData.userName = data.map((ele) => ele.nickName);
      this.taskData.userId = data.map((ele) => ele.userId);
    },
    handleUserClose() {
      this.userSelectDialogVisible = false;
    },
    addTask() {
      this.isEdit = false;
      this.dialogVisible1 = true;
    },
    editTask(row) {
      this.isEdit = true;
      this.dialogVisible1 = true;
      this.taskData = row;
    },
    setTask(row) {
      this.dialogVisible2 = true;
    },
    delTask() {
      this.$confirm(
        "删除任务及绩效数据，该操作不可逆，请谨慎操作",
        "确认删除任务",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      });
    },
    saveTask() {
      this.$refs.taskFormRef.validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  mounted() {
    this.fetchProjectList();
  },
};
</script>

<style lang="scss" scoped>
.project-list {
  padding: 20px;
  background-color: white;
  height: 88vh;
  box-sizing: border-box;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.search-bar {
  margin-bottom: 20px;
}

.demo-form-inline {
  // display: flex;
  // flex-wrap: nowrap;
  // align-items: flex-start;
}

.demo-form-inline .el-form-item {
  // margin-right: 50px; /* 将间距设置为 30px */
  margin-bottom: 0;
}

.demo-form-inline .el-form-item:last-child {
  margin-right: 0; /* 移除最后一个元素的右边距 */
}

.form-item {
  flex: 1;
}

.form-item ::v-deep .el-form-item__content {
  // width: 100%;
}

.form-item ::v-deep .el-input,
.form-item ::v-deep .el-select {
  // width: 100%;
}

.search-buttons {
  white-space: nowrap;
}

::v-deep .operation-buttons .el-button {
  padding: 4px 8px;
  margin: 0 2px;
}

::v-deep .operation-column {
  background-color: #ffffff;
  box-shadow: -2px 0 5px rgba(241, 112, 6, 0.1);
}

.el-button.is-text {
  min-width: 32px !important;
}

.dialog-footer {
  display: flex;
  justify-content: center;
  align-items: center;
}

.dialog-footer .el-button {
  margin: 0 10px;
}

.delete-content {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.warning-icon {
  font-size: 24px;
  color: #e6a23c;
  margin-right: 10px;
}

/* 添加以下样式来使对话框垂直居中 */
::v-deep .delete-dialog.el-dialog {
  margin-top: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
::v-deep .el-table th {
  text-align: center;
}

::v-deep .el-table .cell {
  text-align: center;
}
::v-deep .el-dialog {
  margin-top: 10% !important;
}
.modal {
  display: flex;
  border-bottom: 1px solid #ccc;
  border-top: 1px solid #ccc;

}
.left {
  width: 40%;
  padding: 20px;
  border-right: 1px solid #ccc;
  height: 450px;
  overflow: auto;
}
.right {
  width: 60%;
  padding: 20px 40px;
  height: 450px;
  overflow: auto;
  div.center {
    text-align: center;
    position: relative;
  }
}
.scoreBox {
  position: absolute;
  top: 45px;
  right: -20px;
}
::v-deep .el-slider__runway {
  height: 20px;
  border-radius: 20px;
  margin: 15px !important;
  /* width: 95%; */
}
::v-deep .el-slider__bar {
  height: 20px;
  border-radius: 20px;
  background-color: #ff5722;
}
::v-deep .el-slider__button {
  display: none;
}
.scoreText {
  color: #1686d8;
}
.setText {
  margin-bottom: 20px;

  text-align: right;
}
.leftSub {
  padding-right: 20px;
  margin-bottom: 5px;
}
.setTitle {
  font-weight: bold;
}
::v-deep .el-collapse-item__content {
  padding-bottom: 10px;
}
::v-deep .el-collapse-item__header {
}
.contentTitle {
}
.statusText {
  color: #ff5722;
}
::v-deep .el-collapse{
    border: none !important;
}
</style>
