<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="mb-[10px]" v-show="showSearch">
        <el-card shadow="hover">
          <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
            <el-form-item label="租户名称" prop="companyName">
              <el-input v-model="queryParams.companyName" placeholder="请输入租户名称" clearable style="width: 240px"
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="合作组织" prop="organizationName">
              <el-input v-model="queryParams.organizationName" placeholder="请输入组织名称" clearable style="width: 240px"
                @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="合作组织" prop="tenantOrganization">
              <el-select v-model="queryParams.tenantOrganization" placeholder="请选择合作组织" clearable style="width: 240px">
                <el-option v-for="item in organizationList" :key="item.id" :label="item.organizationName"
                  :value="item.id" />
              </el-select>
            </el-form-item> -->
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <el-table v-loading="loading" :data="listData" empty-text="未找到搜索内容，请重新尝试" :cellStyle="cellStyle">
        <el-table-column label="租户编号" align="center" prop="tenantId" />
        <el-table-column label="租户名称" align="center" prop="companyName" />
        <el-table-column label="合作组织" align="center" prop="organizationName" />
        <el-table-column label="主题色调" align="center" prop="themeColor">
          <template #default="scope">
            <div :style="'width: 80px;height: 20px;margin: auto;background:' + scope.row.themeColor + ';'"></div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            {{ scope.row.status === '0' ? '正常' : '停用' }}
          </template>
        </el-table-column>

        <el-table-column width="300" label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="主题色管理" placement="top">
              <el-button link type="primary" style="font-size: 12px;" v-hasPermi="['system:themeColorManage:edit']"
                @click="handleUpdate(scope.row, scope.$index)">主题色管理</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改租户对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="70%" append-to-body>
      <div class="edit-dialog">
        <el-form class="edit-dialog-left" label-width="90px">
          <el-form-item label="租户名称">
            <el-input v-model="listData[viewUrlIndex].companyName" disabled />
          </el-form-item>
          <el-form-item label="合作组织">
            <el-input v-model="listData[viewUrlIndex].organizationName" disabled />
          </el-form-item>
          <el-form-item label="组织logo">
            <el-image :src="listData[viewUrlIndex].organizationLogo[0].url" style="width: 100px;height: 100px;" />
          </el-form-item>
          <el-form-item label="主题色配置"></el-form-item>
          <div class="label">主题色推荐</div>
          <div class="color-block-content">
            <div class="color-block" :class="{ active: chooseIndex == 1 }">
              <div style="background: #3A93FA;" @click="setThemeColor('#3A93FA', 1)"></div>
            </div>
            <div class="color-block" :class="{ active: chooseIndex == 2 }">
              <div style="background: #26B7B9;" @click="setThemeColor('#26B7B9', 2)"></div>
            </div>
            <div class="color-block" :class="{ active: chooseIndex == 3 }">
              <div style="background: #1FBE89;" @click="setThemeColor('#1FBE89', 3)"></div>
            </div>
            <div class="color-block" :class="{ active: chooseIndex == 4 }">
              <div style="background: #F76560;" @click="setThemeColor('#F76560', 4)"></div>
            </div>
          </div>
          <div class="color-block-content">
            <div class="color-block" :class="{ active: chooseIndex == 5 }">
              <div style="background: #3491FA;" @click="setThemeColor('#3491FA', 5)"></div>
            </div>
            <div class="color-block" :class="{ active: chooseIndex == 6 }">
              <div style="background: #FB7299;" @click="setThemeColor('#FB7299', 6)"></div>
            </div>
            <div class="color-block" :class="{ active: chooseIndex == 7 }">
              <div style="background: #FF7D00;" @click="setThemeColor('#FF7D00', 7)"></div>
            </div>
            <div class="color-block" :class="{ active: chooseIndex == 8 }">
              <div style="background: #FF0000;" @click="setThemeColor('#FF0000', 8)"></div>
            </div>
          </div>
          <div>
            <div class="label" style="margin-top: 20px;">自定义颜色</div>
            <colorPick v-model="colorPickStr" style="margin-top: 20px;margin-left: 20px;" @update:isUpdate="handleUpdateColor"></colorPick>
          </div>
        </el-form>
        <div class="edit-dialog-right">
          <div class="edit-dialog-right-title">
            <div>用户端效果预览</div>
            <div @click="(viewUrldialog.visible = true)">完整效果</div>
          </div>
          <div class="normal-login-container">
            <!-- 标题 -->
            <div class="login-title-content">
              <div class="title">欢迎来到病历夹</div>
            </div>
            <!-- 标题 -->

            <!-- 登录按钮 -->
            <div class="login-btn-content">
              <div class="login-btn">登录</div>
              <div class="xieyi">
                <el-checkbox disabled />
                <div style="margin-left: 5px;">已阅读并同意</div>
                <div class="text-blue" style="margin-left: 2px;">《用户协议》</div>
                <div class="text-blue" style="margin-left: 2px;">《隐私协议》</div>
              </div>
            </div>
            <!-- 登录按钮 -->
            <!-- 登录信息 -->
            <div class="login-form-content">
              <div class="input-item">
                <img class="icon" src="@/assets/images/phone.png" />
                <div class="input">请输入手机号</div>
              </div>
              <div class="input-item">
                <img class="icon" src="@/assets/images/message.png" />
                <div class="input">请输入验证码</div>
                <div class="msgCode text-center">
                  获取验证码
                </div>
              </div>
              <div style="color: #aaaaaa;text-align: center;">收不到验证码</div>

            </div>
            <!-- 登录信息 -->
          </div>
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 添加或修改租户对话框 -->

    <!-- 查看地址 -->
    <el-dialog :title="viewUrldialog.title" v-model="viewUrldialog.visible" width="800px" append-to-body>
      <div class="view_url">
        <el-row :gutter="20">
          <el-col :span="6" class="text_left">租户名称：</el-col>
          <el-col :span="18" style="margin-top: 15px;">{{ listData[viewUrlIndex].companyName }}</el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6" class="text_left">用户端地址：</el-col>
          <el-col :span="18" class="text_right" v-show="listData[viewUrlIndex].appAccessPath">
            <div>{{ listData[viewUrlIndex].appAccessPath || '' }}</div>
            <el-icon class="text_right_icon" @click="openUrl(listData[viewUrlIndex].appAccessPath)">
              <Connection />
            </el-icon>
          </el-col>
        </el-row>
        <div v-if="listData[viewUrlIndex].appAccessPath">
          <div class="text_left">请打开微信扫一扫下方二维码查看手机端效果</div>
          <QrcodeVue :value="listData[viewUrlIndex].appAccessPath" style="margin-top: 20px;"></QrcodeVue>
        </div>

      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="hideViewUrlDialog">确 定</el-button>
          <el-button @click="hideViewUrlDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 查看地址 -->
  </div>
</template>

<script setup name="ThemeColorManage" lang="ts">
import { copyText } from 'vue3-clipboard'
import { listOrganization } from '@/api/system/tenant';
import { listThemeColorManage, updateThemeColorManage } from '@/api/system/themeColorManage';
import { themeColorManageQuery, themeColorManageVO } from '@/api/system/themeColorManage/types';
import { TenantOrganizationVO } from '@/api/business/tenantOrganization/types';
import { ElImage } from 'element-plus';
import colorPick from '@/components/colorPick/index.vue'
import QrcodeVue from 'qrcode.vue'

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const listData = ref<themeColorManageVO[]>([]);

const organizationList = ref<TenantOrganizationVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const viewUrlIndex = ref(-1)

const queryFormRef = ref<ElFormInstance>();
const colorPickStr = ref('')
const chooseIndex = ref(-1);

watch(colorPickStr, (value) => {
  if (value) {
    document.documentElement.style.setProperty('--phone-theme-color', colorPickStr.value);
  }
}, { immediate: true })
//添加或修改租户对话框
const dialog = reactive<DialogOption>({
  visible: false,
  title: '主题色设置'
});

const viewUrldialog = reactive<DialogOption>({
  visible: false,
  title: '完整效果查看'
});


const data = reactive<PageData<any, themeColorManageQuery>>({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    tenantId: '',
    companyName: '',
    organizationName: ''
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

//批量导入-用户状态 文字颜色
const cellStyle = ({ row, column }) => {
  let controlStatusStyle
  switch (row.status) {
    case '0':
      controlStatusStyle = { 'color': '#67c23a' }
      break;
    case '1':
      controlStatusStyle = { 'color': '#f00' }
      break;
    default:
      controlStatusStyle = { 'color': '#000' }
      break;
  }
  if (column.label == '状态') {
    return controlStatusStyle
  }
}

/** 获取合作组织下拉列表 */
const getOrganizationList = async () => {
  const res = await listOrganization();
  organizationList.value = res.data
}

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listThemeColorManage(queryParams.value);
  listData.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

//打开地址
const openUrl = (url: string) => {
  if (url) {
    copyText(url, undefined, (error: any) => {
      if (error) {
        ElMessage.error('复制失败！');
      } else {
        ElMessage.success('复制成功！');
      }
    });
  }
}

const hideViewUrlDialog = () => {
  viewUrldialog.visible = false
}

// 取消按钮
const cancel = () => {
  dialog.visible = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 修改按钮操作 */
const handleUpdate = async (row: themeColorManageVO, index: number) => {
  viewUrlIndex.value = index
  dialog.visible = true;
  colorPickStr.value = row.themeColor || '#165DFF'
}

/** 提交按钮 */
const submitForm = async () => {
  buttonLoading.value = true;
  let itemData = listData.value[viewUrlIndex.value]
  if (itemData.id) {
    const param = { id: itemData.id, themeColor: colorPickStr.value }
    await updateThemeColorManage(param).finally(() => buttonLoading.value = false);
  }
  proxy?.$modal.msgSuccess("操作成功");
  dialog.visible = false;
  await getList();
}

const setThemeColor = (color: string, index:number) => {
  colorPickStr.value = color
  chooseIndex.value = index
}

const handleUpdateColor = () =>{
  chooseIndex.value = -1
}

onMounted(() => {
  getOrganizationList();
  getList();
})
</script>
<style lang="scss" scoped>
.edit-dialog {
  display: flex;

  &-left {
    flex: 2;
    padding: 15px 60px;
    box-sizing: border-box;
  }

  &-right {
    flex: 3;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: center;

    &-title {
      display: flex;
      align-items: center;

      div:nth-child(1) {
        font-size: 14px;
        color: black;
        font-weight: 600;
      }

      div:nth-child(2) {
        font-size: 12px;
        color: blue;
        font-weight: 600;
        text-decoration: underline;
        margin-left: 50px;
        cursor: pointer;
      }
    }

    .normal-login-container {
      display: flex;
      flex-direction: column;
      align-items: center;
      width: 315px;
      height: 560px;
      box-sizing: border-box;
      margin-top: 20px;
      border: 2px gray solid;
      border-radius: 20px;
      position: relative;


      .login-title-content {
        width: 100%;
        height: 48%;
        position: relative;
        z-index: 0;
        overflow: hidden;

        .title {
          width: 100%;
          position: absolute;
          top: 35%;
          color: white;
          font-weight: 500;
          font-size: 24px;
          font-style: normal;
          z-index: 1;
          text-align: center;
        }
      }

      .login-title-content::after {
        content: '';
        display: block;
        width: 200%;
        height: 200%;
        border-radius: 50%;
        position: absolute;
        left: 50%;
        top: -100%;
        transform: translateX(-50%);
        background: linear-gradient(148deg, white 5.53%, var(--phone-theme-color) 84.5%);
      }

      .login-form-content {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        width: 250px;
        height: 120px;
        padding: 16px 10px 10px;
        box-sizing: border-box;
        border-radius: 8px;
        background: #FFF;
        box-shadow: 0px 4px 30px 0px rgba(0, 0, 0, 0.08);
        position: absolute;
        top: 35%;
        z-index: 1;
        font-size: 12px;


        .input-item {
          display: flex;
          align-items: center;
          width: 100%;
          height: 30px;

          .icon {
            width: 14px;
            height: 14px;
          }

          .input {
            flex: 1;
            font-size: 12px;
            height: 30px;
            line-height: 30px;
            text-align: left;
            margin-left: 5px;
            color: #aaaaaa83;
          }

          .msgCode {
            width: 100px;
            height: 30px;
            line-height: 30px;
            font-size: 12px;
            color: var(--phone-theme-color);
          }

        }
      }

      .lable {
        width: 90px;
        text-align: end;
        padding-right: 12px;
      }


      .login-btn-content {
        width: 100%;
        padding: 0 30px;
        margin-top: 120px;
        text-align: center;
        box-sizing: border-box;

        .login-btn {
          height: 30px;
          background: var(--phone-theme-color);
          border-radius: 4px;
          color: white;
          font-size: 12px;
          line-height: 30px;
        }

        .xieyi {
          display: flex;
          justify-content: center;
          align-items: center;
          margin-top: 15px;
          font-size: 12px;
          color: #86909C;
        }
      }
    }
  }

}

.color-block-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 220px;
  margin-top: 20px;
  margin-left: 20px;

  .color-block {
    width: 40px;
    height: 40px;
    border-radius: 40px;
    padding: 2px;


    div {
      width: 100%;
      height: 100%;
      border-radius: 38px;
    }
  }

  .color-block.active {
    border: 1px solid #4862B0;
  }
}

.el-input.is-disabled {
  background: #dbdada;
  border: 1px solid #868686;
}

.view_url {
  box-sizing: border-box;
  padding: 15px 30px;

  .text_left {
    font-size: 16px;
    font-weight: 600;
    color: black;
    margin-top: 15px;
  }

  .text_right {
    margin-top: 15px;
    color: blue;
    text-decoration: underline;
    display: flex;

    .text_right_icon {
      margin-left: 10px;
    }
  }
}

:deep(.el-color-picker__trigger) {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
  height: 40px;
  width: 40px;
  padding: 0px;
  // border: 1px solid var(--el-border-color);
  border: none;
  border-radius: 40px;
  font-size: 0;
  position: relative;
  cursor: pointer;
}

:deep(.el-color-picker__color) {
  position: relative;
  display: block;
  box-sizing: border-box;
  border: 1px solid var(--el-text-color-secondary);
  border-radius: 40px;
  width: 100%;
  height: 100%;
  text-align: center;
}

:deep(.el-color-picker__color-inner) {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  border-radius: 40px;
}
</style>
