<template>
  <div class="box">
    <div style="width: 150%">
      <el-card class="elcard" style="border-radius: 15px">
        <div class="publiccss">项目结果模板</div>
        <el-table
          :data="tableData"
          style="width: 100%; margin-top: 20px"
          row-key="id"
          class="el-table__body-wrapper tbody"
          @row-click="rowclick"
          highlight-current-row
        >
          <el-table-column prop="id" label="编号" width="300">
          </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 prop="result" label="结果" width="">
          </el-table-column>
          <el-table-column label="操作" width="">
            <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>
        <!-- <comp-table
          :tableData="tableData"
          :tableHeader="tableHeader"
          @clickevents="rowclick"
          ref="singleTable"
        >
        </comp-table> -->
      </el-card>
      <!-- 弹框 -->
      <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="itemId">
                <el-select
                  filterable
                  v-model="form.itemId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in itemId"
                    :key="item.id"
                    :label="item.displayName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="结果" prop="result">
                <el-input v-model="form.result" style="width: 80%"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="诊断编号" prop="diagnosisId">
                <el-select
                  filterable
                  v-model="form.diagnosisId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in diagnosisId"
                    :key="item.id"
                    :label="item.displayName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="小结名称" prop="isNameIntoSummary">
                <el-select
                  filterable
                  v-model="form.isNameIntoSummary"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in isNameIntoSummary"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="进入小结" prop="isResultIntoSummary">
                <el-select
                  filterable
                  v-model="form.isResultIntoSummary"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in isResultIntoSummary"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="结果状态" prop="resultStatusId">
                <el-select
                  filterable
                  v-model="form.resultStatusId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in resultStatusId"
                    :key="item.id"
                    :label="item.displayName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="拼音简码" prop="simpleCode">
                <el-input
                  v-model="form.simpleCode"
                  style="width: 80%"
                ></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="onsbmit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <!-- 按钮区域 -->
    <div style="margin-left: 10px; margin-top: 5%" class="buttonarea">
      <el-button type="" @click="add" class="commonbutton">新增</el-button>
      <el-button
        type=""
        @click="editpopup"
        style="margin-left: 0; margin-top: 10px"
        class="commonbutton"
        >编辑</el-button
      >
      <div style="margin-top: 10px">
        <el-button type="" @click="delets" 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: 5px">
        <el-button
          type=""
          :disabled="isshow"
          @click="cancellation"
          class="commonbutton"
          >取消</el-button
        >
      </div>
    </div>
  </div>
</template>
<script>
import { rowDrop } from "@/components/publicjs/public";
import { getapi, postapi, putapi, deletapi } from "@/api/api";
import { getporjectlists } from "@/request/commonapi";
import compTable from "@/components/compTable.vue";
import Sortable from "sortablejs";
export default {
  components: { compTable },
  data() {
    return {
      isshow: true,
      rules: {
        itemId: [{ required: true, message: "请选择项目", trigger: "change" }],
        result: [{ required: true, message: "请输入结果", trigger: "blur" }],
        diagnosisId: [
          { required: true, message: "请选择诊断编号", trigger: "change" },
        ],
        isNameIntoSummary: [
          { required: true, message: "请选择小结名称", trigger: "change" },
        ],
        isResultIntoSummary: [
          { required: true, message: "请选择进入小结", trigger: "change" },
        ],
        resultStatusId: [
          { required: true, message: "请选择结果状态", trigger: "change" },
        ],
        simpleCode: [
          { required: true, message: "请输入拼音简码", trigger: "change" },
        ],
      },
      form: {
        itemId: "",
        result: "",
        diagnosisId: "",
        isNameIntoSummary: "",
        isResultIntoSummary: "",
        resultStatusId: "",

        simpleCode: "",
      },
      dialogVisible: false,
      title: 1,
      pages: {
        SkipCount: 0,
        MaxResultCount: 100,
        Sorting: "displayOrder desc",
      },
      itemId: [], //项目
      diagnosisId: [], //诊断
      resultStatusId: [], //结果状态 1
      isNameIntoSummary: [
        {
          value: "N",
          label: "否",
        },
        {
          value: "Y",
          label: "是",
        },
      ], // 小结名称
      isResultIntoSummary: [
        {
          value: "N",
          label: "否",
        },
        {
          value: "Y",
          label: "是",
        },
      ], //进入小结
      tableData: [],
      initTableData: [],
      tableHeader: [
        // 表头信息，可根据minWidth修改宽度
        { prop: "id", label: "编号", minWidth: "150px" },
        { prop: `creatorName`, label: "创建者" },
        { prop: `creationTime`, label: "创建时间" },
        { prop: `lastModifierName`, label: "修改者" },
        { prop: `lastModificationTime`, label: "创建时间" },
        { prop: "result", label: "结果" },
        // { prop: "simpleCode", label: "拼音简码" },
      ],
      curRow: {},
    };
  },
  created() {
    this.getlist();
  },
  mounted() {
    this.rowDrop();
  },
  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 });
        }
      });
      putapi("/api/app/itemresulttemplate/updatesortmany", {
        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.warning("请选择操作的数据");
      } else {
        putapi(
          `/api/app/itemresulttemplate/updatemanysort?id=${
            this.form.id
          }&SortType=${2}`
        ).then((res) => {
          this.$message.success("操作成功");
          this.getlist();
        });
      }
    },
    topping() {
      this.form = { ...this.curRow };
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        putapi(
          `/api/app/itemresulttemplate/updatemanysort?id=${
            this.form.id
          }&SortType=${1}`
        ).then((res) => {
          this.$message.success("操作成功");
          this.getlist();
        });
      }
    },
    //删除
    delets(row) {
      this.form = { ...this.curRow };
      if (this.form.id == undefined) {
        this.$message.warning("请选择删除的数据");
      } else {
        this.$confirm("是否确认删除,是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            deletapi(`/api/app/item-result-template/${this.form.id}`).then(
              (res) => {
                if (res.code == 0) {
                  this.$message.success("删除成功");
                  this.getlist();
                }
              }
            );
          })
          .catch(() => {});
      }
    },
    //编辑弹框
    editpopup() {
      this.form = { ...this.curRow };
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        this.dialogVisible = true;
        this.title = 2;
        this.addtoedit();
        getapi(`/api/app/item-result-template/${this.form.id}`).then((res) => {
          console.log(res);
        });
      }
    },
    //确定新增或者编辑
    onsbmit() {
      this.$refs.form.validate((v) => {
        if (v) {
          if (this.title == 1) {
            postapi("/api/app/item-result-template", this.form).then((res) => {
              this.$message.success("新增成功");
              this.getlist();
              this.dialogVisible = false;
            });
          } else if (this.title == 2) {
            putapi(
              `/api/app/item-result-template/${this.form.id}`,
              this.form
            ).then((res) => {
              this.$message.success("修改成功");
              this.getlist();
              this.dialogVisible = false;
            });
          }
        }
      });
    },
    rowclick(val) {
      getapi(`/api/app/item-result-template/${val.id}`).then((res) => {
        // this.form = res.data;
        this.curRow = { ...res.data };
        console.log(res);
      });
    },
    handleRowClick(Row) {
      console.log(Row);
      console.log("1");
    },
    // 新增或者编辑需要的类别
    addtoedit() {
      //项目
      getporjectlists().then((res) => {
        this.itemId = res.data.items;
      });
      // 诊断
      postapi("/api/app/diagnosis/getlistinfilter").then((res) => {
        this.diagnosisId = res.data.items;
      });
      //结果状态
      getapi("/api/app/result-status").then((res) => {
        this.resultStatusId = res.data.items;
      });
      getapi("/api/app/diagnosis/getlistinfilter").then((res) => {});
    },
    //新增弹框
    add() {
      this.addtoedit();
      this.dialogVisible = true;
      this.title = 1;
      this.form = {};
    },
    getlist() {
      getapi("/api/app/item-result-template", this.pages).then((res) => {
        console.log(res);
        this.tableData = res.data.items;
        this.initTableData = [...res.data.items];
      });
      let that = this;
    },
  },
};
</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>
