<!--
 * @Author: 
 * @Date: 2023-03-01 19:20:44
 * @LastEditors: guicheng 1625811865@qq.com
 * @LastEditTime: 2024-07-10 11:45:02
 * @Description: kxs files
 * @filePath: 
-->
<script lang="ts">
// 声明额外的选项
export default {
  name: "Add"
};
</script>
<script setup lang="ts">
import { inject, onMounted, reactive, ref, Uploadfile, UploadImg, Editor, useRenderIcon, ElMessage, ElMessageBox, Upload, Close, http, getGroupUrl, RegularVerification, verification } from "@/utils/importUsed"
// 接口列表实例
const UserUrl = ref(null)
// 获取当前板块接口列表
onMounted(async () => {
  UserUrl.value = await getGroupUrl(["User"]);
})
// 组件传参对象
const props = defineProps<{
  editVisible: {
    type: Boolean;
    default: false;
  };
  width: {
    type: Number;
    default: 50;
  };
  formData: {
    id: any;
    type: any;
    default: {};
  };
}>();
// 表单数据
const UpdateForm: any = ref({
  RealName: "",
  AdminName: "",
  PassWord: "",
  RoleId: "",
  content: "",
  upLoadFile: "",
  upLoadImg: 'https://oss.kxssaas.com/KxsAdmin/UploadImg/png/2024/6/20/00c1487f0956405e.png',
});
// 传参选项数据
const optionList = [
  { Id: "1", label: "推荐" },
  { Id: "2", label: "实物" },
  { Id: "3", label: "虚拟商品" }
];
// 表单实例
const ruleFormRef = ref()
// 富文本实例
const childComp = ref();
// 选项卡参数(默认值为列表某项的id)
const activeId = ref("1");
// 提交函数
const submit = async (formEl) => {
  // 表单校验拦截
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      //表单校验成功回调
      console.log('submit!')
      // 富文本实例赋值
      UpdateForm.value.content = childComp.value.valueHtml;
      console.log(UpdateForm.value);
      // 需动态生成接口
      const { status, info }: any = http.request(
        UserUrl.User.userSearch.method,
        UserUrl.User.userSearch.url,
        UpdateForm.value
      );
      if (status === "1") {
        //业务成功回调
        ElMessage({
          message: "修改成功",
          type: "success"
        });
        // 关闭修改弹窗;
        closeVisible();
      } else {
        //业务失败回调
        ElMessageBox.alert(info, "提示", {
          confirmButtonText: "关闭",
          type: "warning"
        });
      }
    } else {
      //表单校验失败回调
      ElMessage({
        message: "请输入完整信息",
        type: "error"
      });
    }
  })
};

//上传文件回调
const UploadFileCallBack = (url) => {
  ElMessage({
    message: `文件上传成功:${url}`,
    type: "success"
  });
  UpdateForm.value.upLoadFile = url;
}
//上传图片回调
const UploadImgCallBack = (url) => {
  ElMessage({
    message: `图片上传成功:${url}`,
    type: "success"
  });
  UpdateForm.value.upLoadImg = url;
}
// 表单校验规则
const rules = reactive({
  RealName: [
    // 必传
    { required: true, message: '请输入账户名称', trigger: 'blur' },
    // 长度
    { min: 3, max: 5, message: '长度为3-5', trigger: 'blur' },
    // 时间类型必填
    {
      type: 'date',
      required: true,
      message: '请选择正确时间类型',
      trigger: 'blur',
    },
    // 封装正则
    {
      validator: (rule: any, value: any, callback: any) => {
        verification.verificationName(value, callback)
      }, trigger: 'blur'
    },
    // 自定义正则
    {
      validator: (rule: any, value: any, callback: any) => {
        // 自定义正则替换
        const regular = /^((13[0-9])|(14(0|[5-7]|9))|(15([0-3]|[5-9]))|(16(2|[5-7]))|(17[0-8])|(18[0-9])|(19([0-3]|[5-9])))\d{8}$/;
        RegularVerification(regular, value, callback)
      }, trigger: 'blur'
    },
  ],
})

// 关闭弹窗回调函数
const closeVisible: any = inject("closeEditVisible");

// 调用修改弹窗函数
const openVisible = async () => {
  //通过ID获取表格数据（二选一）
  const { data, info }: any = http.Request({
    url: UserUrl.User.userSearch.method,
    method: UserUrl.User.userSearch.url,
    params: props.formData.id
  });
  UpdateForm.value = data;

  // 通过表格项获取数据（二选一）
  UpdateForm.value = props.formData;

  // 将获取到的富文本字段数据重新赋值到富文本实例
  childComp.value.valueHtml = "<p>12312312</p>";
};

// 弹窗是否全屏
const isFullscreen = ref(false)
</script>

<template lang="pug">
.main
  el-dialog(v-model='props.editVisible' draggable width="50%" :fullscreen="isFullscreen" title="修改" @close="closeVisible" @open="openVisible")
    //- 有选项卡模板
    el-tabs(v-model="activeId" class="demo-tabs" @tab-click="handleClick")
      el-tab-pane(label="选项卡一" name="1")
        el-form(ref="ruleFormRef" :model='UpdateForm' label-position="right" label-width="100px" :rules="rules")
          el-form-item(label='账户名称' prop="RealName")
            el-input(v-model='UpdateForm.RealName' autocomplete='off' 
              placeholder="请输入账户名称")
          el-form-item(label='用户名' prop="AdminName")
            el-input(v-model='UpdateForm.AdminName' autocomplete='off' 
              placeholder="请输入用户名")
          el-form-item(label='密码' prop="PassWord")
            el-input(v-model='UpdateForm.PassWord' autocomplete='off' 
              placeholder="请输入密码")
          el-form-item(label="角色:", prop="RoleId")
            el-select(
              v-model="UpdateForm.RoleId",
              placeholder="请选择用户角色",
              clearable
            )
              el-option(:label="item.label", :value="item.Id" v-for="(item, index) in optionList")
          el-form-item(label='上传图片' prop="UploadImg")
            UploadImg(:cropper="true" :callBack="UploadImgCallBack" :imgList="UpdateForm.upLoadImg.split(',')")
          el-form-item(label='上传文件' prop="Uploadfile")
            el-input(v-model='UpdateForm.upLoadFile' autocomplete='off' class="!w-[230px]" disabled)
            Uploadfile(btntext="上传文件" :FilePath="UploadCallBack")
          el-form-item(label="富文本:", prop="RoleId")
            Editor(ref="childComp")
      el-tab-pane(label="选项卡二" name="2")
        el-form(:model='UpdateForm' label-position="right" label-width="100px")
          el-form-item(label='账户名称' prop="RealName")
            el-input(v-model='UpdateForm.RealName' autocomplete='off'
              placeholder="请输入账户名称")
          el-form-item(label='用户名' prop="AdminName")
            el-input(v-model='UpdateForm.AdminName' autocomplete='off'
              placeholder="请输入用户名")
          el-form-item(label='密码' prop="PassWord")
            el-input(v-model='UpdateForm.PassWord' autocomplete='off'
              placeholder="请输入密码")
          el-form-item(label="角色:", prop="RoleId")
            el-select(
              v-model="UpdateForm.RoleId",
              placeholder="请选择用户角色",
              clearable
            )
              el-option(:label="item.label", :value="item.Id" v-for="(item, index) in optionList")
    //- 无选项卡模板
    el-form(:model='UpdateForm' label-position="right" label-width="100px")
      el-form-item(label='账户名称' prop="RealName")
        el-input(v-model='UpdateForm.RealName' autocomplete='off' placeholder="请输入账户名称")
      el-form-item(label='用户名' prop="AdminName")
        el-input(v-model='UpdateForm.AdminName' autocomplete='off' placeholder="请输入用户名")
      el-form-item(label='密码' prop="PassWord")
        el-input(v-model='UpdateForm.PassWord' autocomplete='off' placeholder="请输入密码")
      el-form-item(label="角色:", prop="RoleId")
        el-select(
          v-model="UpdateForm.RoleId",
          placeholder="请选择用户角色",
          clearable
        )
          el-option(:label="item.label", :value="item.Id" v-for="(item, index) in optionList")
    .flex.justify-end
      el-button(
        :icon="useRenderIcon(Close)",
        @click="closeVisible"
      ) 关闭
      el-button(
        type="primary",
        :icon="useRenderIcon(Upload)",
        @click="submit(ruleFormRef)"
      ) 确认提交
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-form-item__label) {
  font-weight: 700;
}

:deep(.el-pagination) {
  flex-flow: wrap;
}

:deep(.is-draggable) {
  max-height: 80vh;
  overflow: auto;
}

:deep(.el-dialog__header) {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #fff;
}

.collapsedom {
  padding: 0 20px;
  background-color: #fff;
}

.ovh-x {
  height: 40vh;
  overflow-y: auto;
}

:deep(.el-descriptions__header) {
  margin: 16px 0 !important;
}

.el-select {
  width: 100%;
}
</style>
