<!-- 编辑弹窗 -->
<template>
  <ele-modal
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :maxable="maxable"
    :title="isUpdate ? '编辑应用' : '添加应用'"
    :body-style="{ paddingBottom: '28px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="styleResponsive ? { md: 4, sm: 5, xs: 24 } : { flex: '90px' }"
      :wrapper-col="
        styleResponsive ? { md: 19, sm: 19, xs: 24 } : { flex: '1' }
      "
    >
      <a-form-item label="应用名称" name="appName">
        <a-input
          allow-clear
          placeholder="请输入应用名称"
          v-model:value="form.appName"
        />
      </a-form-item>
      <a-form-item label="应用标识" name="appCode">
        <a-input
          allow-clear
          placeholder="请输入应用标识"
          v-model:value="form.appCode"
        />
      </a-form-item>
      <a-form-item label="应用秘钥" name="appSecret">
        <a-input
          allow-clear
          placeholder="请输入应用秘钥"
          v-model:value="form.appSecret"
        />
      </a-form-item>
      <a-form-item label="上级id, 0是顶级" name="parentId">
        <a-input
          allow-clear
          placeholder="请输入上级id, 0是顶级"
          v-model:value="form.parentId"
        />
      </a-form-item>
      <a-form-item label="应用类型" name="appType">
        <a-input
          allow-clear
          placeholder="请输入应用类型"
          v-model:value="form.appType"
        />
      </a-form-item>
      <a-form-item label="应用类型" name="appTypeMultiple">
        <a-input
          allow-clear
          placeholder="请输入应用类型"
          v-model:value="form.appTypeMultiple"
        />
      </a-form-item>
      <a-form-item label="类型, 0菜单, 1按钮" name="menuType">
        <a-input
          allow-clear
          placeholder="请输入类型, 0菜单, 1按钮"
          v-model:value="form.menuType"
        />
      </a-form-item>
      <a-form-item label="企业ID" name="companyId">
        <a-input
          allow-clear
          placeholder="请输入企业ID"
          v-model:value="form.companyId"
        />
      </a-form-item>
      <a-form-item label="企业名称" name="companyName">
        <a-input
          allow-clear
          placeholder="请输入企业名称"
          v-model:value="form.companyName"
        />
      </a-form-item>
      <a-form-item label="应用图标" name="appIcon">
        <a-input
          allow-clear
          placeholder="请输入应用图标"
          v-model:value="form.appIcon"
        />
      </a-form-item>
      <a-form-item label="二维码" name="appQrcode">
        <a-input
          allow-clear
          placeholder="请输入二维码"
          v-model:value="form.appQrcode"
        />
      </a-form-item>
      <a-form-item label="链接地址" name="appUrl">
        <a-input
          allow-clear
          placeholder="请输入链接地址"
          v-model:value="form.appUrl"
        />
      </a-form-item>
      <a-form-item label="后台管理地址" name="adminUrl">
        <a-input
          allow-clear
          placeholder="请输入后台管理地址"
          v-model:value="form.adminUrl"
        />
      </a-form-item>
      <a-form-item label="下载地址" name="downUrl">
        <a-input
          allow-clear
          placeholder="请输入下载地址"
          v-model:value="form.downUrl"
        />
      </a-form-item>
      <a-form-item label="链接地址" name="serverUrl">
        <a-input
          allow-clear
          placeholder="请输入链接地址"
          v-model:value="form.serverUrl"
        />
      </a-form-item>
      <a-form-item label="文件服务器" name="fileUrl">
        <a-input
          allow-clear
          placeholder="请输入文件服务器"
          v-model:value="form.fileUrl"
        />
      </a-form-item>
      <a-form-item label="回调地址" name="callbackUrl">
        <a-input
          allow-clear
          placeholder="请输入回调地址"
          v-model:value="form.callbackUrl"
        />
      </a-form-item>
      <a-form-item label="腾讯文档地址" name="docsUrl">
        <a-input
          allow-clear
          placeholder="请输入腾讯文档地址"
          v-model:value="form.docsUrl"
        />
      </a-form-item>
      <a-form-item label="代码仓库地址" name="gitUrl">
        <a-input
          allow-clear
          placeholder="请输入代码仓库地址"
          v-model:value="form.gitUrl"
        />
      </a-form-item>
      <a-form-item label="原型图地址" name="prototypeUrl">
        <a-input
          allow-clear
          placeholder="请输入原型图地址"
          v-model:value="form.prototypeUrl"
        />
      </a-form-item>
      <a-form-item label="IP白名单" name="ipAddress">
        <a-input
          allow-clear
          placeholder="请输入IP白名单"
          v-model:value="form.ipAddress"
        />
      </a-form-item>
      <a-form-item label="应用截图" name="images">
        <a-input
          allow-clear
          placeholder="请输入应用截图"
          v-model:value="form.images"
        />
      </a-form-item>
      <a-form-item label="应用包名" name="packageName">
        <a-input
          allow-clear
          placeholder="请输入应用包名"
          v-model:value="form.packageName"
        />
      </a-form-item>
      <a-form-item label="下载次数" name="clicks">
        <a-input
          allow-clear
          placeholder="请输入下载次数"
          v-model:value="form.clicks"
        />
      </a-form-item>
      <a-form-item label="安装次数" name="installs">
        <a-input
          allow-clear
          placeholder="请输入安装次数"
          v-model:value="form.installs"
        />
      </a-form-item>
      <a-form-item label="备注" name="comments">
        <a-textarea
          :rows="4"
          :maxlength="200"
          placeholder="请输入描述"
          v-model:value="form.comments"
        />
      </a-form-item>
      <a-form-item label="应用介绍" name="content">
        <a-input
          allow-clear
          placeholder="请输入应用介绍"
          v-model:value="form.content"
        />
      </a-form-item>
      <a-form-item label="项目需求" name="requirement">
        <a-input
          allow-clear
          placeholder="请输入项目需求"
          v-model:value="form.requirement"
        />
      </a-form-item>
      <a-form-item label="开发者(个人或公司)" name="developer">
        <a-input
          allow-clear
          placeholder="请输入开发者(个人或公司)"
          v-model:value="form.developer"
        />
      </a-form-item>
      <a-form-item label="项目负责人" name="director">
        <a-input
          allow-clear
          placeholder="请输入项目负责人"
          v-model:value="form.director"
        />
      </a-form-item>
      <a-form-item label="项目经理" name="projectDirector">
        <a-input
          allow-clear
          placeholder="请输入项目经理"
          v-model:value="form.projectDirector"
        />
      </a-form-item>
      <a-form-item label="业务员" name="salesman">
        <a-input
          allow-clear
          placeholder="请输入业务员"
          v-model:value="form.salesman"
        />
      </a-form-item>
      <a-form-item label="软件定价" name="price">
        <a-input
          allow-clear
          placeholder="请输入软件定价"
          v-model:value="form.price"
        />
      </a-form-item>
      <a-form-item label="划线价格" name="linePrice">
        <a-input
          allow-clear
          placeholder="请输入划线价格"
          v-model:value="form.linePrice"
        />
      </a-form-item>
      <a-form-item label="评分" name="score">
        <a-input
          allow-clear
          placeholder="请输入评分"
          v-model:value="form.score"
        />
      </a-form-item>
      <a-form-item label="星级" name="star">
        <a-input
          allow-clear
          placeholder="请输入星级"
          v-model:value="form.star"
        />
      </a-form-item>
      <a-form-item label="菜单路由地址" name="path">
        <a-input
          allow-clear
          placeholder="请输入菜单路由地址"
          v-model:value="form.path"
        />
      </a-form-item>
      <a-form-item label="菜单组件地址, 目录可为空" name="component">
        <a-input
          allow-clear
          placeholder="请输入菜单组件地址, 目录可为空"
          v-model:value="form.component"
        />
      </a-form-item>
      <a-form-item label="权限标识" name="authority">
        <a-input
          allow-clear
          placeholder="请输入权限标识"
          v-model:value="form.authority"
        />
      </a-form-item>
      <a-form-item label="打开位置" name="target">
        <a-input
          allow-clear
          placeholder="请输入打开位置"
          v-model:value="form.target"
        />
      </a-form-item>
      <a-form-item label="是否隐藏, 0否, 1是(仅注册路由不显示在左侧菜单)" name="hide">
        <a-input
          allow-clear
          placeholder="请输入是否隐藏, 0否, 1是(仅注册路由不显示在左侧菜单)"
          v-model:value="form.hide"
        />
      </a-form-item>
      <a-form-item label="禁止搜索，1禁止 0 允许" name="search">
        <a-input
          allow-clear
          placeholder="请输入禁止搜索，1禁止 0 允许"
          v-model:value="form.search"
        />
      </a-form-item>
      <a-form-item label="菜单侧栏选中的path" name="active">
        <a-input
          allow-clear
          placeholder="请输入菜单侧栏选中的path"
          v-model:value="form.active"
        />
      </a-form-item>
      <a-form-item label="其它路由元信息" name="meta">
        <a-input
          allow-clear
          placeholder="请输入其它路由元信息"
          v-model:value="form.meta"
        />
      </a-form-item>
      <a-form-item label="版本，0正式版 1基础版 2开发版" name="edition">
        <a-input
          allow-clear
          placeholder="请输入版本，0正式版 1基础版 2开发版"
          v-model:value="form.edition"
        />
      </a-form-item>
      <a-form-item label="版本号" name="version">
        <a-input
          allow-clear
          placeholder="请输入版本号"
          v-model:value="form.version"
        />
      </a-form-item>
      <a-form-item label="是否已安装" name="isUse">
        <a-input
          allow-clear
          placeholder="请输入是否已安装"
          v-model:value="form.isUse"
        />
      </a-form-item>
      <a-form-item label="附近1" name="file1">
        <a-input
          allow-clear
          placeholder="请输入附近1"
          v-model:value="form.file1"
        />
      </a-form-item>
      <a-form-item label="附件2" name="file2">
        <a-input
          allow-clear
          placeholder="请输入附件2"
          v-model:value="form.file2"
        />
      </a-form-item>
      <a-form-item label="附件3" name="file3">
        <a-input
          allow-clear
          placeholder="请输入附件3"
          v-model:value="form.file3"
        />
      </a-form-item>
      <a-form-item label="是否显示续费提醒" name="showExpiration">
        <a-input
          allow-clear
          placeholder="请输入是否显示续费提醒"
          v-model:value="form.showExpiration"
        />
      </a-form-item>
      <a-form-item label="是否作为案例展示" name="showCase">
        <a-input
          allow-clear
          placeholder="请输入是否作为案例展示"
          v-model:value="form.showCase"
        />
      </a-form-item>
      <a-form-item label="是否显示在首页" name="showIndex">
        <a-input
          allow-clear
          placeholder="请输入是否显示在首页"
          v-model:value="form.showIndex"
        />
      </a-form-item>
      <a-form-item label="是否推荐" name="recommend">
        <a-input
          allow-clear
          placeholder="请输入是否推荐"
          v-model:value="form.recommend"
        />
      </a-form-item>
      <a-form-item label="到期时间" name="expirationTime">
        <a-input
          allow-clear
          placeholder="请输入到期时间"
          v-model:value="form.expirationTime"
        />
      </a-form-item>
      <a-form-item label="续费金额" name="renewMoney">
        <a-input
          allow-clear
          placeholder="请输入续费金额"
          v-model:value="form.renewMoney"
        />
      </a-form-item>
      <a-form-item label="应用状态" name="appStatus">
        <a-input
          allow-clear
          placeholder="请输入应用状态"
          v-model:value="form.appStatus"
        />
      </a-form-item>
      <a-form-item label="排序(数字越小越靠前)" name="sortNumber">
        <a-input-number
          :min="0"
          :max="9999"
          class="ele-fluid"
          placeholder="请输入排序号"
          v-model:value="form.sortNumber"
        />
      </a-form-item>
      <a-form-item label="状态, 0正常, 1冻结" name="status">
        <a-radio-group v-model:value="form.status">
          <a-radio :value="0">显示</a-radio>
          <a-radio :value="1">隐藏</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="是否删除, 0否, 1是" name="deleted">
        <a-input
          allow-clear
          placeholder="请输入是否删除, 0否, 1是"
          v-model:value="form.deleted"
        />
      </a-form-item>
      <a-form-item label="用户ID" name="userId">
        <a-input
          allow-clear
          placeholder="请输入用户ID"
          v-model:value="form.userId"
        />
      </a-form-item>
      <a-form-item label="机构id" name="organizationId">
        <a-input
          allow-clear
          placeholder="请输入机构id"
          v-model:value="form.organizationId"
        />
      </a-form-item>
      <a-form-item label="租户编号" name="tenantCode">
        <a-input
          allow-clear
          placeholder="请输入租户编号"
          v-model:value="form.tenantCode"
        />
      </a-form-item>
      <a-form-item label="修改时间" name="updateTime">
        <a-input
          allow-clear
          placeholder="请输入修改时间"
          v-model:value="form.updateTime"
        />
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject, uuid } from 'ele-admin-pro';
  import { addOaApp, updateOaApp } from '@/api/oa/oaApp';
  import { OaApp } from '@/api/oa/oaApp/model';
  import { useThemeStore } from '@/store/modules/theme';
  import { storeToRefs } from 'pinia';
  import { ItemType } from 'ele-admin-pro/es/ele-image-upload/types';
  import { FormInstance } from 'ant-design-vue/es/form';
  import { FileRecord } from '@/api/system/file/model';

  // 是否是修改
  const isUpdate = ref(false);
  const useForm = Form.useForm;
  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);

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

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

  // 提交状态
  const loading = ref(false);
  // 是否显示最大化切换按钮
  const maxable = ref(true);
  // 表格选中数据
  const formRef = ref<FormInstance | null>(null);
  const images = ref<ItemType[]>([]);

  // 用户信息
  const form = reactive<OaApp>({
    appId: undefined,
    appName: undefined,
    appCode: undefined,
    appSecret: undefined,
    parentId: undefined,
    appType: undefined,
    appTypeMultiple: undefined,
    menuType: undefined,
    companyId: undefined,
    companyName: undefined,
    appIcon: undefined,
    appQrcode: undefined,
    appUrl: undefined,
    adminUrl: undefined,
    downUrl: undefined,
    serverUrl: undefined,
    fileUrl: undefined,
    callbackUrl: undefined,
    docsUrl: undefined,
    gitUrl: undefined,
    prototypeUrl: undefined,
    ipAddress: undefined,
    images: undefined,
    packageName: undefined,
    clicks: undefined,
    installs: undefined,
    comments: undefined,
    content: undefined,
    requirement: undefined,
    developer: undefined,
    director: undefined,
    projectDirector: undefined,
    salesman: undefined,
    price: undefined,
    linePrice: undefined,
    score: undefined,
    star: undefined,
    path: undefined,
    component: undefined,
    authority: undefined,
    target: undefined,
    hide: undefined,
    search: undefined,
    active: undefined,
    meta: undefined,
    edition: undefined,
    version: undefined,
    isUse: undefined,
    file1: undefined,
    file2: undefined,
    file3: undefined,
    showExpiration: undefined,
    showCase: undefined,
    showIndex: undefined,
    recommend: undefined,
    expirationTime: undefined,
    renewMoney: undefined,
    appStatus: undefined,
    sortNumber: undefined,
    status: undefined,
    deleted: undefined,
    userId: undefined,
    organizationId: undefined,
    tenantCode: undefined,
    tenantId: undefined,
    createTime: undefined,
    updateTime: undefined,
    oaAppId: undefined,
    oaAppName: '',
    status: 0,
    comments: '',
    sortNumber: 100
  });

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

  // 表单验证规则
  const rules = reactive({
    oaAppName: [
      {
        required: true,
        type: 'string',
        message: '请填写应用名称',
        trigger: 'blur'
      }
    ]
  });

  const chooseImage = (data: FileRecord) => {
    images.value.push({
      uid: data.id,
      url: data.path,
      status: 'done'
    });
    form.image = data.path;
  };

  const onDeleteItem = (index: number) => {
    images.value.splice(index, 1);
    form.image = '';
  };

  const { resetFields } = useForm(form, rules);

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const formData = {
          ...form
        };
        const saveOrUpdate = isUpdate.value ? updateOaApp : addOaApp;
        saveOrUpdate(formData)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        images.value = [];
        if (props.data) {
          assignObject(form, props.data);
          if(props.data.image){
            images.value.push({
              uid: uuid(),
              url: props.data.image,
              status: 'done'
            })
          }
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    },
    { immediate: true }
  );
</script>
