<template>
  <div class="box">
    <div style="width: 95%">
      <el-card style="height: 800px">
        <el-table
          :data="tableData"
          width="100%"
          height="350px"
          v-loading="loading"
          row-key="id"
          class="el-table__body-wrapper tbody"
          border:stripe="true"
          @row-click="rowick"
          highlight-current-row
        >
          <el-table-column prop="id" label="编号" width="300">
          </el-table-column>
          <el-table-column prop="displayName" label="名称"> </el-table-column>
          <el-table-column prop="createUserName" label="创建者">
          </el-table-column>
          <el-table-column prop="creationTime" label="创建时间" width="180">
            <template slot-scope="scope">
              {{ scope.row.creationTime | dateFormat }}
            </template>
          </el-table-column>
          <el-table-column prop="lastModifierName" label="修改者">
          </el-table-column>
          <el-table-column
            prop="lastModificationTime"
            label="修改时间"
            width="180"
          >
            <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>
        <!-- 新增弹框 -->
        <el-dialog
          :title="title == 1 ? '新增' : '修改'"
          :visible.sync="dialogVisible"
          width="53%"
          :close-on-click-modal="false"
        >
          <el-form ref="form" :model="form" label-width="80px" :rules="rules">
            <el-row>
              <el-col :span="12">
                <el-form-item label="编号">
                  <el-input v-model="form.id" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="名称" prop="displayName">
                  <el-input
                    v-model="form.displayName"
                    ref="refinput"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="标本类型" prop="sampleTypeId">
                  <el-select v-model="form.sampleTypeId" placeholder="请选择">
                    <el-option
                      v-for="item in sampleTypeId"
                      :key="item.id"
                      :label="item.displayName"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="标本容器" prop="sampleContainerId">
                  <el-select
                    v-model="form.sampleContainerId"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in sampleContainerId"
                      :key="item.id"
                      :label="item.displayName"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="6">
                <el-form-item label="创建者">
                  <el-input v-model="form.creatorName" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="创建时间">
                  <el-input :value="form.creationTime" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="修改者">
                  <el-input v-model="form.lastModifierName" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="修改时间">
                  <el-input
                    :value="form.lastModificationTime"
                    disabled
                  ></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>
        <div style="margin-top: 20px" class="intermediatesubject">
          <div>
            <span style="margin-top: 10px; padding: 5px">项目类别</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>
          </div>
          <!--  -->
          <span style="margin-left: 10%; padding: 5px; margin-top: 10px"
            >搜索选择</span
          >
          <el-select
            value-key="id"
            filterable
            v-model="flitvalues"
            placeholder="请选择"
            style="margin-top: 5px"
            @change="selectchange"
          >
            <el-option
              v-for="item in filetelists"
              :key="item.id"
              :label="item.displayName"
              :value="item"
            >
            </el-option>
          </el-select>
        </div>
        <!-- 111 -->
        <div style="display: flex; padding: 10px">
          <div class="instrumentcategory">
            <div class="leftbox">
              <div class="weixuan">未选项目</div>
              <div
                v-for="(item, index) in ites"
                :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="leftbox" style="margin-left: 5%">
              <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></div>
        </div>
        <!-- <el-transfer
              v-model="checked"
              :data="thetatabe"
              filterable
              :button-texts="['到左边', '到右边']"
              :titles="['已选', '未选']"
              :props="{ key: 'id', label: 'displayName' }"
              @change="handlechang"
            >
            </el-transfer> -->
        <div class="confirmcancellation">
          <el-button :disabled="isdislob" @click="Onsubmit">确定</el-button>
          <el-button :disabled="isdislob">取消</el-button>
        </div>
      </el-card>
    </div>
    <!-- 按钮区域 -->
    <div style="margin-left: 10px; margin-top: 4rem">
      <el-button type="primary" @click="addll">新增</el-button>
      <div style="margin-top: 10px">
        <el-button type="primary" @click="editreport">编辑</el-button>
      </div>
      <div style="margin-top: 10px">
        <el-button type="danger" @click="deleteid">删除</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="setlow">置底</el-button>
      </div>
      <div style="margin-top: 10px">
        <el-button type="primary" :disabled="isshow" @click="assertion"
          >排序</el-button
        >
      </div>
      <div style="margin-top: 10px">
        <el-button type="primary" :disabled="isshow" @click="cancellation"
          >取消</el-button
        >
      </div>
    </div>
  </div>
</template>
<script>
import Sortable from "sortablejs";
import {
  groupinglist,
  samplegroup,
  samplegropid,
  barcodeediting,
  barcodedeleteid,
  barcodedetopbottom,
  barcodedraganddrop,
  projectlist,
  itemtypeid,
  sampleasbitemid,
  asbitmfilte,
  batchcreaion,
} from "@/request/commonapi";
import { specimenlist, containerlist } from "@/request/systemapi";
export default {
  data() {
    return {
      rules: {
        displayName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
        sampleTypeId: [
          { required: true, message: "请选择标本类型", trigger: "change" },
        ],
        sampleContainerId: [
          { required: true, message: "请选择标本容器", trigger: "change" },
        ],
      },
      isdislob: true,
      filetelists: [],
      flitvalues: [],
      checked: [],
      tableHeight: window.innerHeight - 500, //表格动态高度
      screenHeight: window.innerHeight, //内容区域高度
      isshow: true,
      form: {
        displayName: "",
        sampleTypeId: "",
        sampleContainerId: "",
      },
      dialogVisible: false,
      title: 1,
      loading: false,
      pages: {
        SkipCount: 0,
        MaxResultCount: 100,
        Sorting: "displayOrder desc",
      },
      tableData: [],
      initTableData: [],
      sampleTypeId: [], //标本类型id
      sampleContainerId: [], //标本容器id
      projectid: [],
      ites: [], //左侧框数据
      rightdata: [], //右侧数据
      num: "",
      nums: "",
      values: "",
      rightobj: {},
      leftobj: {},
      ischank: 1,
      filete: {
        Filter: "",
      },

      ary1: [],
      ary2: [],
      sdate: [],
      curRow: {},
    };
  },
  created() {
    this.getlist();
    this.posjectlist();
    this.getasbitem();
  },
  mounted() {
    this.rowDrop();
    window.onresize = () => {
      return (() => {
        // window.innerHeight:浏览器的可用高度
        window.screenHeight = window.innerHeight;
        this.screenHeight = window.screenHeight;
      })();
    };
  },
  watch: {
    // 监听screenHeight从而改变table的高度
    // screenHeight(val) {
    //   this.screenHeight = val;
    //   this.tableHeight = this.screenHeight - 220;
    // },
  },
  methods: {
    selectchange(v) {
      console.log(v);
      this.rightdata.push({ displayName: v.displayName });
      // console.log(v);
    },
    Onsubmit() {
      let sampleGroupId = this.form.id;
      console.log(this.form.id);
      console.log(this.rightdata);
      let sdate = [];
      this.rightdata.forEach((element) => {
        let sss = { sampleGroupId: sampleGroupId, asbitemId: element.id };
        sdate.push(sss);
      });
      this.ites.forEach((element) => {
        let sss = { sampleGroupId: sampleGroupId, asbitemId: element.id };
        sdate.push(sss);
      });
      console.log(sdate);
      if (sampleGroupId == undefined) {
        this.$message.warning("请选择列表");
      } else {
        batchcreaion(sdate).then((res) => {
          if (res.code == 0) {
            this.$message.success("操作成功");
          }

          this.isdislob = true;
        });
      }
    },
    // 获取组合项目列表带搜索
    getasbitem() {
      asbitmfilte(this.filete).then((res) => {
        this.filetelists = res.data.items;
        console.log(res, "res");
      });
    },
    //全添加
    addall() {
      this.isdislob = false;
      if (this.ites.length > 0) {
        this.ary1 = this.ites;
        this.ary2 = this.rightdata;
        this.updatedate();
        this.ites = [];
        // this.ites.forEach((item) => {
        //   let iscz = false;
        //   this.rightdata.forEach((item2) => {
        //     if (item2.id == item.id) {
        //       iscz = true;
        //     }
        //   });
        //   if (!iscz) {
        //     console.log(item.id);
        //     this.rightdata.push(item);
        //     // this.allid.push({ id: item.id });
        //   }
        // });
        // this.ites = [];
        // console.log(this.allid);
      }
    },
    //q全移去
    allclear() {
      this.isdislob = false;
      if (this.rightdata.length > 0) {
        this.ary1 = this.rightdata;
        this.ary2 = this.ites;
        this.updatedate();
        this.rightdata = [];
      }
    },
    //左侧添加按钮
    addobj() {
      if (this.ites.length > 0) {
        this.isdislob = false;
        this.ites.splice(this.num, 1);
        this.rightdata.push(this.rightobj);
        // this.sdate.push({ id: this.leftobj.id });
        this.updatedate();
        // console.log(this.sdate);
        //  this.rightdata.forEach(item=>{
        //  })
      }
    },
    //右侧双击移动删除
    rightdelite(item, index) {
      this.isdislob = false;
      this.rightdata.splice(this.num, 1);
      this.ites.push(item);
      this.isdislob = false;
      this.updatedate();
    },
    //右侧点击选中
    rightindex(item, index) {
      this.nums = index;
      this.leftobj = item;
      this.isdislob = false;
      console.log(this.leftobj);
    },
    //左侧双击事件
    shuanji(item, index) {
      this.ites.splice(index, 1);
      this.rightdata.push(item);
      this.isdislob = false;
    },
    //右移动
    removeright() {
      if (this.rightdata.length > 0) {
        this.rightdata.splice(this.num, 1);
        this.ites.push(this.leftobj);
        this.isdislob = false;
        this.updatedate();
      }
    },
    clickPaixu(item, index) {
      console.log(item);
      this.rightobj = item;
      // let arr = [];
      // arr.push({ itemId: this.rightobj.id });
      // this.allid.forEach((item) => {
      //   if (item.id == arr[id]) {
      //     return;
      //   } else {
      //     this.allid.push({ id: arr[id] });
      //   }
      // });
      console.log(this.allid);
      this.ischank = 2;
      this.num = index;
      this.isdislob = false;
    },
    //科室选择
    ischangs(v) {
      itemtypeid(v).then((res) => {
        this.ites = res.data;
        this.ites[1] = {};
        // let dq = res.data;
        // if (dq.length > 0) {
        //   dq.forEach((element) => {
        //     let bb = { displayName: element.displayName, id: element.id };
        //     this.ites.push(bb);
        //   });
        // }
      });
    },
    // 获取项目类别接口
    posjectlist() {
      projectlist().then((res) => {
        console.log(res);
        this.projectid = res.data;
      });
    },
    handlechang(value, direction, movedkeys) {
      console.log(value);
      console.log(direction);
      console.log(movedkeys);
    },
    //取消按钮
    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 });
        }
      });
      // console.log('assertion', result);
      barcodedraganddrop({ 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));
          },
        });
      });
    },
    //置底
    setlow() {
      this.form = { ...this.curRow };
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        barcodedetopbottom(this.form.id, 2).then((res) => {
          console.log(res, "3333");
          this.getlist();
          this.$message.success("操作成功");
        });
      }
    },
    //置顶
    topping() {
      this.form = { ...this.curRow };
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        barcodedetopbottom(this.form.id, 1).then((res) => {
          this.$message.success("操作成功");
          this.getlist();
        });
      }
    },
    //删除
    deleteid() {
      this.form = { ...this.curRow };
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        this.$confirm("是否确认删除, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          barcodedeleteid(this.form.id).then((res) => {
            console.log(res);
            if (res.code == 0) {
              this.$message.success("删除成功");
              this.getlist();
            }
          });
        });
        // barcodedeleteid(this.form.id).then((res) => {
        //   console.log(res);
        //   this.$message.success("删除成功");
        //   this.getlist();
        // });
      }
    },
    //编辑弹框
    editreport() {
      this.form = { ...this.curRow };
      if (this.form.id == undefined) {
        this.$message.warning("请选择操作的数据");
      } else {
        this.dialogVisible = true;
        this.title = 2;
        //获取标本类型id
        specimenlist(this.pages).then((res) => {
          this.sampleTypeId = res.data.items;
        });
        // 获取标本容器Id
        containerlist(this.pages).then((res) => {
          this.sampleContainerId = res.data.items;
        });
        samplegropid(this.form.id).then((res) => {
          this.form = res.data;
        });
      }
    },
    //确定新增或者编辑
    addoredit() {
      this.$refs.form.validate((v) => {
        if (v) {
          if (this.title == 1) {
            samplegroup({
              displayName: this.form.displayName,
              sampleTypeId: this.form.sampleTypeId,
              sampleContainerId: this.form.sampleContainerId,
            }).then((res) => {
              this.$message.success("新增成功");
              this.getlist();
              this.dialogVisible = false;
            });
          } else if (this.title == 2) {
            barcodeediting(this.form.id, {
              displayName: this.form.displayName,
              sampleTypeId: this.form.sampleTypeId,
              sampleContainerId: this.form.sampleContainerId,
            }).then((res) => {
              this.$message.success("修改成功");
              this.dialogVisible = false;
              this.getlist();
            });
          }
        }
      });
    },
    //新增弹框
    addll() {
      this.dialogVisible = true;
      this.title = 1;
      this.form = {};
      this.$nextTick(() => {
        this.$refs.refinput.focus();
      });

      //获取标本类型id
      specimenlist(this.pages).then((res) => {
        this.sampleTypeId = res.data.items;
        console.log(res);
      });
      // 获取标本容器Id
      containerlist(this.pages).then((res) => {
        this.sampleContainerId = res.data.items;
        console.log(res, "11111");
      });
    },
    rowick(row) {
      samplegropid(row.id).then((res) => {
        this.curRow = { ...res.data };
        this.form = res.data;
      });
      sampleasbitemid(row.id).then((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);
          });
        }
        // this.rightdata = res.data;
        // console.log(res);
      });
    },
    getlist() {
      this.loading = true;
      groupinglist(this.pages).then((res) => {
        this.loading = false;
        this.tableData = res.data.items;
        this.initTableData = [...res.data.items];
        console.log(res);
      });
    },
    //ary1 需要追加的数据  ary2目标数组
    updatedate() {
      this.ary1.forEach((item) => {
        let iscz = false;
        this.ary2.forEach((item2) => {
          if (item2.id == item.id) {
            iscz = true;
          }
        });
        if (!iscz) {
          console.log(item.id);
          this.ary2.push(item);
          // this.allid.push({ id: item.id });
        }
      });
    },
  },
};
</script>
<style scoped>
.intermediatesubject {
  /* width: 100%; */
  display: flex;
  justify-content: center;
}
.confirmcancellation {
  margin-left: 50%;
}
.Selectbutton {
  margin-left: 3%;
}
.activetext {
  background: rgb(185, 203, 235);
}
.weixuan {
  text-align: center;
  line-height: 20px;
  background: rgb(185, 203, 235);
}
.leftbox {
  width: 200px;
  height: 240px;
  border: 1px solid #ccc;
  margin-left: 6%;
  overflow-x: hidden;
  overflow-y: scroll;
}
.instrumentcategory {
  width: 100%;
  display: flex;
  justify-content: center;
  /* margin-left: 15%; */
}
.el-card__body {
  height: 700px;
}
.box {
  display: flex;
}
</style>
