<!-- 用户编辑弹窗 -->
<template>
  <a-drawer
    :width="width"
    :visible="visible"
    :confirm-loading="loading"
    :title="'项目详情'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    :footer="null"
  >
    <a-row :gutter="16" type="flex" justify="space-around">
      <a-col :span="4">
        <div class="project-card-head">
          <div class="project-avatar">
            <a-image
              v-if="project.projectAvatar"
              :src="`https://file.wsdns.cn/${project.projectAvatar}`"
              class="avatar"
            />
            <a-avatar v-else :size="100">
              <template #icon>
                <AntDesignOutlined />
              </template>
            </a-avatar>
          </div>
          <a-tooltip title="双击切换全屏">
            <h4 class="project-title" @dblclick="onFullscreen">
              {{ project.projectName }}
            </h4>
          </a-tooltip>
          <!--          <div class="progress">-->
          <!--            <a-progress-->
          <!--              :width="60"-->
          <!--              type="circle"-->
          <!--              :steps="5"-->
          <!--              :percent="project.progress * 2"-->
          <!--            />-->
          <!--            <small class="ele-text-placeholder">项目进度</small>-->
          <!--          </div>-->
          <!--          <div class="progress">-->
          <!--            <ele-qr-code-svg :value="project.projectName" :size="120" />-->
          <!--            <small class="ele-text-placeholder">手机扫一扫</small>-->
          <!--          </div>-->
        </div>
      </a-col>
      <a-col :span="19">
        <a-form
          :label-col="{ md: { span: 4 }, sm: { span: 24 } }"
          :wrapper-col="{ md: { span: 19 }, sm: { span: 24 } }"
        >
          <div class="base-form" style="margin-bottom: 20px">
            <a-descriptions bordered>
              <a-descriptions-item :span="2" label="项目名称">
                {{ project.projectName }}
              </a-descriptions-item>
              <a-descriptions-item label="项目进度">
                <a-progress :percent="project.progress * 2" :steps="5" />
                <!--                <a-tag v-if="project.status === '0'" color="green">正常</a-tag>-->
                <!--                <a-tag v-if="project.status === '1'" color="red">禁用</a-tag>-->
              </a-descriptions-item>
              <a-descriptions-item label="项目编号">
                <div style="min-width: 90px">{{ project.projectCode }}</div>
              </a-descriptions-item>
              <a-descriptions-item label="项目分类">
                {{ project.projectCategory }}
              </a-descriptions-item>
              <a-descriptions-item label="点击次数">
                {{ project.views }}
              </a-descriptions-item>
              <a-descriptions-item :span="2" label="客户名称">
                {{ project.customerName }}
              </a-descriptions-item>
              <a-descriptions-item :span="1" label="联系人">
                {{ project.comments }}
              </a-descriptions-item>
              <a-descriptions-item :span="2" label="开发环境">
                <a href="#" @click="openUrl(project.urlDev)">
                  {{ project.urlDev }}
                </a>
              </a-descriptions-item>
              <a-descriptions-item :span="1" label="账号">
                {{ project.account }}
              </a-descriptions-item>
              <a-descriptions-item :span="2" label="生产环境">
                <a href="#" @click="openUrl(project.url)">
                  {{ project.url }}
                </a>
              </a-descriptions-item>
              <a-descriptions-item :span="1" label="密码">
                <span>gxwebsoft.com</span>
              </a-descriptions-item>
              <a-descriptions-item :span="3" label="项目简介">
                {{ project.comments }}
              </a-descriptions-item>
            </a-descriptions>
          </div>
          <a-tabs v-model:activeKey="activeKey">
            <a-tab-pane key="1" tab="项目资料">
              <div class="content" v-html="content"></div>
            </a-tab-pane>
            <a-tab-pane key="2" tab="项目附件">
              <div class="content">附件</div>
            </a-tab-pane>
          </a-tabs>
        </a-form>
      </a-col>
    </a-row>
  </a-drawer>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject, toggleFullscreen } from 'ele-admin-pro';
  import type { Project } from '@/api/oa/project/model';
  import { htmlToText } from 'ele-admin-pro';
  import { AntDesignOutlined } from '@ant-design/icons-vue';
  import { openUrl } from '@/utils/common';

  const useForm = Form.useForm;

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: Project | null;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 用户信息
  const project = reactive<Project>({
    createTime: '',
    projectName: '',
    projectCode: '',
    comments: '',
    projectCategory: '',
    projectAvatar: '',
    projectType: '',
    url: '',
    urlDev: '',
    urlAdmin: '',
    account: '',
    annualFee: '',
    money: '',
    views: '',
    content: '',
    progress: '',
    customerId: undefined,
    customerName: '',
    realMoney: '',
    qrcode: '',
    sortNumber: undefined,
    status: undefined,
    projectId: 0
  });

  // 请求状态
  const loading = ref(true);

  const activeKey = ref('1');

  const { resetFields } = useForm(project);

  const content = ref('');

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  // 富文本处理
  const showHtml = (str) => {
    return str
      .replace(str ? /&(?!#?\w+;)/g : /&/g, '&amp;')
      .replace(/&lt;/g, '<')
      .replace(/&gt;/g, '>')
      .replace(/&quot;/g, '"')
      .replace(/&#39;/g, "'")
      .replace(/&amp;nbsp;/g, '\u3000');
  };

  /* 全屏切换 */
  const width = ref('75%');
  const onFullscreen = () => {
    try {
      width.value = width.value == '100%' ? '75%' : '100%';
      toggleFullscreen();
    } catch (e) {
      message.error('您的浏览器不支持全屏模式');
    }
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.data) {
          loading.value = false;
          content.value = showHtml(props.data.content);
          assignObject(project, props.data);
        }
      } else {
        resetFields();
      }
    }
  );
</script>
<style lang="less">
  .tab-pane {
    min-height: 100px;
  }
  .content img {
    max-width: 95%;
  }
  .project-card-head {
    display: flex;
    justify-content: center;
    align-project: center;
    flex-direction: column;
    padding-top: 0px;
  }
  .project-avatar .avatar {
    width: 180px;
  }
  .progress {
    margin-top: 250px;
    display: flex;
    flex-direction: column;
    align-project: center;
  }
</style>
