<template>
  <div style="display: flex">
    <el-card style="width: 95%">
      <!-- <el-button type="primary" @click="add">新增</el-button> -->
      <el-table
        :data="tableData"
        style="width: 100%"
        row-key="id"
        class="el-table__body-wrapper tbody"
        @row-click="rowclick"
        highlight-current-row
      >
        <el-table-column prop="id" label="编号" width="180"> </el-table-column>
        <el-table-column prop="displayName" label="名称" width="180">
        </el-table-column>

        <el-table-column prop="reportPrompt" label="报告单提示" width="180">
        </el-table-column>
        <el-table-column
          prop="dataInputPrompt"
          label="数据录入提示"
          width="180"
        >
        </el-table-column>
        <el-table-column prop="reportFontColor" label="报告单字体色">
        </el-table-column>
        <el-table-column label="操作" width="400">
          <!-- <template slot-scope="scope"> -->
          <!-- <el-button type="primary" @click="delsrts(scope.row)"
              >删除</el-button
            > -->
          <!-- <el-button type="primary" @click="topping(scope.row, 1)"
              >置顶</el-button
            >
            <el-button type="primary" @click="topping(scope.row, 2)"
              >置底</el-button
            > -->
          <!-- <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">
        <el-button type="primary" :disabled="isshow" @click="assertion"
          >确定排序</el-button
        >
        <el-button type="primary" :disabled="isshow" @click="cancellation"
          >取消排序</el-button
        >
      </div> -->
      <!-- 新增或者编辑弹框 -->
      <el-dialog
        :title="title == 1 ? '新增' : '修改'"
        :visible.sync="dialogVisible"
        width="55%"
        :before-close="handleClose"
        :close-on-click-modal="false"
      >
        <el-form :model="form" :rules="rules" ref="ruleForm" label-width="80px">
          <el-row>
            <!-- <el-col :span="12">
              <el-form-item f
                label="id"
                label-width="120px"
                prop="resultStatusId"
              >
                <el-input v-model="form.resultStatusId"></el-input>
              </el-form-item>
            </el-col> -->
            <!-- <el-col :span="12">
              <el-form-item label="名称" label-width="120px" prop="displayName">
                <el-input v-model="form.displayName"></el-input>
              </el-form-item>
            </el-col> -->
            <el-col :span="12">
              <el-form-item
                label="数据录入提示"
                label-width="120px"
                prop="dataInputPrompt"
              >
                <el-input v-model="form.dataInputPrompt"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="报告单提示"
                label-width="120px"
                prop="reportPrompt"
              >
                <el-input v-model="form.reportPrompt"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="报告单背景色"
                label-width="120px"
                prop="reportBackgroundColor"
              >
                <el-color-picker
                  @change="reportBackgroundColorchange"
                  v-model="reportBackgroundColor16"
                ></el-color-picker>
                <!-- <el-input></el-input> -->
                <!-- <input type="text" v-model="form.reportBackgroundColor"> -->
                <el-input
                  v-model.number="form.reportBackgroundColor"
                  @input="reportBackgroundColorchange1"
                  style="
                    width: 85%;
                    position: absolute;
                    top: 0px;
                    margin-left: 10px;
                  "
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="报告单字体色"
                label-width="120px"
                prop="reportFontColor"
              >
                <!-- <el-input v-model="form.reportFontColor"></el-input> -->
                <el-color-picker
                  v-model="reportFontColor16"
                  @change="reportFontColorchange"
                ></el-color-picker>
                <el-input
                  v-model="form.reportFontColor"
                  @input="reportFontColor1"
                  style="
                    width: 85%;
                    position: absolute;
                    top: 0px;h
                    margin-left: 10px;
                  "
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="数据录入背景" label-width="100px">
                <el-color-picker
                  v-model="dataInputBackgroundColor16"
                  @change="dataInputBackgroundColorchange"
                ></el-color-picker>
                <el-input
                  @input="inputdataInputBackgroundColor"
                  v-model="form.dataInputBackgroundColor"
                  style="
                    width: 85%;
                    position: absolute;
                    top: 0px;
                    margin-left: 10px;
                  "
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item
                label="数据录入字体"
                label-width="120px"
                prop="dataInputFontColor"
              >
                <el-color-picker
                  v-model="dataInputFontColor16"
                  @change="dataInputFontColorchange"
                ></el-color-picker>
                <el-input
                  v-model.number="form.dataInputFontColor"
                  @input="dataInputFontColor1"
                  style="
                    width: 85%;
                    margin-left: 10px;
                    position: absolute;
                    top: 0px;
                  "
                ></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="confirmsubmission">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
    <div style="margin-left: 10px">
      <el-button type="primary" @click="editpopup" style="margin-top: 60px"
        >编辑</el-button
      >
    </div>
  </div>
</template>
<script>
import Sortable from "sortablejs";
import {
  resultlist,
  statusaddition,
  statusmodification,
  deletedstate,
  stateBottomTop,
  sortstatus,
} from "../../request/systemapi";
export default {
  data() {
    return {
      isshow: true,
      form: {
        resultStatusId: "",
        displayName: "",
        dataInputPrompt: "",
        reportPrompt: "",
        reportBackgroundColor: 1089274,
        // reportBackgroundColor16: "#409EFF",
        reportFontColor: 1089274,
        // reportFontColor16: "#409EFF",
        dataInputBackgroundColor: 1089274,
        // dataInputBackgroundColor16: "#409EFF",
        dataInputFontColor: 1089274,
        // dataInputFontColor16: "#409EFF",
      },
      reportBackgroundColor16: "",
      reportFontColor16: "#409EFF",
      dataInputBackgroundColor16: "#409EFF",
      dataInputFontColor16: "#409EFF",
      dialogVisible: false,
      title: 1,
      tableData: [],
      initTableData: [],
      pages: {
        // currentPage: 0,
        SkipCount: 0,
        MaxResultCount: 100,
        Sorting: "displayOrder desc",
        // totalCount: 0,
      },
      //表单校验
      rules: {
        resultStatusId: [
          { required: true, message: "请输入id", trigger: "blur" },
        ],
        displayName: [
          { required: true, message: "请输入名称", trigger: "blur" },
        ],
        dataInputPrompt: [
          { required: true, message: "请输入数据录入", trigger: "blur" },
        ],
        reportPrompt: [
          { required: true, message: "请输入报关单", trigger: "blur" },
        ],
        reportBackgroundColor: [
          { required: true, message: "请输入报告单背景色", trigger: "blur" },
        ],
        reportFontColor: [
          { required: true, message: "请输入报告单字体色", trigger: "blur" },
        ],
        dataInputBackgroundColor: [
          { required: true, message: "请输入数据录入背景色", trigger: "blur" },
        ],
        dataInputFontColor: [
          { required: true, message: "请输入数据录入字体色", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getlist();
  },
  mounted() {
    this.rowDrop();
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },

    //数据录入字体
    dataInputFontColorchange(v) {
      this.form.dataInputFontColor = eval(v.replace("#", "0x"));
    },
    //数据录入背景Input
    inputdataInputBackgroundColor(v) {
      this.form.dataInputBackgroundColor = Number(v);
      let temp = "000000" + this.form.dataInputBackgroundColor.toString(16);
      this.dataInputBackgroundColor16 =
        "#" + temp.substring(temp.length - 6, temp.length);
    },
    //数据录入背景
    dataInputBackgroundColorchange(v) {
      this.form.dataInputBackgroundColor = eval(v.replace("#", "0x"));
    },
    //颜色选择器报告单字体色
    reportFontColorchange(v) {
      this.form.reportFontColor = eval(v.replace("#", "0x"));
      console.log(this.form.reportFontColor);
    },
    //报告单背景色
    reportBackgroundColorchange(v) {
      this.form.reportBackgroundColor = eval(v.replace("#", "0x"));
      console.log(this.form.reportBackgroundColor);
    },
    //数据录入字体
    dataInputFontColor1(v) {
      this.form.dataInputFontColor = Number(v);
      let temp = "000000" + this.form.dataInputFontColor.toString(16);
      this.dataInputFontColor16 =
        "#" + temp.substring(temp.length - 6, temp.length);
    },
    //报告单字体色
    reportFontColor1(v) {
      this.form.reportFontColor = Number(v);
      let temp = "000000" + this.form.reportFontColor.toString(16);
      this.reportFontColor16 =
        "#" + temp.substring(temp.length - 6, temp.length);
    },
    //报告单背景色
    reportBackgroundColorchange1(v) {
      this.form.reportBackgroundColor = Number(v);
      let temp = "000000" + this.form.reportBackgroundColor.toString(16);
      this.reportBackgroundColor16 =
        "#" + temp.substring(temp.length - 6, temp.length);
    },
    rowclick(row) {
      this.form = row;
      console.log(this.form);
      this.reportBackgroundColor16 = "#" + this.form.reportBackgroundColor; //reportBackgroundColor
      console.log("reportBackgroundColor16", this.reportBackgroundColor16);
      this.reportBackgroundColorchange1(this.form.reportBackgroundColor);
      this.reportFontColor1(this.form.reportFontColor);
      this.dataInputFontColor1(this.form.dataInputFontColor);
      this.inputdataInputBackgroundColor(this.form.dataInputBackgroundColor);
    },
    haxadecimal() {
      let str = this.form.dataInputFontColor;
      this.form.dataInputFontColor = str
        .replace(/[^0-9a-fA-F]/g, "")
        .replace(/\s/g, "");
    },
    //取消排序
    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.resultStatusId,
            displayOrder: currentDisplayOrder,
          });
        }
      });
      sortstatus({ 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,
          // 指定父元素下可被拖拽的子元素
          // 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));
          },
        });
      });
    },
    //置顶置底
    topping(row, t) {
      stateBottomTop(row.resultStatusId, t).then((res) => {
        this.$message.success("操作成功");
        this.getlist();
      });
    },
    //删除
    delsrts(row) {
      deletedstate(row.resultStatusId).then((res) => {
        this.$message.success("删除成功");
        this.getlist();
      });
    },
    //编辑弹框
    editpopup() {
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        console.log("22222222222222");

        this.dialogVisible = true;
        this.title = 2;
      }
    },
    //q确定
    confirmsubmission() {
      this.$refs.ruleForm.validate((v) => {
        if (v) {
          if (this.title == 1) {
            statusaddition(this.form).then((res) => {
              this.$message.success("新增成功");
              this.getlist();
              this.dialogVisible = false;
              this.form = {};
            });
          } else if (this.title == 2) {
            console.log(this.form.reportBackgroundColor);
            let obj = {
              // displayName: this.form.displayName,
              dataInputPrompt: this.form.dataInputPrompt,
              reportPrompt: this.form.reportPrompt,
              reportBackgroundColor: this.form.reportBackgroundColor,
              reportFontColor: this.form.reportFontColor,
              dataInputBackgroundColor: this.form.dataInputBackgroundColor,
              dataInputFontColor: this.form.dataInputFontColor,
            };
            statusmodification(this.form.id, obj).then((res) => {
              this.$message.success("修改成功");
              this.getlist();
              this.dialogVisible = false;
            });
          }
        }
      });
    },
    //新增弹框
    add() {
      this.title = 1;
      this.dialogVisible = true;
    },
    //获取数据列表
    getlist() {
      resultlist(this.pages).then((res) => {
        this.tableData = res.data.items;
        this.initTableData = [...res.data.items];
        this.totalCount = res.data.totalCount;
        console.log(res);
      });
    },
  },
};
</script>
<style scoped>
.cancelorconfirm {
  display: flex;
  justify-content: flex-end;
  padding: 20px;
}
</style>
