<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="displayName" 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="displayName">
                <el-input
                  v-model="form.displayName"
                  style="width: 80%"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="价格" prop="price">
                <el-input v-model="form.price" style="width: 80%"></el-input>
              </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="isActive">
                <el-select v-model="form.isActive" placeholder="请选择">
                  <el-option
                    v-for="item in isActive"
                    :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="remark">
                <el-input v-model="form.remark" style="width: 80%"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider></el-divider>
          <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 | dateFormat"
                  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 | dateFormat"
                  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="onsbmit">确 定</el-button>
        </span>
      </el-dialog>
      <!--  -->
      <span style="margin-top: 10px; padding: 5px; margin-left: 20.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>
    </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 {
      num: 0,
      leftdata: [],
      filetelists: [],
      flitvalues: [],
      projectid: [],
      values: [],
      isshow: true,
      rules: {
        forSexId: [
          { required: true, message: "请选择性别", trigger: "change" },
        ],
        isActive: [
          { required: true, message: "请选择启用标志", trigger: "change" },
        ],
        displayName: [
          { required: true, message: "请输入名称", trigger: "blur" },
        ],
        price: [{ required: true, message: "请输入价格", trigger: "blur" }],
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
      },
      form: {
        displayName: "",
        price: "",
        forSexId: "",
        isActive: "",
        remark: "",
      },
      isActive: [
        {
          value: "N",
          label: "否",
        },
        {
          value: "Y",
          label: "是",
        },
      ], //启用标志
      forSexId: [],
      dialogVisible: false,
      title: 1,
      pages: {
        SkipCount: 0,
        MaxResultCount: 100,
        Sorting: "displayOrder desc",
      },

      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: "拼音简码" },
      ],
      rightdata: [],
      isdislob: true,
      ary1: [],
      ary2: [],
      leftobj: {},
      rightobj: {},
      nums: 0,
    };
  },
  created() {
    this.getlist();
    this.gitprojectcategory();
  },
  mounted() {
    this.rowDrop();
  },
  methods: {
    Onsubmit() {
      let sampleGroupId = this.form.id;
      console.log(this.form.id);
      console.log(this.rightdata);
      let sdate = [];
      this.rightdata.forEach((element) => {
        let sss = { medicalPackageId: sampleGroupId, asbitemId: element.id };
        sdate.push(sss);
      });
      this.leftdata.forEach((element) => {
        let sss = { sampleGroupId: sampleGroupId, asbitemId: element.id };
        sdate.push(sss);
      });
      console.log(sdate);
      if (sampleGroupId == undefined) {
        this.$message.warning("请选择列表");
      } else {
        postapi("/api/app/medical-package-detail/many", sdate).then((res) => {
          this.$message.success("操作成功");
          this.isdislob = true;
        });
      }
    },
    //右侧点击选中
    rightindex(item, index) {
      this.nums = index;
      this.leftobj = item;
      console.log(this.leftobj);
    },
    //右侧双击
    rightdelite(item, index) {
      console.log(item);
      this.rightdata.splice(index, 1);
      this.leftdata.push(item);
    },
    //左侧双击事件
    shuanji(item, index) {
      console.log(item);
      this.leftdata.splice(index, 1);
      this.rightdata.push(item);
    },
    //全添加
    addall() {
      this.isdislob = false;
      if (this.leftdata.length > 0) {
        this.ary1 = this.leftdata;
        this.ary2 = this.rightdata;
        this.updatedate();
        this.leftdata = [];
      }
    },
    //左侧添加按钮
    addobj() {
      if (this.leftdata.length > 0) {
        this.isdislob = false;
        this.leftdata.splice(this.num, 1);
        this.rightdata.push(this.rightobj);
      }
    },
    //右移动
    removeright() {
      if (this.rightdata.length > 0) {
        this.rightdata.splice(this.nums, 1);
        this.leftdata.push(this.leftobj);
        this.isdislob = true;

        this.updatedate();
      }
    },
    //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 });
        }
      });
    },
    clickPaixu(item, index) {
      this.rightobj = item;
      this.num = index;
      console.log(this.num);
      console.log(this.rightobj);
    },
    //q全移去
    allclear() {
      this.isdislob = false;
      if (this.rightdata.length > 0) {
        this.ary1 = this.rightdata;
        this.ary2 = this.leftdata;
        this.updatedate();
        this.rightdata = [];
      }
    },
    //获取项目类别 3a0b0b10-719f-5824-6956-8cb70f204013
    gitprojectcategory() {
      getapi("/api/app/item-type").then((res) => {
        this.projectid = res.data.items;
      });
    },
    //项目类别选择
    ischangs(v) {
      console.log(v);
      getapi(`/api/app/asbitem/in-item-type/${v}`).then((res) => {
        console.log(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从大到小排
        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/medical-package/${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/medical-package/${this.form.id}`).then((res) => {
          this.form = res.data;
          console.log(res);
        });
      }
    },
    //确定新增或者编辑
    onsbmit() {
      this.$refs.form.validate((v) => {
        if (v) {
          if (this.title == 1) {
            this.form.price = Number(this.form.price);
            postapi("/api/app/medical-package", this.form).then((res) => {
              this.$message.success("新增成功");
              this.getlist();
              this.dialogVisible = false;
            });
          } else if (this.title == 2) {
            putapi(`/api/app/medical-package/${this.form.id}`, this.form).then(
              (res) => {
                this.$message.success("修改成功");
                this.getlist();
                this.dialogVisible = false;
              }
            );
          }
        }
      });
    },
    rowclick(val) {
      getapi(`/api/app/medical-package/${val.id}`).then((res) => {
        this.form = res.data;
        console.log(res);
      });
      getapi(
        `/api/app/medical-package-detail/medical-package-in-asbitem?MedicalPackageId=${val.id}`
      ).then((res) => {
        console.log(res);
        this.rightdata = res.data;
        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);
          });
        }
      });
    },
    handleRowClick(Row) {
      console.log(Row);
      console.log("1");
    },
    // 新增或者编辑需要的类别
    addtoedit() {
      getapi("/api/app/sex").then((res) => {
        this.forSexId = res.data;
      });
    },
    //新增弹框
    add() {
      this.addtoedit();
      this.dialogVisible = true;
      this.title = 1;
      this.form = {};
    },
    getlist() {
      getapi("/api/app/medical-package/in-filter", this.pages).then((res) => {
        console.log(res);
        this.tableData = res.data.items;
        this.initTableData = [...res.data.items];
      });
      let that = this;
    },
  },
};
</script>
<style scoped>
.Selectbutton {
  margin-left: 3%;
}
.weixuan {
  text-align: center;
  line-height: 20px;
  background: rgb(185, 203, 235);
}
.confirmcancellation {
  margin-left: 45%;
}
.mainleftbox {
  width: 200px;
  height: 240px;
  border: 1px solid #ccc;
  margin-left: 6%;
}
.mainbox {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.box {
  display: flex;
}
.activetext {
  background: rgb(185, 203, 235);
}
</style>