<template>
  <div class="box">
    <div style="width: 95%">
      <el-card class="elcard" style="border-radius: 15px">
        <div class="publiccss">支付方式</div>
        <el-table
          :data="tableData"
          style="width: 100%; height: 500px;margin-top:20px"
          row-key="id"
          :height="tableHeight"
          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="300">
          </el-table-column>
          <el-table-column prop="simpleCode" label="快捷码" width="300">
          </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="修改"
          :visible.sync="dialogVisible"
          width="40%"
          :close-on-click-modal="false"
        >
          <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" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="名称">
                  <el-input v-model="form.displayName"></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: 5%">
      <el-button type="" @click="editpopup" class="commonbutton">编辑</el-button>
      <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 {
  paymodelist,
  paymentediting,
  paytopandbottom,
  paymentdraganddrop,
} from "@/request/commonapi";
export default {
  data() {
    return {
      isshow: true,
      tableHeight: window.innerHeight - 180, //表格动态高度
      screenHeight: window.innerHeight, //内容区域高度 1
      initTableData: [],
      tableData: [],
      form: {
        displayName: "",
      },
      dialogVisible: false,
    };
  },
  created() {
    this.getlist();
  },
  mounted() {
    this.rowDrop();
    // window.onresize:浏览器尺寸变化响应事件
    window.onresize = () => {
      return (() => {
        // window.innerHeight:浏览器的可用高度
        window.screenHeight = window.innerHeight;
        this.screenHeight = window.screenHeight;
      })();
    };
  },
  watch: {
    // 监听screenHeight从而改变table的高度
    screenHeight(val) {
      this.screenHeight = val;
      this.tableHeight = this.screenHeight - 180;
    },
  },
  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,
          });
        }
      });
      paymentdraganddrop({ 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() {
      console.log(this.form.id);
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        paytopandbottom(this.form.id, 2).then((res) => {
          this.$message.success("操作成功");
          this.getlist();
        });
      }
    },
    //置顶
    topping() {
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        paytopandbottom(this.form.id, 1).then((res) => {
          this.$message.success("操作成功");
          this.getlist();
        });
      }
    },
    // 确定修改编辑
    addoredit() {
      if (this.form.displayName == "") {
        this.$message.warning("请输入名称");
      } else {
        paymentediting(this.form.id, {
          displayName: this.form.displayName,
        }).then((res) => {
          this.$message.success("修改成功");
          this.getlist();
          this.dialogVisible = false;
        });
      }
    },
    //编辑弹框
    editpopup() {
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        this.dialogVisible = true;
        let copy = Object.assign({}, this.form);
        this.form = copy;
        // invoiceidquery(this.form.payModeId).then((res) => {
        //   console.log(res);
        //   this.form = res.data;
        // });
      }
    },
    rowick(row) {
      this.form = row;
    },
    getlist() {
      paymodelist().then((res) => {
        this.initTableData = [...res.data];
        this.tableData = res.data;
        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); /* 设置表头文字颜色 */
}
.box {
  display: flex;
}
</style>
