<template>
  <a-drawer
    :visible="Changevisible"
    class="drawerStyle changegroup"
    placement="right"
    width="70%"
    @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="onerow">将此学员移动到</div>
        <div class="secondrow">
          <a-select
            v-model:value="groupValue"
            style="width: 264px; border-radius: 8px"
            :options="stugroupList"
            allowClear
            @change="handleChange"
          />
        </div>
      </div>
      <div class="btnn">
        <button class="btn1" @click="closeDrawer">取消</button>
        <button class="btn2" @click="changeGroup">确定</button>
      </div>
    </div>
  </a-drawer>
</template>

<script>
import { reactive, toRefs } from "vue";
import { getGroupList, addStudent } from "../../api/indexProjStu";
import { message } from "ant-design-vue";

export default {
  name: "ChangeGroup",
  props: {
    Changevisible: {
      type: Boolean,
      default: false,
    },
    projectId: {
      type: Number,
      default: null,
    },
    ChangeGroupStuId: {
      type: Number,
      default: null,
    },
    ChangeGroupStuName: {
      type: String,
      default: null,
    },
    ChangeGroupId: {
      type: Number,
      default: null,
    },
  },
  setup(props, ctx) {
    const state = reactive({
      stugroupList: [
        // {
        //   id: "1",
        //   value: "好好学习",
        //   label: "好好学习",
        // },
      ],
      groupValue: null, //下拉框选择的值
      chooseGroupId: null, //下拉框选择的小组id
    });
    const closeDrawer = () => {
      ctx.emit("update:Changevisible", false);
      state.groupValue = null;
    };
    const setStuGroups = (tableData) => {
      let data = tableData;
      let array = [];
      data.map((value) => {
        let obj = {
          id: value.projectGroupId,
          value: value.groupName,
          label: value.groupName,
        };
        array.push(obj);
      });
      state.stugroupList = array;
    };

    const afterVisibleChange = (bool) => {
      console.log("state", bool);
      let obj = {
        name: "",
        pageNo: 1,
        pageSize: 12,
        projectId: props.projectId,
      };

      getGroupList(obj).then((res) => {
        console.log("在抽屉里获取了小组列表", res.data.data.rows);
        let result = res.data.data.rows;
        if (result.length > 0) {
          setStuGroups(result);
        }
      });
    };
    const handleChange = (value, item) => {
      console.log(`selected ${value}`);
      console.log(item);
      state.chooseGroupId = item.id;
    };
    const changeGroup = () => {
      // closeDrawer()
      console.log("state.chooseGroupId", state.chooseGroupId);
      console.log("props.projectId", props.projectId);
      console.log("props.ChangeGroupStuId", props.ChangeGroupStuId);
      console.log("props.ChangeGroupStuName", props.ChangeGroupStuName);
      if (state.chooseGroupId !== null) {
        if (props.ChangeGroupId == state.chooseGroupId) {
          message.destroy();
          return message.warning("学员已在该小组");
        } else {
          let obj = {
            deptList: [
              {
                id: 0,
                name: "",
              },
            ],
            groupList: [
              {
                id: 0,
                name: "",
              },
            ],
            projectGroupId: state.chooseGroupId,
            projectId: props.projectId,
            studentList: [
              {
                id: props.ChangeGroupStuId,
                name: props.ChangeGroupStuName,
              },
            ],
          };
          addStudent(obj)
            .then((res) => {
              message.destroy();
              console.log(res, "换组成功");
              ctx.emit("changeD", false);
              closeDrawer();
              return message.success("换组成功");
            })
            .catch((err) => {
              console.log(err, "换组失败");
              message.warning("换组失败");
            });
        }
      }
    };
    return {
      ...toRefs(state),
      afterVisibleChange,
      closeDrawer,
      setStuGroups,
      handleChange,
      changeGroup,
      // change,
    };
  },
};
</script>

<style lang="scss">
.changegroup {
  .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 {
      .onerow {
        margin: 32px 0 32px 24px;
        color: rgba(51, 51, 51, 1);
        font-size: 16px;
      }
      .secondrow {
        margin-left: 24px;
        .ant-select {
          height: 40px;
        }
        .ant-select-selector {
          height: 40px;
          padding: 4px 11px;
          border-radius: 8px;
        }
        .ant-select-selection-search-input {
          height: 40px;
        }
      }
    }
    .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>
