<template>

  <Dialog :title="dialogTitle" v-model="dialogVisible" width="70%">

    <ContentWrap v-loading="formLoading" style="max-height:400px;min-height:400px;overflow-y: auto;border:none ;">
      <div class="left">
        <!-- <SPuUploadImg v-model="avatar" :disabled="isDetail" /> -->
        <el-tabs v-model="activeName" tab-position="left"  class="child-tabs">
          <el-tab-pane label="基本信息" name="info" />
          <el-tab-pane label="收款信息" name="Payment" v-if="openType != 'create'" />
        </el-tabs>
      </div>
      <div class="right">
        <MerchantStoreFrom :obj="obj" ref="MerchantStoreFromRef"  :isDetail="isDetail" :openType="openType" v-if="activeName == 'info'" />
        <MerchantStorePaymentFrom :obj="obj" ref="MerchantStorePaymentFromRef" :isDetail="isDetail"  v-if="activeName == 'Payment'" />
      </div>

      <div style="clear: both;"></div>
    </ContentWrap>
    <!-- 基本信息的保存 --><!-- 分配人员的保存 -->
    <template #footer v-if="!isDetail">
      <div class="dialog-footer">
        <el-button v-if="!isDetail && checkPermi(['sale:shop:delete']) && openType != 'create'" :loading="formLoading" type="danger" @click="deleteStore">删除</el-button>
        <el-button v-if="!isDetail && activeName == 'info'" :loading="formLoading" type="primary" @click="MerchantStoreFromSubmit" v-hasPermi="['sale:shop:update'] || ['sale:shop:create']">保存</el-button>
        
        <el-button v-if="!isDetail && activeName == 'Payment' && checkPermi(['sale:shop:update'])" :loading="formLoading" type="primary" @click="MerchantStorePaymentForm" >保存</el-button>
      </div>
    </template>
  </Dialog>
  <!-- </div> -->

</template>

<script lang="ts" setup>
import { ShopApi } from '@/api/system/sale/shop/index'
import Dialog from '@/components/Dialog/src/Dialog.vue'
import MerchantStoreFrom from './MerchantStoreFrom.vue';
import MerchantStorePaymentFrom from './MerchantStorePaymentFrom.vue';
import { CommonStatusEnum } from '@/utils/constants'
import * as UserApi from '@/api/system/user'
import { checkPermi } from '@/utils/permission'


const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const activeName = ref('info') // Tag 激活的窗口
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const isDetail = ref(false)
const openType = ref('create')
const obj = ref({})

const avatar = ref('avatar') //头像
/** 打开弹窗 */
const open = async (type: string, obj1: object) => {
  activeName.value = 'info'
  obj.value = obj1
  openType.value = type
  dialogTitle.value = t('action.' + type)
  if (type == "view") {
    dialogTitle.value = "查看"
  }
  // 判断打开状态，如果是view那就只能查看 否则可以编辑
  if ('view' == type) {
    isDetail.value = true
  } else {
    isDetail.value = false
  }
  dialogVisible.value = true
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const MerchantStoreFromRef = ref() // 表单 Ref
const MerchantStoreFromSubmit = async () => {
  if (await MerchantStoreFromRef.value?.submitForm()){
    emit('success')
    dialogVisible.value = false
  }
}
const MerchantStorePaymentFromRef = ref() // 表单 Ref
const MerchantStorePaymentForm = async () => {
  if (await MerchantStorePaymentFromRef.value?.submitForm()){
    emit('success')
    dialogVisible.value = false
  }
}

/** 删除按钮操作 */
const deleteStore = async() => {
  await message.confirm('是否确认删除'+obj.value.name+'？')
  try{
    await ShopApi.deleteShop(obj.value.id as number)
  }catch{
    formLoading.value = false
  }
  dialogVisible.value = false
  message.success('删除成功')
  emit('success')
}
</script>

<style lang="scss" >
.el-dialog__body{
    padding: 0 ;
  }
.left {
  width: 106px;
  float: left;
}

.child-tabs {
  border-top: 2px solid #e4e7ef;
  margin-top: -7px;
}
.child-tabs .el-tabs__item {
  width: 106px;
  justify-content: center;
}
.right {
  // padding: 10px 0 60px 10px;
  border-left: 2px solid #e4e7ee;
  margin-left: -2px;
  float: left;
  width: calc(100% - 120px);
}
</style>