<template>
  <div @click="openDrawer">
    <slot></slot>
  </div>
  <a-drawer
    :visible="visible"
    class="drawerStyle unlockmode"
    placement="right"
    width="700"
  >
    <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="classify">
          <div
            v-for="(item, index) in classify"
            :key="index"
            class="classifyItem"
            @click="selectClassify(item)"
            :style="{
              color: item.type === classifyActive ? '#FFFFFF' : '#999999',
              'background-color':
                item.type === classifyActive ? '#4ea6ff' : '#FFFFFF',
              border:
                item.type === classifyActive
                  ? '1px solid #4ea6ff'
                  : '1px solid #999999',
            }"
          >
            {{ item.text }}
          </div>
        </div>
        <div
          v-if="classifyActive === 1"
          @click="updateLockMode(1)"
          class="type1"
        >
          <span style="font-weight: 500">描述：</span
          ><span>不设学习限制，学员可以在任何时间学习</span>
        </div>
        <div
          v-if="classifyActive === 2"
          @click="updateLockMode(2)"
          class="type1 type3"
        >
          <div>
            <span style="font-weight: 500">描述：</span
            ><span>前一个阶段达成目标后解锁下一个阶段</span>
          </div>
          <div class="radio" style="display: flex">
            <div style="margin-top: 1px">解锁条件：</div>
            <a-radio-group v-model:value="routerInfoData.unlockMode">
              <div>
                <a-radio :value="2"
                  >逐个任务解锁，完成一个任务后解锁下一个
                </a-radio>
              </div>
              <div v-if="types !== 1" style="margin-top: 24px">
                <a-radio :value="3"
                  >完成当前阶段所有必修任务解锁下一阶段
                </a-radio>
              </div>
            </a-radio-group>
          </div>
        </div>
      </div>
      <div class="btnn">
        <button class="btn2" @click="closeDrawer">取消</button>
        <button class="btn2" @click="saveUnlock">确定</button>
      </div>
    </div>
  </a-drawer>
</template>

<script setup>
import { computed, defineEmits, defineProps, ref, watch } from "vue";
import { editProjectModel, editRouterModel } from "@/api/indexLearningPath";

const emit = defineEmits({});
const visible = ref(false);
const classify = [
  {
    type: 1,
    text: "自由学习模式",
  },
  {
    type: 2,
    text: "闯关模式",
  },
];
const props = defineProps({
  routerInfo: {},
  types: Number,
});
const routerInfoData = ref({});
const classifyActive = computed(() =>
  routerInfoData.value.unlockMode === 1 ? 1 : 2
);
watch(
  () => props.routerInfo,
  () => {
    routerInfoData.value = props.routerInfo;
    console.log("routerInfoData", routerInfoData);
  }
);
const closeDrawer = () => {
  visible.value = false;
};

function selectClassify(e) {
  console.log("选择模式", e, props.types);
  if (routerInfoData.value.status !== 3 && props.types === 2) {
    routerInfoData.value.unlockMode = e.type;
  }
  if (routerInfoData.value.status !== 1 && props.types === 1) {
    routerInfoData.value.unlockMode = e.type;
  }
  // if (e.type === 1) {
  //   routerInfoData.value.unlockMode = e.type;
  // }
  // // 当为学习路径图时候 直接默认第一个选项 不用选中
  // if (e.type === 2 && props.types === 1) {
  //   routerInfoData.value.unlockMode = e.type;
  // }
}

const saveUnlock = () => {
  emit("update:userInfo", routerInfoData.value);
  if (props.types === 2) {
    editProjectModel(routerInfoData.value);
  } else {
    editRouterModel(routerInfoData.value);
  }
  closeDrawer();
};

function updateLockMode(type) {
  console.log("22222", type);
  routerInfoData.value.unlockMode = type;
}

function openDrawer() {
  visible.value = true;
}
</script>
<style lang="scss">
.unlockmode {
  .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 {
      display: flex;
      flex-direction: column;

      .classify {
        display: flex;

        .classifyItem {
          width: 160px;
          height: 38px;
          display: flex;
          align-items: center;
          justify-content: center;
          border-radius: 4px;
          border: 1px solid #999999;
          font-size: 16px;
          font-weight: 500;
          color: #999999;
          line-height: 22px;
          margin-right: 16px;
          cursor: pointer;
        }
      }

      .type1 {
        margin-top: 50px;
        font-size: 14px;
        font-weight: 400;
        color: #333333;
        line-height: 20px;
      }

      .radio {
        margin-top: 24px;
      }
    }

    .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>
