<template>
  <div>
    <div style="position: relative">
      <div class="middlebox">
        <div class="contenttitle">
          基础资料 /
          <span class="contenttitleBold"
            >支付方式</span
          >
        </div>
      </div>
      <div style="display: block; margin-top: 7px; margin-right: 110px">
        <div style="background-color: #fff; padding: 15px; border-radius: 8px">
          <el-table
            :data="tableData"
            row-key="id"
            :height="window.pageHeight < 600 ? 480 : window.pageHeight - 130"
            class="el-table__body-wrapper tbody"
            @row-click="rowick"
            highlight-current-row
            ref="tableData"
          >
            <el-table-column prop="id" label="编号" min-width="80" align="center"/>
            <el-table-column prop="displayName" label="名称" min-width="80" align="center"/>
            <el-table-column prop="simpleCode" label="快捷码" min-width="80" align="center"/>
            <el-table-column prop="isActive" label="启用" min-width="80" align="center">
              <template slot-scope="scope">
                <div>{{ scope.row.isActive == 'Y' ? '是':'否'}}</div>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template>
                <el-tag
                  class="move"
                  style="
                    cursor: move;
                    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>
        </div>
      </div>
      <!-- 按钮  -->
        <div style="margin-left: 10px;margin-top: 3%;position: absolute;top: 0;right: 0;">
          <div>
            <el-button type="" @click="editpopup" 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>
    <!-- 新增或者编辑 -->
    <el-dialog
      title="修改"
      :visible.sync="dialogVisible"
      width="600px"
      :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 size="small"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称">
              <el-input v-model="form.displayName" size="small"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="启用">
              <el-radio v-model="form.isActive" label="Y">是</el-radio>
              <el-radio v-model="form.isActive" label="N">否</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" class="difference">取 消</el-button>
        <el-button type="primary" @click="addoredit" class="commonbutton"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Sortable from "sortablejs";
import {
  paymodelist,
  paymentediting,
  paytopandbottom,
  paymentdraganddrop,
} from "@/request/commonapi";
import { mapState } from "vuex"
export default {
  data() {
    return {
      isshow: true, 
      initTableData: [],
      tableData: [],
      form: {
        displayName: "",
        isActive: "Y",
      },
      dialogVisible: false,
    };
  },
  created() {
    this.getlist();
  },
  mounted() {
    this.rowDrop();
    
  },
  watch: {
    
  },
  computed:{
    ...mapState(['window'])
  },
  methods: {
    //取消排序
    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;
        // if (item.displayOrder != currentDisplayOrder) {
          // 如果它的displayOrder和它当前所在的位置不同代表挪动过位置
          result.push({
            id: item.id,
            displayOrder: index+1,
          });
        // }
      });
      paymentdraganddrop({ itemList: result }).then((res) => {
        if(res.code!=-1){
          this.isshow = true;
        this.getlist();
        //this.$message.success('操作成功')
        }
      });
    },
    //初始化
    rowDrop() {
      this.$nextTick(() => {
        const tbody = document.querySelector(".el-table__body-wrapper tbody");
        const _this = this;
        Sortable.create(tbody, {
          handle: ".move",
          animation: 300,
          onChoose({ oldIndex }) {
            _this.$refs["tableData"].setCurrentRow(_this.tableData[oldIndex]);
            _this.rowick(_this.tableData[oldIndex]);
          },
          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) {
              }
            });
          },
        });
      });
    },
    //置低
    toppings() {
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        paytopandbottom(this.form.id, 2).then((res) => {
          if(res.code!=-1){
            this.getlist();
          //this.$message.success('操作成功')
          }
        });
      }
    },
    //置顶
    topping() {
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        paytopandbottom(this.form.id, 1).then((res) => {
          if(res.code!=-1){
            this.getlist();
          //this.$message.success('操作成功')
          }
        });
      }
    },
    // 确定修改编辑
    addoredit() {
      if (this.form.displayName == "" || this.form.displayName == undefined) {
        this.$message.warning("请输入名称");
      } else {
        paymentediting(this.form.id, {
          displayName: this.form.displayName,
          isActive:this.form.isActive
        }).then((res) => {
          if(res.code!=-1){
            this.getlist();
          this.dialogVisible = false;
          //this.$message.success('修改成功')
          }
        });
      }
    },
    //编辑弹框
    editpopup() {
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        this.dialogVisible = true;
      }
    },
    rowick(row) {
      this.form = Object.assign({},row);
    },
    getlist() {
      paymodelist().then((res) => {
        if(res.code!=-1){
          this.initTableData = [...res.data];
        this.tableData = res.data;
        }
      });
    },
  },
};
</script>
<style scoped>
@import "../../assets/css/global_button.css";
@import "../../assets/css/global_dialog.css";
@import "../../assets/css/global_table.css";
@import "../../assets/css/global_form.css";
@import "../../assets/css/global_input.css";
@import "../../assets/css/global.css";
.box {
  display: flex;
  flex-direction: column;
}
:deep .el-form-item {
  margin-bottom: 14px;
}
/* el-dialog的头部样式 */
:deep .el-dialog__header {
  padding: 11px 20px 11px;
}
/* el-dialog的主体样式 */
:deep .el-dialog__body {
  padding: 0px 20px 0px;
}
/* el-divider样式 */
:deep .el-divider--horizontal {
  margin: 0px 0 12px;
}
/* el-dialog的底部样式 */
:deep .el-dialog__footer {
  padding: 0px 20px 14px;
}
</style>
