<template>
  <p class="tip">开启后，用户可在组织管理页面，通过项目ID和申请码加入此项目。
    通过此方法加入项目的用户在项目中为项目成员，在组织中为组织临时成员。
  </p>
  <div class="setting-item">
    <div class="setting-label-div">
      <div class="setting-label">申请码加入</div>
    </div>
    <div class="setting-value-div">
      <a-switch class="setting-switch" v-model:checked="props.project.isAllowRequestCode" @change="openRequestCode"/>
    </div>
  </div>
  <div class="setting-title" v-if="props.project.isAllowRequestCode">
    <div class="setting-title-content">申请码</div>
  </div>
  <div class="setting-name-input-div" v-if="props.project.isAllowRequestCode">
    <a-input class="setting-name-input" :disabled="disable" v-model:value="props.project.requestCode" ref="requestCode" />
  </div>
  <div class="setting-title" v-if="props.project.isAllowRequestCode">
    <div class="setting-title-content">截止时间</div>
  </div>
  <div class="setting-name-input-div" v-if="props.project.isAllowRequestCode">
    <a-date-picker class="setting-date" :disabled="disable" v-model:value="props.project.requestCodeCutOffTime" :allowClear="false" show-time placeholder="请选择日期" />
  </div>
  <div class="button-div" v-if="props.project.isAllowRequestCode">
    <a-button v-if="disable" class="button-item" style="margin-right: 15px;" type="primary" @click="openChange(false)">编辑</a-button>
    <a-button v-if="disable" class="button-item" type="primary" @click="copyRequestCode">复制申请码</a-button>
    <a-button v-if="!disable" class="button-item" style="margin-right: 15px;background: #6b6b6b;" type="primary" @click="openChange(true)">取消</a-button>
    <a-button v-if="!disable" class="button-item" type="primary" @click="openChange(true)">确定</a-button>
  </div>
</template>

<script setup lang="ts">
import { defineProps, defineEmits, ref } from "vue"
import dayjs from 'dayjs';
const props = defineProps(['project'])
const emits = defineEmits([])

const disable = ref(true)
const openChange = (type) => {
  disable.value = type
}
const requestCode = ref()
const generateRandomCode = (length = 6) => {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';
  for (let i = 0; i < length; i++) {
    result += chars.charAt(Math.floor(Math.random() * chars.length));
  }
  return result;
}
const openRequestCode = (e) => {
  console.log(e)
  if(e){
    props.project.requestCode = generateRandomCode()
    props.project.requestCodeCutOffTime = dayjs().add(1, 'day')
  }else{
    props.project.requestCode = ''
    props.project.requestCodeCutOffTime = ''
  }
}
const copyRequestCode = () => {
  const textarea = document.createElement('textarea');
  textarea.value = props.project.requestCode;
  textarea.style.position = 'fixed'; // 防止页面滚动
   document.body.appendChild(textarea);
  textarea.focus();
  textarea.select();
   try {
    const success = document.execCommand('copy');
    alert(success ? '复制成功' : '复制失败');
  } catch (err) {
    alert('复制失败');
  }
  document.body.removeChild(textarea);
}
</script>

<style lang="scss" scoped>
.tip{
  font-family: 'PingFangSC-Medium';
  font-weight: 500;
  font-size: 13px;
  color: #fff;
  opacity: 0.7;
}
.setting-item{
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-family: 'PingFangSC-Medium';
  font-weight: 500;
  font-size: 13px;
  color: #FFFFFF;
  line-height: 18px;
  text-shadow: 0px 10px 30px rgba(0,0,6,0.15);
  margin-bottom: 10px;
  .setting-label-div{
    display: flex;
    align-items: center;
  }
  .setting-value-div{
    display: flex;
    align-items: center;
    cursor: pointer;
    .arrow-icon{
      width: 8px;
      height: 12px;
      background-image: url('/public/projecthome/arrow_icon.png');
      background-size: 100% 100%;
      margin-left: 8px;
    }
    .setting-switch{
      min-width: 40px;
      width: 40px;
      height: 21px;
      background: #ccc;
    }
    :deep(.ant-switch-checked){
      background: #2941B0;
    }
  }
}
.setting-title{
  display: flex;
  align-items: center;
  font-family: 'PingFangSC-Medium';
  font-weight: 500;
  font-size: 13px;
  margin-bottom: 9px;
  line-height: 20px;
  text-shadow: 0px 10px 30px rgba(0,0,6,0.15);
  .setting-title-content{
    color: #ffffff;
  }
}
.setting-name-input-div{
  margin-bottom: 13px;
  .setting-name-input{
    height: 30px;
    background: #151823;
    box-shadow: 0px 10px 30px 0px rgba(0,0,6,0.15);
    border-radius: 2px;
    border: 1px solid #3B4154;
    color: #FFFFFF;
    line-height: 18px;
    text-shadow: 0px 10px 30px rgba(0,0,6,0.15);
    font-family: 'PingFangSC-Regular';
    font-weight: 400;
    font-size: 13px;
  }
  .setting-date{
    width: 100%;
    height: 30px;
    background: #151823;
    box-shadow: 0px 10px 30px 0px rgba(0,0,6,0.15);
    border-radius: 2px;
    border: 1px solid #3B4154;
    :deep(input){
      color: #FFFFFF;
      font-family: 'PingFangSC-Regular';
      font-weight: 400;
      font-size: 13px;
    }
    :deep(input::placeholder){
      color: #FFFFFF;
      font-family: 'PingFangSC-Regular';
      font-weight: 400;
      font-size: 13px;
    }
  }
}
.button-div{
  display: flex;
  .button-item{
    background: #3A57E8;
    border-radius: 4px;
    font-family: 'PingFangSC-Medium';
    font-weight: 500;
    font-size: 13px;
    color: #FFFFFF;
    text-shadow: 0px 10px 30px rgba(0,0,6,0.15);
  }
}
</style>
