<template>
  <div class="box">
    <div style="width: 200%">
      <el-card>
        <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="primary" style="margin-left: 5%" @click="getlist"
              >查询</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="tableData"
          style="width: 100%; height: 500px"
          row-key="id"
          class="el-table__body-wrapper tbody"
          @row-click="rowick"
          highlight-current-row
          height="500"
        >
          <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"
                draggable="true"
              >
                <i
                  class="el-icon-d-caret"
                  style="width: 1rem; height: 1rem"
                ></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="9">
                <el-form-item label="编号">
                  <el-input
                    v-model="form.id"
                    style="width: 100%"
                    disabled
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item label="名称">
                  <el-input
                    v-model="form.displayName"
                    maxlength="1"
                    show-word-limit
                    style="width: 90%"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="7">
                类别
                <el-select
                  v-model="form.commonCharName"
                  placeholder="请选择"
                  @change="change"
                >
                  <el-option
                    v-for="item in ars"
                    :key="item.id"
                    :label="item.displayName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </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.creatorName | dateFormat"
                    disabled
                    style="width: 95%"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item label="修改者">
                  <el-input v-model="form.lastModifierName" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="修改时间">
                  <el-input
                    :value="form.lastModificationTime | dateFormat"
                    disabled
                    style="width: 95%"
                  ></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">确 定</el-button>
          </span>
        </el-dialog>
      </el-card>
    </div>
    <!-- 按钮区域 -->
    <div style="margin-left: 10px; margin-top: 10%">
      <el-button type="primary" @click="add">新增</el-button>
      <el-button
        type="primary"
        @click="editpopup"
        style="margin-left: 0; margin-top: 10px"
        >编辑</el-button
      >
      <div style="margin-top: 10px">
        <el-button type="danger" @click="delsrts">删除</el-button>
      </div>
      <div style="margin-top: 10px">
        <el-button type="primary" @click="topping">置顶</el-button>
        <div style="margin-top: 10px">
          <el-button type="primary" @click="toppings">置底</el-button>
        </div>
        <div style="margin-top: 10px">
          <el-button type="primary" :disabled="isshow" @click="assertion"
            >排序</el-button
          >
        </div>
        <div style="margin-top: 5px">
          <el-button type="primary" :disabled="isshow" @click="cancellation"
            >取消</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Sortable, { get } from "sortablejs";
import {
  frequentlyusedlist,
  categorytype,
  addingwordes,
  commonid,
  commoneditors,
  usedtopping,
  sortcommonwords,
  commondelt,
} from "../../request/systemapi";
import { getapi } from "@/api/api";
export default {
  data() {
    return {
      pages: {
        Filter: "",
        SkipCount: 0,
        MaxResultCount: 100,
        Sorting: "displayOrder desc",
      },
      form: {
        displayName: "",
        ars: "",
      },
      ars: [],
      tableData: [],
      value: "",
      title: 1,
      dialogVisible: false,
      isshow: true,
    };
  },
  created() {
    this.getlist();
  },
  mounted() {
    this.rowDrop();
  },
  methods: {
    //删除
    delsrts() {
      if (this.form.id == undefined) {
        this.$message.warning("请选择删除的数据");
      } else {
        this.$confirm("是否确认删除,是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            commondelt(this.form.id).then((res) => {
              this.$message.success("删除成功");
              this.getlist();
              this.form = {};
            });
          })
          .catch(() => {});
        // commondelt(this.form.id).then((res) => {
        //   this.$message.success("删除成功");
        //   this.form = {};
        //   this.getlist();
        // });
      }
    },
    change(v) {
      this.form.ids = v;
      console.log(v);
    },
    //取消排序
    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 });
        }
      });
      sortcommonwords({ 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() {
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        usedtopping(this.form.id, 2).then((res) => {
          this.$message.success("操作成功");
          this.getlist();
        });
      }
    },
    //置顶
    topping() {
      console.log(this.form.id);
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        usedtopping(this.form.id, 1).then((res) => {
          this.$message.success("操作成功");
          this.getlist();
        });
      }
    },
    //编辑弹框
    editpopup() {
      console.log(this.form.id);
      if (this.form.id == undefined) {
        this.$message.success("请点击选择操作的数据");
      } else {
        this.dialogVisible = true;
        this.title = 2;
        categorytype(this.pages).then((res) => {
          this.ars = res.data.items;
          console.log(res);
        });
        // console.log(this.tableData[0].id);
        commonid(this.form.id).then((res) => {
          this.form = res.data;
        });
      }
    },
    //确定新增或者编辑
    addoredit() {
      console.log(this.form.ids);
      if (this.form.displayName == undefined) {
        this.$message.warning("请输入名称");
      } else if (this.form.ars == "") {
        this.$message.warning("请选择项目"); //测试时校验选择项目不为空
      } else {
        if (this.title == 1) {
          addingwordes({
            displayName: this.form.displayName,
            commonCharTypeId: this.form.ids,
          }).then((res) => {
            this.$message.success("新增成功");
            this.getlist();
            this.dialogVisible = false;
            this.form = {};
          });
        } else if (this.title == 2) {
          commoneditors(this.form.id, {
            displayName: this.form.displayName,
            commonCharTypeId: this.form.ids,
          }).then((res) => {
            this.$message.success("修改成功");
            this.getlist();
            this.dialogVisible = false;
          });
        }
      }
    },
    //新增弹框
    add() {
      this.title = 1;
      this.dialogVisible = true;
      this.form = {};
      categorytype(this.pages).then((res) => {
        this.ars = res.data.items;
        console.log(res);
      });
    },
    getlist() {
      frequentlyusedlist(this.pages).then((res) => {
        this.initTableData = [...res.data.items];
        this.tableData = res.data.items;
        console.log(res);
      });
    },
    //点击列表通过id查询
    rowick(row) {
      commonid(row.id).then((res) => {
        console.log(res);
        this.form = res.data;
      });
      // //获取类别
      // getapi("/api/app/common-char-type/in-filter").then((res) => {
      //   console.log(res, "类别");
      //   this.ars = res.data.items;
      // });
      console.log(row);
    },
  },
};
</script>
<style scoped>
::v-deep .el-input.is-disabled .el-input__inner {
  width: 88%;
}
.box {
  display: flex;
}
</style>
