<template>
  <div class="box">
    <div style="width: 95%">
      <el-card>
        <el-table
          :data="tableData"
          style="width: 100%"
          row-key="id"
          class="el-table__body-wrapper tbody"
          border:stripe="true"
          @row-click="rowick"
          highlight-current-row
        >
          <el-table-column prop="id" label="编号" width=""> </el-table-column>
          <el-table-column prop="displayName" label="名称" width="">
          </el-table-column>
          <!-- <el-table-column prop="sexId" label="性别" width="">
        </el-table-column> -->
          <el-table-column prop="simpleCode" label="快捷码" width="">
          </el-table-column>
          <el-table-column label="操作" width="400">
            <template>
              <el-tag
                class="move"
                style="cursor: move; margin-left: 15px"
                draggable="true"
              >
                <i
                  class="el-icon-d-caret"
                  style="width: 1rem; height: 1rem"
                ></i>
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
        <div class="cancelorconfirm"></div>
        <!--  -->
        <el-dialog
          title="修改"
          :visible.sync="dialogVisible"
          width="40%"
          :before-close="handleClose"
        >
          <el-form ref="form" :model="form">
            <el-row>
              <el-col :span="12">
                <el-form-item label="性别" label-width="80px">
                  <el-input
                    v-model="form.displayName"
                    style="width: 80%"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="快捷码" label-width="80px">
                  <el-input
                    v-model="form.simpleCode"
                    style="width: 80%"
                  ></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="confirmmodification"
              >确 定</el-button
            >
          </span>
        </el-dialog>
      </el-card>
    </div>
    <!-- 按钮区域 -->
    <div style="margin-left: 10px; margin-top: 5%">
      <el-button type="primary" @click="eddte">编辑</el-button>
      <div style="margin-top: 10px">
        <el-button type="primary" @click="topping">置顶</el-button>
      </div>
      <div style="margin-top: 10px">
        <el-button type="primary" @click="setlow">置底</el-button>
      </div>
      <div style="margin-top: 10px">
        <el-button type="primary" :disabled="isshow" @click="assertion"
          >排序</el-button
        >
      </div>
      <div style="margin-top: 10px">
        <el-button type="primary" :disabled="isshow" @click="cancellation"
          >取消</el-button
        >
      </div>
    </div>
  </div>
</template>
<script>
import Sortable from "sortablejs";
import {
  examinationgender,
  modifygender,
  genderbottomtotop,
  modifydraganddrop,
} from "../../request/systemapi";
import { postapi } from "@/api/api";
export default {
  data() {
    return {
      isshow: true,
      tableData: [],
      dialogVisible: false,
      form: {
        displayName: "",
      },
      initTableData: [],
    };
  },
  created() {
    this.getlist();
  },
  mounted() {
    this.rowDrop();
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    rowick(row) {
      this.form = row;
    },
    //取消按钮
    cancellation() {
      this.$message.info("取消操作");
      this.isshow = true;
      this.getlist();
    },
    //确定排序
    assertion() {
      const result = [];
      this.tableData.forEach((item, index) => {
        // index 从0开始的， 你的displayOrder从大到小排
        // const currentDisplayOrder = this.tableData.length -1
        const currentDisplayOrder = this.initTableData[index].displayOrder;
        console.log(currentDisplayOrder);
        if (item.displayOrder != currentDisplayOrder) {
          // 如果它的displayOrder和它当前所在的位置不同代表挪动过位置
          result.push({ id: item.id, displayOrder: currentDisplayOrder });
        }
      });
      modifydraganddrop({ itemList: result }).then((res) => {
        this.getlist();
        this.$message.success("操作成功");
        this.isshow = true;
      });
    },
    //初始化
    rowDrop() {
      this.$nextTick(() => {
        const tbody = document.querySelector(".el-table__body-wrapper tbody");
        const _this = this;
        Sortable.create(tbody, {
          handle: ".move",
          animation: 300,
          // 指定父元素下可被拖拽的子元素方法
          // draggable: ".module-manager .el-table__row",
          onEnd({ newIndex, oldIndex }) {
            // console.log(arr);
            _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));
          },
        });
      });
    },
    //置底
    setlow(row) {
      console.log(this.form.id);
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        genderbottomtotop(this.form.id, 2).then((res) => {
          this.getlist();
          this.$message.success("操作成功");
        });
      }
    },
    //置顶
    topping() {
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        genderbottomtotop(this.form.id, 1).then((res) => {
          this.getlist();
          this.$message.success("操作成功");
        });
      }
    },
    //确定修改
    confirmmodification() {
      if (this.form.displayName == "") {
        this.$message.warning("请输入名称");
      } else if (this.form.simpleCode == "") {
        this.$message.warning("请输入快捷码");
      } else {
        // modifygender(this.form.id, {
        //   displayName: this.form.displayName,
        //   simpleCode: this.form.simpleCode,
        // }).then((res) => {
        //   if (res.code == 1) {
        //     this.$message.success("修改成功");
        //     this.getlist();
        //     this.dialogVisible = false;
        //   }
        // });
        postapi(`/api/app/sex/update/${this.form.id}`, {
          displayName: this.form.displayName,
          simpleCode: this.form.simpleCode,
        }).then((res) => {
          if (res.code == 1) {
            this.$message.success("修改成功");
            this.getlist();
            this.dialogVisible = false;
          }
        });
      }
    },
    //编辑弹框
    eddte() {
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        let obj = Object.assign({}, this.form);
        console.log(obj);
        this.form = obj;
        this.dialogVisible = true;
      }
    },
    //
    getlist() {
      examinationgender().then((res) => {
        this.initTableData = [...res.data];
        this.tableData = res.data;
        console.log(res);
      });
    },
  },
};
</script>
<style scoped>
.box {
  display: flex;
}
.cancelorconfirm {
  display: flex;
  justify-content: flex-end;
  padding: 20px;
}
</style>
