<template>
  <div class="wrap">
    <el-radio-group fill="#022950" v-model="radio1">
      <el-radio-button :label="1" size="large" @click="reset" border>翻译文本</el-radio-button>
      <el-radio-button :label="2" size="large" @click="reset" border>翻译文件</el-radio-button>
      <el-radio-button :label="3" size="large" @click="reset" border>优化翻译</el-radio-button>
      <el-radio-button :label="4" size="large" @click="reset" border>OCR识别</el-radio-button>
    </el-radio-group>
    <div class="change-wrap" >
      <el-select v-model="value" class="m-2" popper-class="popper" @click="selectClick1"  placeholder="请选择语言" size="large">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <img src="../assets/images/zhuanhuan.png" alt="">
      <el-select v-model="value2" class="m-2" popper-class="popper" @click="selectClick2" placeholder="请选择语言" size="large">
        <el-option
          v-for="item in options2"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button v-if="radio1==1||radio1==3"  @click="translate">翻译</el-button>
      <el-button v-if="radio1==4||radio1==2"  @click="translate2">识别</el-button>
      <div class="select_wrap" v-if="xiala" ref="tanchuang1">
        <el-input placeholder="搜索语言"></el-input>
        <div class="hr"></div>
        <div class="language_wrap">
          <el-scrollbar class="scrollbar">
            <div class="language_box" v-for="(value,key,index) in language" :key="index">
              <div class="firstWord">{{key}}</div>
              <span class="language" v-for="(item2,index) in value" @click="selectLanguage(item2)" :key="index">{{item2.name}}</span>
            </div>
          </el-scrollbar>
        </div>
      </div>
      <div class="select_wrap" v-if="xiala2" ref="tanchuang2">
        <el-input placeholder="搜索语言"></el-input>
        <div class="hr"></div>
        <div class="language_wrap">
          <el-scrollbar class="scrollbar">
            <div class="language_box" v-for="(value,key,index) in language" :key="index">
              <div class="firstWord">{{key}}</div>
              <span class="language" v-for="(item2,index) in value" @click="selectLanguage2(item2)" :key="index">{{item2.name}}</span>
            </div>
          </el-scrollbar>
        </div>
      </div>
    </div>
    <div class="import-wrap">
      <div class="importBox1">
        <span class="importTitle" v-if="radio1==1||radio1==3">{{value||'检测源语言'}}</span>
        <span class="importTitle" v-if="radio1==2||radio1==4">见识别文件</span>
        <el-input
        v-if="radio1==1||radio1==3"
          v-model="textarea"
          :rows="2"
          type="textarea"
          placeholder="输入翻译"/>
          <el-upload
          v-if="radio1==2||radio1==4"
          ref="upload"
            class="upload-demo"
            drag
            limit="1"
            :before-upload="brforeUpload"
            :on-change="uploadChange"
            :on-success="uploadSuccess"
            :on-error="uploadError"
            action="/api/common/upload">
            <img style="width:64px;height:64px;marginLeft:45px" src="../assets/images/fileIcon.png" alt="">
            <div class="el-upload__text">
              点击文件上传
            </div>
            <div class="el-upload__text2">
              支持扩展名：XML、TXT、DOC、DOCX、PDF或JPG格式
            </div>
            <!-- <template #tip>
              <div class="el-upload__tip">
                jpg/png files with a size less than 500kb
              </div>
            </template> -->
          </el-upload>
      </div>
      <!-- <div class="importBox3" v-if="radio1==2||radio1==3">
        <el-button class="button1">英语</el-button>
        <el-button class="button2" @click="translate2">翻译</el-button>
      </div> -->
      <div class="importBox2">
        <span class="importTitle">{{value2||'英语（美式）'}}</span>
        <el-input
          v-model="textarea2"
          :rows="2"
          type="textarea"/>
      </div>
    </div>
    <div class="jilu-wrap">
      <div class="jilu"><img src="../assets/images/jilu.png" alt="">翻译记录</div>
      <div class="first" v-if="radio1==1" @click="goYouHua">优化翻译</div>
    </div>
    <div :class="`${prefixCls}-content dynamicInfo flex flex-col `">
      <el-form size="large" inline>
        <el-row>
          <el-col :span="9">
            <el-form-item label="选择日期">
              <el-date-picker type="daterange" v-model="searchData.selecttime" placeholder="请选择日期"/>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item label="名称">
              <el-input v-model="searchData.name" placeholder="请输入关键字"/>
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="0" class="flex justify-end">
            <el-form-item style="margin-right: 0;">
              <el-button type="primary" @click="getList(currentTab)">查询</el-button>
              <el-button @click="resetData">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        
      </el-form>
      <el-table v-loading="loading" size="large" border :data="content" :empty-text="'暂无数据'"
                :header-cell-style="{background: '#f5f5f5', color:'#333333'}">
        <el-table-column show-overflow-tooltip align="center" label="时间"
                         prop="date"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="名称"
                         prop="name">
          <template #header>
            <span>名称</span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="附件"
                         prop=""></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="操作"
                         prop="notification_time_text">
          <span style="color:#E60B0B"><a href="">下载</a></span>
        </el-table-column>
      </el-table>
      <div class="mt-32">
        <Pagination
          :current="current"
          :limit="limit"
          :pages="pagesRef"
          :total="total"
          @change="onChangePage"
        />
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import dayjs from "dayjs";
import { onClickOutside } from "@vueuse/core"
import { genFileId } from 'element-plus'

const {translateApi} = useApi()

const textarea = ref('')
const textarea2 = ref('')
const xiala = ref(false)
const xiala2 = ref(false)
// const textarea2 = ref('')
const radio1 = ref(1)
const {setTotal, current, total, setCurrentPage, limit, pagesRef} = usePagination(1, 6, 0)
const currentTab = ref({});
let language = reactive({})

const options = reactive([
  {value:'zh',lable:'中文（简体）'},
  {value:"en",lable:'英文'},
])
const options2 = reactive([
  {value:'zh',lable:'中文（简体）'},
  {value:"en",lable:'英文'},
])
const value2 = ref('')
const value = ref('')
const abbreviation1 = ref('')
const abbreviation2 = ref('en')
const searchData = ref({
  selecttime:'',
  name:''
})
const loading = ref(true);
const content = ref([]);
const tanchuang1 = ref()
const tanchuang2 = ref()
const upload = ref()
const fileUrl = ref('')


definePageMeta({
  name: '翻译',
  hidden:true,
  // headerHost: false,
  // homeBanner:true,
  // order: 1,
})
//   const props = defineProps(['title']);
  const {prefixCls} = useDesign('mai-wrap');

// watch(radio1, ()=>{
//   value.value = ''
//   value2.value = ''
//   abbreviation1.value = ''
//   abbreviation2.value = ''
//   textarea.value = ''
//   textarea2.value = ''
  
// })
function brforeUpload(file:any){
  console.log(file)

}
function uploadChange(){
  
}
function uploadSuccess(response:any){
  ElMessage.success(response.msg)
  console.log(typeof response.data.url)
  fileUrl.value = response.data.url
}
function uploadError(response:any){
  ElMessage.error(response.msg)
}
// function handleExceed(files:any){
//   upload.value!.clearFiles()
//   const file = files[0]
//   file.uid = genFileId()
//   upload.value!.handleStart(file)
// }
function resetData(flag: boolean = true) {
  searchData.value = {
    selecttime:'',
    name:''
  }
  flag && getList(currentTab.value)
}
function goYouHua(){
  radio1.value = 3
}
function reset(){
  value.value = ''
  value2.value = ''
  abbreviation1.value = ''
  abbreviation2.value = ''
  textarea.value = ''
  textarea2.value = ''
}
function onChangePage(val: number) {
  setCurrentPage(val)
  getList(currentTab.value)
}

function selectClick1() {
  // debugger
  xiala.value = !xiala.value
}

function selectClick2() {
  xiala2.value = !xiala2.value
}
function selectLanguage(item:any) {
  console.log(item)
  if(xiala.value){
    value.value = item.name
    abbreviation1.value = item.abbreviation
    xiala.value = false
  }
}
function selectLanguage2(item:any) {
  console.log(item)
  if(xiala2.value){
    value2.value = item.name
    abbreviation2.value = item.abbreviation
    xiala2.value = false
  }
  
  
}

async function translate() {
  if(radio1.value==1){
    const {data:data} = await translateApi.translateText({content:textarea.value,source:abbreviation1.value,target:abbreviation2.value})
    textarea2.value = data
  }
  if(radio1.value==3){
    const {data:data} = await translateApi.translateOptimizeText({content:textarea.value,source:abbreviation1.value,target:abbreviation2.value})
    textarea2.value = data
  }
  // console.log(data)
}
async function translate2() {
  const {data:data} = await translateApi.translateFile({file:fileUrl.value,source:abbreviation1.value,target:abbreviation2.value})
  // console.log(data)
  // textarea2.value = data
}
async function allLanguage() {
  const {data:data} = await translateApi.getAllLanguage({})
  language = data
}

async function getList(data: any) {
  if (data) {
    loading.value = true;
    const searchParams = {
      ...searchData.value,
      start_date:'',
      end_date: '',
      type:0
    }
    if (searchData.value.selecttime) {
      searchParams.start_date = dayjs(searchData.value.selecttime[1]).format('YYYY-DD-MM hh:mm:ss')
      searchParams.end_date = dayjs(searchData.value.selecttime[0]).format('YYYY-DD-MM hh:mm:ss')
      // @ts-ignore
      delete searchParams.selecttime;
    }
    searchParams.type = radio1.value
    const {data: listDatas} = await translateApi.getTranslateList({
      ...searchParams,
      page: current.value,
      limit: limit,
      category_id: data.id
    })
    content.value = listDatas.data;
    loading.value = false;
    setTotal(listDatas.total)
  }
}

  onClickOutside(tanchuang1, ()=>{
        xiala.value = false

  })
  onClickOutside(tanchuang2, ()=>{
    xiala2.value = false
  })
  getList(currentTab.value)
  allLanguage()
</script>
<style lang="scss">
.popper{
  display: none !important;
}
</style>
<style lang="scss" scoped>
.wrap{
  padding: 0 244px;
  background-color: #EEF1F9;
  padding-bottom: 50px;
}
.el-radio-group{
  margin-top: 42px;
  margin-bottom: 76px;
}
.el-radio-button{
  border-radius: 16px;
  margin-right:12px ;
  border: 0px;
  outline: none;
  width: 218px;
  height: 61px;
}
::v-deep .el-radio-button__inner{
    width: 218px;
    height: 61px;
    border: 0px;
    outline: none;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 20px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #333333;
    border-radius: 16px !important;
    
  }
::v-deep .el-input__wrapper{
    width: 218px;
    height: 61px;
    background: #FFFFFF;
    border-radius: 16px;
    border-color:#ffffff ;
}
::v-deep .el-input__inner{
  text-align: center;
}
.change-wrap{
  display: flex;
  align-items: center;
  margin-bottom: 24px;
  position: relative;
  ::v-deep .el-input__wrapper{
    font-size: 20px !important;
  }
  .el-button{
    width: 218px;
    height: 61px;
    background: #022950;
    border-radius: 16px;
    color: #FFFFFF;
    font-size: 20px;
    margin-left: 24px;
  }
  img{
    width: 55px;
    height: 44px;
    margin: 0 28px 0 34px;
  }
}
::v-deep .el-textarea__inner{
  width: 100%;
  height: 239px;
  border-radius: 16px;
  border: 0px;
  padding-top: 17px;
  padding-left: 37px;
}
.el-textarea{
  border-radius: 16px;
  margin-top: 17px;
  font-size: 36px;
}
.import-wrap{
  display: flex;
  justify-content: center;
  // align-items: center;
  .importBox1{
    flex: 1;
    margin-right: 32px;
  }
  .importBox3{
    flex: 0.25;
    margin-right: 22px;
    .el-button{
      width: 150px;
      height: 55px;
      margin: 0;
      border-radius: 8px;
      font-size: 20px;
    }
    .button1{
      margin-bottom: 24px;
      color: #BABABA;
      background-color: #fff;

    }
    .button2{
      color: #fff;
      background-color: #022950;
    }
  }
  .importBox2{
    flex: 1;
  }
}
.importTitle{
  width: 120px;
height: 28px;
font-size: 20px;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #333333;
line-height: 28px;
margin-left: 10px;
}
.el-select{
  margin: 0;
}
.jilu-wrap{
  display: flex;
  justify-content: space-between;
  font-size: 20px;
  margin-top: 30px;
  .jilu{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 158px;
    height: 49px;
    background: #E8EBF5;
    border-radius: 16px;
    color: #333333;
    img{
      width: 20px;
      height: 20px;
      margin-right: 12px;
    }
  }
  .first{
    color: #E60B0B;
    cursor: pointer;
  }
}
.yangliu-mai-wrap-content{
  margin: 30px 0 0 0;
  width: 100%;
}
// .dynamicInfo {
//   margin-top: 0;
//   padding-bottom: 100px;
// }
::v-deep .el-table__inner-wrapper{
  font-size: 20px;

  }
::v-deep .el-table__header-wrapper{
  height: 80px;
}
::v-deep .el-table__header{
  height: 80px;
}
::v-deep .el-table__empty-block{
  height: 300px !important;
}
:deep(.el-form) {
  margin-bottom: 43px;
  .el-form-item__label {
    height: 60px;
    line-height: 60px !important;
    padding-right: 16px;
    @apply text-xl;
  }
  
  .el-input__wrapper {
    border-radius: 10px;
    height: 60px;
    width: 448px;
    box-shadow: unset;
    background-color: #ffffff;
    @apply text-xl;
  }
  .el-icon {
    font-size: 20px;
  }
  .el-range-input {
    font-size: 20px;
  }
  .el-button {
    width: 150px;
    height: 60px;
    box-shadow: unset;
    @apply text-xl rounded-xl;
    &--primary{

      background-color: #022950;
    }
  }
}
::v-deep .el-upload{
  margin-top: 17px;
border-radius: 16px;
}
::v-deep .el-upload-dragger{
  height: 239px;
border-radius: 16px;
}
::v-deep .el-upload{
  height: 239px;
border-radius: 16px;
}
.el-upload__text{
font-size: 16px;
font-family: PingFangSC-Semibold, PingFang SC;
font-weight: 600;
color: #333333;
line-height: 22px;
margin-bottom: 49px;
display: flex;
justify-content: center;
align-items: center;
}
.el-upload__text2{
font-size: 14px;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #999999;
line-height: 20px;
display: flex;
justify-content: center;
align-items: center;
}
::v-deep .el-table__cell {
    padding: 26px 0 !important;
}
.select_wrap{
  width: 100%;
  // width: 1436px;
  padding-left: 32px;
  padding-right: 40px;
  height: 774px;
  background: #FFFFFF;
  box-shadow: 0px 0px 22px 7px rgba(224,224,224,0.5);
  border-radius: 16px;
  position: absolute;
  top: 65px;
  left: 0;
  z-index: 100;
  ::v-deep .el-input__inner {
    text-align: left;
    font-size: 24px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #CACACA;
  }
  .hr{
    width: 100%;
    height: 1px;
    background: #D8D8D8;
  }
  .scrollbar{
    ::v-deep .el-scrollbar__bar{
      width: 13px !important;
      border-radius: 6.5px !important;
    }
  }
  .language_wrap{
    height: 698px;
    overflow-y: auto;
    padding-bottom: 64px;
    .language_box{
      padding: 0 5px;
      .firstWord{
        font-size: 18px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #999999;
        margin-top: 24px;
      }
      .language{
        font-size: 20px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #333333;
        margin-top: 20px;
        display: inline-block;
        margin-right: 24px;
        cursor: pointer;
      }
    }
  }
}
::v-deep .el-input__wrapper {
  border: none !important;
  box-shadow: none !important;
  // padding: 0px; //前边边距去掉
}

</style>