<script setup>
import {apiSystemCommon,apiSystemSet} from '@/api/system'

const chartWaterImgs=ref([])
async function handleUploadImg(e){
  const file=e.raw
  const res=await apiSystemCommon.uploadImg({
    file:file
  })
  const resObj={
    status:'',
    error:'',
    response:{
      url:''
    }
  }
  if(res.Ret!==200){
    resObj.status='fail'
    resObj.error='上传失败'
  }else{
    resObj.status='success'
    resObj.response.url=res.Data.ResourceUrl
    chartWaterImgs.value=[{url:res.Data.ResourceUrl}]
  }

  return resObj
}

// 获取配置
async function getConfigData(){
  const res=await apiSystemSet.getConfigData()
  if(res.Ret!==200) return
  if(res.Data.CompanyWatermark){
    chartWaterImgs.value=[{url:res.Data.CompanyWatermark}]
  }
}
getConfigData()

// 保存配置
async function handleSaveConfig(){
  const res=await apiSystemSet.updateConfigData({
    CompanyWatermark:chartWaterImgs.value[0]?.url||''
  })
  if(res.Ret!==200) return
  MessagePlugin.success('保存成功')
}

</script>

<template>
  <div class="bg-white common-set-page">
    <h3>设置图表水印</h3>
    <t-upload
      v-model="chartWaterImgs"
      theme="image"
      tips="上传格式:png、jpg"
      accept="image/png,image/jpeg"
      :showImageFileName="false"
      :request-method="handleUploadImg"
      :locale="{
        triggerUploadText: {
          image: '点击上传图片',
        },
      }"
    />
    <t-button style="margin-top:30px" @click="handleSaveConfig">应用到全部社区图表</t-button>
  </div>
</template>

<style lang="scss" scoped>
.common-set-page{
  padding: 20px;
  border-radius: 4px;
  height: calc(100vh - 160px);
}
</style>