<template>
  <div class="box">
    <div style="width: 95%">
      <el-card>
        <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="300">
          </el-table-column>
          <el-table-column
            prop="referenceRangeValue"
            label="参考范围"
            width="130"
          >
          </el-table-column>
          <el-table-column prop="creatorName" label="创建者" width="150">
          </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="操作" width="">
            <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>
        <!-- <comp-table
          :tableData="tableData"
          :tableHeader="tableHeader"
          @clickevents="rowclick"
          ref="singleTable"
        >
        </comp-table> -->
      </el-card>
      <!-- 弹框 -->
      <el-dialog
        :title="title == 1 ? '新增' : '编辑'"
        :visible.sync="dialogVisible"
        width="75%"
      >
        <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 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="sexHormoneTermId"
                label-width="100px"
              >
                <el-select v-model="form.sexHormoneTermId" placeholder="请选择">
                  <el-option
                    v-for="item in sexHormoneTermId"
                    :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="ageLowerLimit">
                <el-input
                  v-model="form.ageLowerLimit"
                  style="width: 80%"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="年龄上限" prop="ageUpperLimit">
                <el-input
                  v-model="form.ageUpperLimit"
                  style="width: 80%"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="参考范围" prop="referenceRangeValue">
                <el-input
                  v-model="form.referenceRangeValue"
                  style="width: 80%"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="危急值参考"
                prop="criticalRangeValue"
                label-width="100px"
              >
                <el-input
                  v-model="form.criticalRangeValue"
                  style="width: 80%"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="下限诊断" prop="lowerDiagnosisId">
                <el-select v-model="form.lowerDiagnosisId" placeholder="请选择">
                  <el-option
                    v-for="item in lowerDiagnosisId"
                    :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="upperDiagnosisId">
                <el-select v-model="form.upperDiagnosisId" placeholder="请选择">
                  <el-option
                    v-for="item in upperDiagnosisId"
                    :key="item.id"
                    :label="item.displayName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </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%">
      <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="delets">删除</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>
</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 {
      sexHormoneTermId: [],
      filetelists: [],
      flitvalues: [],
      projectid: [],
      values: [],
      isshow: true,
      rules: {
        itemId: [
          { required: true, message: "请选择项目编号", trigger: "change" },
        ],
        sexHormoneTermId: [
          { required: true, message: "请选择性激素期限ID", trigger: "change" },
        ],
        ageLowerLimit: [
          { required: true, message: "请输入年龄下限", trigger: "blur" },
        ],
        ageUpperLimit: [
          { required: true, message: "请输入年龄上限", trigger: "blur" },
        ],
        referenceRangeValue: [
          { required: true, message: "请输入参考范围", trigger: "blur" },
        ],
        criticalRangeValue: [
          { required: true, message: "请输入危急值参考", trigger: "blur" },
        ],
        lowerDiagnosisId: [
          { required: true, message: "请选择下限诊断", trigger: "change" },
        ],
        upperDiagnosisId: [
          { required: true, message: "请选择上限诊断", trigger: "change" },
        ],
      },
      form: {
        itemId: "",
        sexHormoneTermId: "",
        ageLowerLimit: "",
        ageUpperLimit: "",
        referenceRangeValue: "",
        criticalRangeValue: "",
        lowerDiagnosisId: "",
        upperDiagnosisId: "",
      },
      lowerDiagnosisId: [],
      upperDiagnosisId: [],
      sexHormoneTermId: [],
      isActive: [
        {
          value: "N",
          label: "否",
        },
        {
          value: "Y",
          label: "是",
        },
      ], //启用标志
      itemId: [],
      dialogVisible: false,
      title: 1,
      pages: {
        SkipCount: 0,
        MaxResultCount: 100,
      },

      tableData: [],
      initTableData: [],
      tableHeader: [
        // 表头信息，可根据minWidth修改宽度
        { prop: "id", label: "编号", minWidth: "150px" },
        { prop: `creatorName`, label: "创建者" },
        { prop: `creationTime`, label: "创建时间" },
        { prop: `lastModifierName`, label: "修改者" },
        { prop: `lastModificationTime`, label: "创建时间" },
        { prop: "result", label: "结果" },
      ],
    };
  },
  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/medicalpackage/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() {
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        putapi(
          `/api/app/medicalpackage/updatemanysort?id=${
            this.form.id
          }&SortType=${2}`
        ).then((res) => {
          this.$message.success("操作成功");
          this.getlist();
        });
      }
    },
    topping() {
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        putapi(
          `/api/app/medicalpackage/updatemanysort?id=${
            this.form.id
          }&SortType=${1}`
        ).then((res) => {
          this.$message.success("操作成功");
          this.getlist();
        });
      }
    },
    //删除
    delets(row) {
      if (this.form.id == undefined) {
        this.$message.warning("请选择删除的数据");
      } else {
        this.$confirm("是否确认删除,是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            deletapi(
              `/api/app/sex-hormone-reference-range/${this.form.id}`
            ).then((res) => {
              this.$message.success("删除成功");
              this.getlist();
            });
          })
          .catch(() => {});
      }
    },
    //编辑弹框
    editpopup() {
      if (this.form.id == undefined) {
        this.$message.warning("请点击选择操作的数据");
      } else {
        this.dialogVisible = true;
        this.title = 2;
        this.addtoedit();
        getapi(`/api/app/sex-hormone-reference-range/${this.form.id}`).then(
          (res) => {
            this.form = res.data;
            console.log(res);
          }
        );
      }
    },
    //确定新增或者编辑
    onsbmit() {
      this.$refs.form.validate((v) => {
        if (v) {
          if (this.title == 1) {
            postapi("/api/app/sex-hormone-reference-range", this.form).then(
              (res) => {
                this.$message.success("新增成功");
                this.getlist();
                this.dialogVisible = false;
              }
            );
          } else if (this.title == 2) {
            putapi(
              `/api/app/sex-hormone-reference-range/${this.form.id}`,
              this.form
            ).then((res) => {
              this.$message.success("修改成功");
              this.getlist();
              this.dialogVisible = false;
            });
          }
        }
      });
    },
    rowclick(val) {
      getapi(`/api/app/sex-hormone-reference-range/${val.id}`).then((res) => {
        this.form = res.data;
        console.log(res);
      });
    },
    handleRowClick(Row) {
      console.log(Row);
      console.log("1");
    },
    // 新增或者编辑需要的类别
    addtoedit() {
      getapi("/api/app/item/in-filter").then((res) => {
        this.itemId = res.data.items;
      });
      postapi("/api/app/diagnosis/getlistinfilter").then((res) => {
        this.lowerDiagnosisId = res.data.items;
        this.upperDiagnosisId = res.data.items;
      });
      getapi("/api/app/sex-hormone-term/in-filter").then((res) => {
        this.sexHormoneTermId = res.data.items;
        console.log(res, "rr");
      });
    },
    //新增弹框
    add() {
      this.addtoedit();
      this.dialogVisible = true;
      this.title = 1;
      this.form = {};
    },
    getlist() {
      getapi("/api/app/sex-hormone-reference-range", this.pages).then((res) => {
        console.log(res);
        this.tableData = res.data.items;
        this.initTableData = [...res.data.items];
      });
      let that = this;
    },
  },
};
</script>
<style scoped>
.box {
  display: flex;
}
</style>
