<template>
  <div class="box">
    <div style="width: 95%">
      <el-card style="border-radius: 15px;">
        <div class="publiccss">诊断</div>
        <div class="" style="display: flex">
          <div style="margin-left: 5px; margin-top: 15px; width: 15%">
            <el-tree
              :data="treedata"
              :props="defaultProps"
              @node-click="handleNodeClick"
            ></el-tree>
          </div>
          <div style="width: 85%">
            <el-table
              :data="tableData"
              style="width: 100%; margin-top: 20px; height: 500px"
              row-key="id"
              class="el-table__body-wrapper tbody"
              @row-click="rowick"
              :height="window.pageHeight < 600 ? 440 : window.pageHeight - 160"
              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="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="操作" 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>
          </div>
        </div>
        <!--  -->
        <!-- <span style="margin-top: 10px; padding: 5px; margin-left: 28.5%"
          >项目类别</span
        >
        <el-select
          v-model="values"
          placeholder="请选择"
          @change="ischangs"
          style="margin-top: 5px"
        >
          <el-option
            v-for="item in projectid"
            :key="item.id"
            :label="item.displayName"
            :value="item.id"
          >
          </el-option>
        </el-select> -->
        <!-- <span style="margin-left: 10%; padding: 5px">搜索选择</span>
        <el-select
          filterable
          v-model="flitvalues"
          placeholder="请选择"
          style="margin-top: 5px"
        >
          <el-option
            v-for="item in filetelists"
            :key="item.id"
            :label="item.displayName"
            :value="item.id"
          >
          </el-option>
        </el-select> -->
        <!-- <div class="mainbox">
          <div class="mainleftbox">
            <div class="weixuan">未选项目</div>
            <div
              v-for="(item, index) in leftdata"
              :key="index"
              @click="clickPaixu(item, index)"
              @dblclick="shuanji(item, index)"
              :class="num === index ? 'activetext' : ''"
              style=""
            >
              <div>{{ item.displayName }}</div>
            </div>
          </div>
          <div class="Selectbutton">
            <div style="margin-top: 10px">
              <el-button @click="addall"
                >全添加<i class="el-icon-caret-right"></i
                ><i class="el-icon-caret-right"></i
              ></el-button>
            </div>
            <div style="margin-top: 10px">
              <el-button @click="addobj" style="width: 111px"
                >添加<i class="el-icon-caret-right"></i>
              </el-button>
            </div>
            <div style="margin-top: 10px">
              <el-button @click="removeright" style="width: 111px"
                >移去<i class="el-icon-caret-left"></i>
              </el-button>
            </div>
            <div style="margin-top: 10px">
              <el-button @click="allclear"
                >全移去<i class="el-icon-caret-left"></i
                ><i class="el-icon-caret-left"></i>
              </el-button>
            </div>
          </div>
          <div class="mainleftbox">
            <div class="weixuan">已选项目</div>
            <div
              v-for="(item, index) in rightdata"
              :key="index"
              @click="rightindex(item, index)"
              @dblclick="rightdelite(item, index)"
              :class="nums === index ? 'activetext' : ''"
            >
              <div>{{ item.displayName }}</div>
            </div>
          </div>
        </div> -->
        <!-- <div class="confirmcancellation">
          <el-button :disabled="isdislob" @click="Onsubmit">确定</el-button>
          <el-button :disabled="isdislob">取消</el-button>
        </div> -->
      </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="100px" :rules="rules">
          <el-row>
            <el-col :span="8">
              <el-form-item label="名称" prop="displayName">
                <el-input
                  ref="refinput"
                  v-model="form.displayName"
                  style="width: 100%"
                ></el-input> </el-form-item
            ></el-col>
            <el-col :span="8">
              <el-form-item label="项目类别" prop="itemTypeId">
                <el-select v-model="form.itemTypeId" placeholder="请选择">
                  <el-option
                    v-for="item in itemTypeId"
                    :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="isIll">
                <el-select v-model="form.isIll" placeholder="请选择">
                  <el-option
                    v-for="item in isIll"
                    :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="suggestionName">
                <el-input
                  v-model="form.suggestionName"
                  style="width: 100%"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="是总检模板" prop="isSummaryTemplate">
                <el-select
                  v-model="form.isSummaryTemplate"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in isSummaryTemplate"
                    :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="forSexId">
                <el-select v-model="form.forSexId" placeholder="请选择">
                  <el-option
                    v-for="item in forSexId"
                    :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="diagnosisLevelId">
                <el-select v-model="form.diagnosisLevelId" placeholder="请选择">
                  <el-option
                    v-for="item in diagnosisLevelId"
                    :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="onsbmint">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <!-- 按钮区域 -->
    <div style="margin-left: 10px; margin-top: 5%">
      <el-button type="" @click="add" class="commonbutton">新增</el-button>
      <div style="margin-top: 10px">
        <el-button type="" @click="edit" class="commonbutton">编辑</el-button>
      </div>
      <div style="margin-top: 10px">
        <el-button type="" @click="delets" class="deleteButton">删除</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: 10px">
        <el-button
          type=""
          :disabled="isshow"
          @click="cancellation"
          class="commonbutton"
          >取消</el-button
        >
      </div>
    </div>
  </div>
</template>
<script>
import Sortable from "sortablejs";
import { postapi, getapi, putapi, deletapi } from "@/api/api";
import { projectlist } from "@/request/commonapi";
import { examinationgender, diagsislist } from "@/request/systemapi";
export default {
  data() {
    return {
      treedata: [], //树形结构
      num: 0,
      rightdata: [], //右侧数据
      leftdata: [], //左侧数据
      projectid: [],
      filetelists: [],
      flitvalues: [],
      values: "",
      isshow: true,
      isdislob: true,
      rules: {
        displayName: [
          { required: true, message: "请输入名称", trigger: "blur" },
        ],
        itemTypeId: [
          { required: true, message: "请选择项目类别", trigger: "blur" },
        ],
        suggestionName: [
          { required: true, message: "请输入建议名称", trigger: "blur" },
        ],
        isIll: [{ required: true, message: "请选择是疾病", trigger: "blur" }],
        isSummaryTemplate: [
          { required: true, message: "请选择是总检模板", trigger: "blur" },
        ],
        forSexId: [
          { required: true, message: "请选择适用性别", trigger: "blur" },
        ],
        diagnosisLevelId: [
          { required: true, message: "请选择诊断级别", trigger: "blur" },
        ],
      },
      tableData: [],
      initTableData: [],
      dialogVisible: false,
      title: 1,
      form: {
        displayName: "",
        itemTypeId: "",
        suggestionName: "",
        isIll: "",
        isSummaryTemplate: "",
        forSexId: "",
        diagnosisLevelId: "",
      },
      isIll: [
        {
          value: "Y",
          label: "是",
        },
        {
          value: "N",
          label: "否",
        },
      ], //是疾病
      isSummaryTemplate: [
        {
          value: "Y",
          label: "是",
        },
        {
          value: "N",
          label: "否",
        },
      ], //是总监模板

      itemTypeId: [], //项目类别
      forSexId: [], //性别
      diagnosisLevelId: [], //诊断级别
      ary1: [],
      ary2: [],
      nums: 0,
      curRow: {},
      defaultProps: {
        children: "treeChildren",
        label: "displayName",
      },
    };
  },
  created() {
    this.getlist();
    this.gitprojectcategory();
  },
  mounted() {
    this.rowDrop();
    this.getlisttree();
  },
  methods: {
    getlisttree() {
      getapi("/api/app/item-type/by-code-all").then((res) => {
        this.treedata = res.data;
      });
    },
    //树形结构点击
    handleNodeClick(data) {
      postapi("/api/app/diagnosis/getlistinsuggestion", {
        diagnosisName: data.diagnosisName,
        itemTypeId: data.id,
      }).then((res) => {
        this.tableData = res.data;
      });
    },
    Onsubmit() {
      let sampleGroupId = this.form.id;
      let sdate = [];
      this.rightdata.forEach((element) => {
        let sss = { sampleGroupId: sampleGroupId, asbitemId: element.id };
        sdate.push(sss);
      });
      this.leftdata.forEach((element) => {
        let sss = { sampleGroupId: sampleGroupId, asbitemId: element.id };
        sdate.push(sss);
      });
      if (sampleGroupId == undefined) {
        this.$message.warning("请选择列表");
      } else {
        postapi("/api/app/item-template-detail/many", sdate).then((res) => {
          this.isdislob = true;
        });
      }
    },
    //q全移去
    allclear() {
      this.isdislob = false;
      if (this.rightdata.length > 0) {
        this.ary1 = this.rightdata;
        this.ary2 = this.leftdata;
        this.updatedate();
        this.rightdata = [];
      }
    },
    //右移动
    removeright() {
      this.isdislob = false;
      if (this.rightdata.length > 0) {
        this.rightdata.splice(this.num, 1);
        this.leftdata.push(this.leftobj);
        this.isdislob = true;
        this.updatedate();
      }
    },
    //左侧添加按钮
    addobj() {
      this.isdislob = false;
      if (this.leftdata.length > 0) {
        this.leftdata.splice(this.num, 1);
        this.rightdata.push(this.rightobj);
      }
    },
    //全添加
    addall() {
      this.isdislob = false;
      if (this.leftdata.length > 0) {
        this.ary1 = this.leftdata;
        this.ary2 = this.rightdata;
        this.updatedate();
        this.leftdata = [];
      }
    },
    //左侧双击事件
    shuanji(item, index) {
      this.isdislob = false;
      this.leftdata.splice(index, 1);
      this.rightdata.push(item);
    },
    clickPaixu(item, index) {
      this.rightobj = item;
      this.num = index;
    },
    //获取项目类别 3a0b0b10-719f-5824-6956-8cb70f204013
    gitprojectcategory() {
      getapi("/api/app/item-type/by-code-all").then((res) => {
        this.projectid = res.data;
        // this.tableData = res.data;
      });
    },
    //ary1 需要追加的数据  ary2目标数组
    updatedate() {
      this.ary1.forEach((item) => {
        let iscz = false;
        this.ary2.forEach((item2) => {
          if (item2.id == item.id) {
            iscz = true;
          }
        });
        if (!iscz) {
          this.ary2.push(item);
          // this.allid.push({ id: item.id });
        }
      });
    },
    //项目类别选择
    ischangs(v) {
      getapi(`/api/app/diagnosis/in-item-type/${v}`).then((res) => {
        this.leftdata = res.data;
      });
    },
    //取消排序
    cancellation() {
      this.$message.info("取消操作");
      this.isshow = true;
      this.getlist();
    },
    //确定排序
    assertion() {
      const result = [];
      this.tableData.forEach((item, index) => {
        // index 从0开始的， 你的displayOrder从大到小排
        // 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/diagnosis/updatesortmany", { itemList: result }).then(
        (res) => {
          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) {
              }
            });
          },
        });
      });
    },
    toppings() {
      this.form = { ...this.curRow };
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        putapi(
          `/api/app/diagnosis/updatemanysort?id=${this.form.id}&SortType=${2}`
        ).then((res) => {
          console.log("操作成功");
          this.getlist();
        });
      }
    },
    topping() {
      this.form = { ...this.curRow };
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        putapi(
          `/api/app/diagnosis/updatemanysort?id=${this.form.id}&SortType=${1}`
        ).then((res) => {
          console.log("操作成功");
          this.getlist();
        });
      }
    },
    //删除
    delets(row) {
      this.form = { ...this.curRow };
      console.log(row);
      if (this.form.id == undefined) {
        this.$message.warning("请选择删除的数据");
      } else {
        this.$confirm("是否确认删除,是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            deletapi(`/api/app/diagnosis/${this.form.id}`).then((res) => {
              if (res.code == 0) {
                console.log("删除成功");
                this.getlist();
              }
            });
          })
          .catch(() => {
            this.$message.info("已取消操作");
          });
      }
    },
    //编辑弹框
    edit(row) {
      this.form = { ...this.curRow };
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        //项目类别
        projectlist().then((res) => {
          this.itemTypeId = res.data;
        });
        //适用性别
        getapi("/api/app/for-sex").then((res) => {
          this.forSexId = res.data;
        });
        // examinationgender().then((res) => {
        //   this.forSexId = res.data;
        // });
        //诊断级别
        diagsislist().then((res) => {
          this.diagnosisLevelId = res.data;
        });
        this.title = 2;

        this.dialogVisible = true;
      }
    },
    //确定新增或者编辑
    onsbmint() {
      this.$refs.form.validate((v) => {
        if (v) {
          if (this.title == 1) {
            postapi("/api/app/diagnosis", this.form).then((res) => {
              console.log("新增成功");
              this.getlist();
              this.dialogVisible = false;
            });
          } else if (this.title == 2) {
            putapi(`/api/app/diagnosis/${this.form.id}`, this.form).then(
              (res) => {
                console.log("修改成功");
                this.getlist();
                this.dialogVisible = false;
              }
            );
          }
        }
      });
      // postapi('/api/app/diagnosis',this)
    },
    //新增弹框
    add() {
      this.dialogVisible = true;
      this.title = 1;
      this.form = {};
      this.$nextTick(() => {
        this.$refs.refinput.focus();
      });
      //项目类别
      projectlist().then((res) => {
        this.itemTypeId = res.data;
      });
      //适用性别
      getapi("/api/app/for-sex").then((res) => {
        this.forSexId = res.data;
        console.log("适用性别", res);
      });
      // examinationgender().then((res) => {
      //   this.forSexId = res.data;
      //   console.log(res, "性别");
      // });
      //诊断级别
      diagsislist().then((res) => {
        this.diagnosisLevelId = res.data;
      });
    },
    rowick(row) {
      getapi(`/api/app/diagnosis/${row.id}`).then((res) => {
        // this.form = res.data;
        this.curRow = { ...res.data };
      });
      getapi(
        `/api/app/item-template-detail/item-template-in-item?ItemTemplateId=${row.id}`
      ).then((res) => {
        console.log(res);
        let dq = res.data;
        if (dq.length > 0) {
          this.rightdata = [];
          dq.forEach((element) => {
            let bb = { displayName: element.displayName, id: element.id };
            this.rightdata.push(bb);
          });
        }
      });
      console.log(row);
    },
    getlist() {
      postapi("/api/app/diagnosis/getlistinsuggestion").then((res) => {
        this.initTableData = [...res.data];
        this.tableData = res.data;
        console.log(res);
      });
    },
  },
};
</script>
<style scoped>
::v-deep .el-table__header th {
  /* font-size: px; */
  background-color: rgb(245, 245, 245); /* 设置表头背景颜色 */
  color: rgb(113, 113, 113); /* 设置表头文字颜色 */
}
.confirmcancellation {
  margin-left: 45%;
}
.Selectbutton {
  margin-left: 3%;
}
.activetext {
  background: rgb(185, 203, 235);
}
.weixuan {
  text-align: center;
  line-height: 20px;
  background: rgb(185, 203, 235);
}
.mainleftbox {
  width: 200px;
  height: 240px;
  border: 1px solid #ccc;
  margin-left: 6%;
}
.mainbox {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.box {
  display: flex;
}
</style>
