<template>
  <a-drawer
    :visible="Svisible"
    class="drawerStyle subset"
    placement="right"
    width="660px"
    @after-visible-change="afterVisibleChange"
  >
    <div class="drawerMain">
      <div class="header">
        <div class="headerTitle">随机分组</div>
        <img
          style="width: 29px; height: 29px; cursor: pointer"
          src="../../assets/images/basicinfo/close.png"
          @click="closeDrawer"
        />
      </div>
      <div class="main">
        <div class="group">
          <span>创建：</span>
          <a-input-number
            :min="1"
            :precision="0"
            style="width: 64px; height: 40px; border-radius: 8px"
            v-model:value="groupNum"
          />
          <span style="margin-left: 8px">个小组，</span>
          <span>每组：</span>
          <a-input-number
            :min="1"
            :precision="0"
            style="width: 64px; height: 40px; border-radius: 8px"
            v-model:value="peopleNum"
          />
          <span style="margin-left: 8px">个学员</span>
          <button class="sure" @click="showAddGroup">确定</button>
        </div>
        <!-- <div class="groupl">小组：</div> -->
        <div>
          <div class="groupin" v-for="(value, index) in groupNum2" :key="index">
            <a-input
              v-model:value="value.groupName"
              style="border-radius: 8px; height: 40px"
            />
            <a-input-number
              :min="1"
              :precision="0"
              style="width: 64px; height: 40px; border-radius: 8px"
              v-model:value="value.capacity"
            />
            <span style="margin-left: 3px">人</span>
            <div class="delete" @click="deleteGroup(value)">删除</div>
          </div>
        </div>
      </div>
      <!-- <div class="addgroup" @click="showAddGroup">
        <img src="../../assets/images/courseManage/add0.png" />
        <span class="grot">创建小组</span>
      </div> -->
      <!-- <div class="zhu">注：随机分组不对小组长生效</div> -->
      <div class="btnn">
        <button class="btn2" @click="closeDrawer">取消</button>
        <button class="btn2" @click="addGroup">确定</button>
      </div>
    </div>
    <!-- 创建小组抽屉 -->
    <add-group v-model:Avisible="Avisible" />
  </a-drawer>
</template>

<script>
import { reactive, toRefs } from "vue";
import AddGroup from "./AddGroup.vue";
import { message } from "ant-design-vue";
import { randomGroup } from "@/api/index1";
export default {
  name: "SubsetManage",
  components: { AddGroup },
  props: {
    Svisible: {
      type: Boolean,
      default: false,
    },
    projectId: {
      type: Number,
      default: null,
    },
    getGroup: {
      type: Function,
      default: null,
    },
  },
  setup(props, ctx) {
    const state = reactive({
      Avisible: false,
      value1: "",
      value2: "",
      value3: "",
      groupNum: null,
      peopleNum: null,
      groupNum2: [],
      peopleNum2: 0,
    });
    const closeDrawer = () => {
      ctx.emit("update:Svisible", false);
    };

    const afterVisibleChange = (bool) => {
      console.log("state", bool);
    };

    const showDrawer = () => {
      state.Svisible = true;
    };

    // const showAddGroup = () => {
    //   state.Avisible = true;
    // };
    function uuid() {
      //获取当前时候
      return new Date().getSeconds()+''+Math.round(Math.random()*100);
      // const temp_url = URL.createObjectURL(new Blob());
      // const uuid = temp_url.toString();
      // URL.revokeObjectURL(temp_url);
      // return uuid.substr(uuid.lastIndexOf("/") + 1);
    }
    //展示要增加的小组
    const showAddGroup = () => {
      if (!state.groupNum) return message.warning("请输入要添加的小组数");
      if (!state.peopleNum) return message.warning("请输入每组的人员数");
      let arr = [];
      for (let i = 0; i < state.groupNum; i++) {
        let obj = {
          key: i,
          groupName: uuid(),
          capacity: state.peopleNum,
        };
        arr.push(obj);
      }
      state.groupNum2 = arr;
      state.peopleNum2 = state.peopleNum;
    };
    //删除单个小组
    const deleteGroup = (item) => {
      for (let i = 0; i < state.groupNum2.length; i++) {
        if (item.key === state.groupNum2[i].key) {
          state.groupNum2.splice(i, 1);
        }
      }
    };
    //添加小组
    const addGroup = () => {
      state.loading = true;
      console.log("state.groupNum2", state.groupNum2);
      let obj = {
        pid: props.projectId,
        randomGroups: state.groupNum2,
      };
      ctx.emit("update:Svisible", false);
      randomGroup(obj, props.projectId)
        .then((res) => {
          console.log("随机分组结果", res);
          if (res.data.code === 200) {
            message.success("小组添加成功");
            props.getGroup && props.getGroup();
          }
          // 清空数据
          state.groupNum = null;
          state.peopleNum = null;
          state.groupNum2 = [];
          state.loading = false
        })
        .catch((err) => {
          console.log("随机分组失败", err);
        });
    };

    return {
      ...toRefs(state),
      afterVisibleChange,
      showDrawer,
      closeDrawer,
      showAddGroup,
      addGroup,
      // change,
      deleteGroup,
    };
  },
};
</script>

<style scoped lang="scss">
.subset {
  .ant-drawer-content-wrapper {
    // max-width: 1000px;
    .ant-drawer-header {
      display: none !important;
    }
    .ant-drawer-body {
      padding: 0;
    }
  }
  .drawerMain {
    min-width: 600px;
    margin: 0px 32px 0px 32px;
    overflow-x: auto;
    display: flex;
    flex-direction: column;
    .header {
      height: 73px;
      border-bottom: 1px solid #e8e8e8;
      display: flex;
      justify-content: space-between;
      align-items: center;
      // background-color: red;
      margin-bottom: 20px;
      .headerTitle {
        font-size: 18px;
        font-weight: 600;
        color: #333333;
        line-height: 25px;
        // margin-left: 24px;
      }
    }
    .main {
      .ant-input-number-input {
        height: 38px;
      }
      .ant-input-number {
        margin-left: 10px;
      }
      .group {
        display: flex;
        align-items: center;
        font-size: 16px;
        font-weight: 400;
        color: rgba(0, 0, 0, 0.85);
        line-height: 22px;
        .sure {
          cursor: pointer;
          width: 100px;
          height: 40px;
          background: #4ea6ff;
          border-radius: 8px;
          border: 0;
          margin-left: 15px;
          color: #fff;
          margin-left: 32px;
        }
      }
      .groupin {
        display: flex;
        align-items: center;
        margin-top: 32px;
        font-size: 16px;
        font-weight: 400;
        color: rgba(0, 0, 0, 0.85);
        line-height: 22px;
        .ant-input {
          border-radius: 5px;
          // height: 120%;
          width: 384px;
          height: 40px;
        }
        .delete {
          color: rgba(56, 125, 247, 1);
          font-size: 14px;
          margin-left: 10px;
          cursor: pointer;
        }
        .peopleNum {
          width: 56px;
          height: 40px;
          border-radius: 8px;
          border: 1px solid #c7cbd2;
          display: flex;
          align-items: center;
          justify-content: center;

          margin-left: 12px;
        }
      }
      .groupl {
        color: rgba(0, 0, 0, 0.85);
        font-size: 16px;
        margin-top: 36px;
      }
    }
    .addgroup {
      width: 130px;
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: center;
      //margin-right: 16px;
      margin-top: 32px;
      border: 1px solid #4ea6ff;
      border-radius: 8px;
      background: #4ea6ff;
      cursor: pointer;
      .grot {
        color: #ffffff;
        margin-left: 5px;
        font-size: 14px;
      }
    }
    .zhu {
      margin-top: 24px;
      color: rgba(153, 153, 153, 1);
      font-size: 14px;
    }
    .btnn {
      height: 72px;
      width: 100%;
      position: absolute;
      bottom: 0;
      left: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      box-shadow: 0px 1px 35px 0px rgba(118, 136, 166, 0.16);
      .btn1 {
        width: 100px;
        height: 40px;
        border: 1px solid #4ea6ff;
        border-radius: 8px;
        color: #4ea6ff;
        background-color: #fff;
        cursor: pointer;
      }
      .btn2 {
        cursor: pointer;
        width: 100px;
        height: 40px;
        background: #4ea6ff;
        border-radius: 8px;
        border: 0;
        margin-left: 15px;
        color: #fff;
      }
    }
  }
}
</style>
