<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
        >
          <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="containerRemark" label="备注" width="" />
          <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%"
          :close-on-click-modal="false"
        >
          <el-form ref="form" :model="form" label-width="80px" :rules="rules">
            <el-row>
              <el-col :span="8">
                <el-form-item label="编号" prop="">
                  <el-input v-model="form.id" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="名称" prop="displayName">
                  <el-input
                    v-model="form.displayName"
                    ref="refinput"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-color-picker
                  v-model="color16"
                  @change="containerColorchange"
                ></el-color-picker>
                <el-form-item
                  label="颜色"
                  prop="containerColor"
                  style="width: 90%; margin-top: -40px"
                >
                  <el-input
                    v-model="form.containerColor"
                    @input="inputcontainerColor"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="容器名称" prop="containerColorName">
                  <el-input v-model="form.containerColorName"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注" prop="containerRemark">
                  <el-input v-model="form.containerRemark"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-divider />
              </el-col>
              <el-col :span="4" style="margin-left: 15px">
                <el-form-item label="创建者">
                  <el-input
                    v-model="form.creatorName"
                    disabled
                    style="width: 110%"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="创建时间">
                  <el-input
                    :value="form.creationTime | 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
                    style="width: 110%"
                  ></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>
    <!-- //ann按钮区域 -->
    <div style="margin-left: 10px; margin-top: 7%">
      <el-button type="primary" @click="add">新增</el-button>
      <el-button
        type="primary"
        @click="editpopup"
        style="margin-left: 0; margin-top: 10px"
        >编辑</el-button
      >
      <div>
        <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>
        <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 from "sortablejs";
import {
  containerlist,
  newlyspecimen,
  Containerid,
  specimenediting,
  detecontainer,
  specimentopbottom,
  specimensort,
} from "../../request/systemapi";
export default {
  data() {
    return {
      isshow: true,
      rules: {
        displayName: [
          { required: true, message: "请输入名称", trigger: "blur" },
        ],
        containerColorName: [
          { required: true, message: "请输入容器名称", trigger: "blur" },
        ],
        containerColor: [
          { required: true, message: "请输入容器颜色", trigger: "blur" },
        ],
      },
      pages: {
        Filter: "",
        SkipCount: 0,
        MaxResultCount: 100,
        Sorting: "displayOrder desc",
      },
      tableData: [],
      initTableData: [],
      title: 1,
      dialogVisible: false,
      form: {
        displayName: "",
        containerColorName: "",
        containerColor: 1089274,
        containerRemark: "",
      },
      color16: "#409EFF",
      curRow: {},
    };
  },
  created() {
    this.getlist();
  },
  mounted() {
    this.rowDrop();
  },
  methods: {
    inputcontainerColor(v) {
      this.form.containerColor = Number(v);
      let temp = "000000" + this.form.containerColor.toString(16);
      this.color16 = "#" + temp.substring(temp.length - 6, temp.length);
    },
    containerColorchange(v) {
      this.form.containerColor = eval(v.replace("#", "0x"));
    },
    colorchange(v) {
      var hexColor = v;
      // 去除开头的#号
      hexColor = hexColor.substring(1);
      // 转换为十进制
      this.form.containerColor = parseInt(hexColor, 16);

      console.log(this.form.containerColor);
      // this.form.containerColor = parseInt(v)
      // console.log(this.form.containerColor);
    },
    //取消排序
    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 });
        }
      });
      specimensort({ 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.success("请点击选择操作的数据");
      } else {
        specimentopbottom(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 {
        specimentopbottom(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(() => {
            detecontainer(this.form.id).then((res) => {
              this.$message.success("删除成功");
              this.getlist();
              this.form = {};
            });
          })
          .catch(() => {});
        // detecontainer(this.form.id).then((res) => {
        //   this.$message.success("删除成功");
        //   this.form = {};
        //   this.getlist();
        // });
      }
    },
    //编辑弹框
    editpopup() {
      this.resetFields();
      this.form = { ...this.curRow };
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        this.dialogVisible = true;
        this.title = 2;
        this.getFocus();
        Containerid(this.form.id).then((res) => {
          console.log(res);
          this.form = res.data;
        });
      }
    },
    //点击列表通过id查询
    rowick(row) {
      Containerid(row.id).then((res) => {
        this.curRow = { ...res.data };
        // console.log(res);
        // this.form = res.data;
      });
    },
    //确定新增或者编辑
    addoredit() {
      this.$refs.form.validate((v) => {
        if (v) {
          if (this.title == 1) {
            newlyspecimen({
              displayName: this.form.displayName,
              containerColorName: this.form.containerColorName,
              containerColor: this.form.containerColor,
              containerRemark: this.form.containerRemark,
            }).then((res) => {
              this.$message.success("新增成功");
              this.getlist();
              this.dialogVisible = false;
            });
          } else if (this.title == 2) {
            specimenediting(this.form.id, {
              displayName: this.form.displayName,
              containerColorName: this.form.containerColorName,
              containerColor: this.form.containerColor,
              containerRemark: this.form.containerRemark,
            }).then((res) => {
              this.$message.success("修改成功");
              this.getlist();
              this.dialogVisible = false;
            });
          }
        }
      });
    },
    //新增弹框
    add() {
      this.title = 1;
      this.dialogVisible = true;
      this.form = {};
      this.getFocus();
      this.resetFields();
    },
    getFocus() {
      this.$nextTick(() => {
        this.$refs.refinput.focus();
      });
    },

    resetFields() {
      this.$nextTick(() => {
        this.$refs.form.resetFields();
      });
    },
    getlist() {
      containerlist(this.pages).then((res) => {
        this.initTableData = [...res.data.items];
        this.tableData = res.data.items;
        console.log(res);
      });
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-input.is-disabled .el-input__inner {
  width: 88%;
}
.box {
  display: flex;
}
</style>
