<template>
  <div class="box">
    <div style="width: 200%">
      <el-card class="elcard" style="border-radius: 15px">
        <div class="publiccss">项目默认结果</div>
        <el-row style="margin-top: 20px">
          <el-col :span="4">
            <el-input
              v-model="pages.Filter"
              @clear="getlist"
              clearable
            ></el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="" style="margin-left: 5%;background:rgb(203,240,209);color:rgb(31, 148, 96)" @click="getlist"
              >查询</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="tableData"
          style="width: 100%; height: 500px;margin-top:20px"
          row-key="id"
          class="el-table__body-wrapper tbody"
          @row-click="rowick"
          highlight-current-row
        >
          <el-table-column prop="id" label="编号" width="300">
          </el-table-column>
          <el-table-column prop="displayName" label="名称" width="">
          </el-table-column>
          <el-table-column prop="creatorName" label="创建者" width="">
          </el-table-column>
          <el-table-column prop="creationTime" label="创建时间" width="200">
            <template slot-scope="scope">
              {{ scope.row.creationTime | dateFormat }}
            </template>
          </el-table-column>
          <el-table-column prop="lastModifierName" label="修改者" width="">
          </el-table-column>
          <el-table-column
            prop="lastModificationTime"
            label="修改时间"
            width="200"
          >
            <template slot-scope="scope">
              {{ scope.row.lastModificationTime | dateFormat }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template>
              <el-tag
                class="move"
                style="cursor: move; margin-left: 15px;background-color: rgb(245, 245, 245);border:none"
                draggable="true"
              >
                <i
                  class="el-icon-d-caret"
                  style="width: 1rem; height: 1rem;color: rgb(113, 113, 113)"
                ></i>
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
        <!-- 新增或者编辑弹框 -->
        <el-dialog
          :title="title == 1 ? '新增' : '编辑'"
          :visible.sync="dialogVisible"
          width="75%"
        >
          <el-form ref="form" :model="form" label-width="80px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="编号">
                  <el-input
                    v-model="form.id"
                    style="width: 70%"
                    disabled
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="名称">
                  <el-input
                    ref="refinput"
                    v-model="form.displayName"
                    style="width: 65%"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-divider></el-divider>
              <el-col :span="4" style="margin-left: 15px">
                <el-form-item label="创建者">
                  <el-input v-model="form.creatorName" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="创建时间">
                  <el-input
                    :value="form.creationTime | dateFormat"
                    disabled
                    style="width: 94%"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="4" style="margin-left: 0%">
                <el-form-item label="修改者">
                  <el-input v-model="form.lastModifierName" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item label="修改时间">
                  <el-input
                    :value="form.lastModificationTime | dateFormat"
                    disabled
                    style="width: 76%"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addoredit" class="bulletcommit">确 定</el-button>
          </span>
        </el-dialog>
      </el-card>
    </div>
    <!-- 按钮区域 -->
    <div style="margin-left: 10px; margin-top: 7%">
      <el-button type="" @click="add" class="commonbutton">新增</el-button>
      <el-button
        type=""
        @click="editpopup"
        style="margin-left: 0; margin-top: 10px"
        class="commonbutton"
        >编辑</el-button
      >
      <div style="margin-top: 10px">
        <el-button type="" @click="delsrts" class="commonbutton">删除</el-button>
      </div>
      <div style="margin-top: 10px">
        <el-button type="" @click="topping" class="commonbutton">置顶</el-button>
      </div>
      <div style="margin-top: 10px">
        <el-button type="" @click="toppings" class="commonbutton">置底</el-button>
      </div>
      <div style="margin-top: 10px">
        <el-button type="" :disabled="isshow" @click="assertion" class="commonbutton"
          >确定</el-button
        >
      </div>
      <div style="margin-top: 10px">
        <el-button type="" :disabled="isshow" @click="cancellation" class="commonbutton"
          >取消</el-button
        >
      </div>
    </div>
  </div>
</template>
<script>
import Sortable from "sortablejs";
import {
  projectresultlist,
  projecresutid,
  addprojecreuitd,
  editprojectresult,
  deletepojectresult,
  projecrselttopbottom,
  projectrultsdeg,
} from "../../request/systemapi";
export default {
  data() {
    return {
      isshow: true,
      pages: {
        Filter: "",
        SkipCount: 0,
        MaxResultCount: 100,
        Sorting: "displayOrder desc",
      },
      tableData: [],
      dialogVisible: false,
      title: 1,
      initTableData: [],
      form: {
        displayName: "",
      },
      curRow:{}
    };
  },
  created() {
    // this.form.lastModificationTime=this.tableData[]
    this.getlist();
  },
  mounted() {
    this.rowDrop();
  },
  watch: {
    foremtime() {
      return;
    },
  },
  methods: {
    //取消排序
    cancellation() {
      this.$message.info("取消操作");
      this.isshow = true;
      this.getlist();
    },
    //确定拖拽
    assertion() {
      const result = [];
      this.tableData.forEach((item, index) => {
        // index 从0开始的， displayOrder从大到小排
        console.log(item.id);
        // const currentDisplayOrder = this.tableData.length -1
        const currentDisplayOrder = this.initTableData[index].displayOrder;
        if (item.displayOrder != currentDisplayOrder) {
          // 如果它的displayOrder和它当前所在的位置不同代表挪动过位置
          result.push({ id: item.id, displayOrder: currentDisplayOrder });
        }
      });
      projectrultsdeg({ itemList: result }).then((res) => {
        this.$message.success("操作成功");
        this.isshow = true;
        this.getlist();
      });
    },
    //初始化
    rowDrop() {
      this.$nextTick(() => {
        const tbody = document.querySelector(".el-table__body-wrapper tbody");
        const _this = this;
        Sortable.create(tbody, {
          handle: ".move",
          animation: 300,
          onEnd({ newIndex, oldIndex }) {
            _this.isshow = false;
            const currRow = _this.tableData.splice(oldIndex, 1)[0];
            _this.tableData.splice(newIndex, 0, currRow);
            _this.tableData.map((item, index) => {
              if (index == newIndex && index == oldIndex) {
                // console.log(item, "新数据");
              } else if (index == oldIndex) {
              } else if (index == newIndex) {
              }
            });
            console.log(_this.tableData.map((item) => item.displayOrder));
          },
        });
      });
    },
    //置低
    toppings() {
      this.form={...this.curRow}
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        projecrselttopbottom(this.form.id, 2).then((res) => {
          this.$message.success("操作成功");
          this.getlist();
        });
      }
    },
    //置顶
    topping() {
       this.form={...this.curRow}
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        projecrselttopbottom(this.form.id, 1).then((res) => {
          this.$message.success("操作成功");
          this.getlist();
        });
      }
    },
    //删除
    delsrts() {
       this.form={...this.curRow}
      if (this.form.id == undefined) {
        this.$message.warning("请选择删除的数据");
      } else {
        this.$confirm("是否确认删除,是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            deletepojectresult(this.form.id).then((res) => {
              this.$message.success("删除成功");
              this.getlist();
              this.form = {};
            });
          })
          .catch(() => {});
        // deletepojectresult(this.form.id).then((res) => {
        //   this.$message.success("删除成功");
        //   this.form = {};
        //   this.getlist();
        // });
      }
    },
    //编辑弹框
    editpopup() {
       this.form={...this.curRow}
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        this.dialogVisible = true;
        this.title = 2;
        projecresutid(this.form.id).then((res) => {
          console.log(res);
          this.form = res.data;
          // this.form.lastModificationTime={{res.data.lastModificationTime | a}}
        });
      }
    },
    //确定新增或者编辑
    addoredit() {
      if (this.form.displayName == undefined) {
        this.$message.warning("请输入名称");
      } else {
        if (this.title == 1) {
          addprojecreuitd({ displayName: this.form.displayName }).then(
            (res) => {
              this.$message.success("新增成功");
              this.getlist();
              this.dialogVisible = false;
            }
          );
        } else if (this.title == 2) {
          console.log(this.form.displayName);
          if (this.form.displayName == "") {
            this.$message.warning("请输入名称");
          } else {
            editprojectresult(this.form.id, {
              displayName: this.form.displayName,
            }).then((res) => {
              this.$message.success("修改成功");
              this.getlist();
              this.dialogVisible = false;
            });
          }
        }
      }
    },
    //新增弹框
    add() {
      this.title = 1;
      this.dialogVisible = true;
      this.form = {};
      this.$nextTick(()=>{
        this.$refs.refinput.focus()
      })
    },
    //点击列表通过id查询
    rowick(row) {
      projecresutid(row.id).then((res) => {
        // console.log(res);
        // this.form = res.data;
        this.curRow={...res.data}
      });
      console.log(row);
    },
    getlist() {
      projectresultlist(this.pages).then((res) => {
        this.initTableData = [...res.data.items];
        this.tableData = res.data.items;
        console.log(res);
      });
    },
  },
};
</script>
<style scoped>
::v-deep .el-table__header th {
  /* font-size: px; */
  background-color: rgb(245, 245, 245); /* 设置表头背景颜色 */
  color: rgb(113, 113, 113); /* 设置表头文字颜色 */
}
::v-deep .el-input.is-disabled .el-input__inner {
  width: 88%;
}
.box {
  display: flex;
}
</style>
