<template>

  <headerBar title="主图与详情生成" />

  <div class="detail-container">
    <div>
      <!-- 处理模式 -->
      <div class="logo-section flex left top" >
        <div class="section-title" style="margin-bottom: 0px;">
          <img src="@/assets/images/Photography/zhuangshi.png" style="width: 32px; height: 32px;" />
          处理模式：

            <el-checkbox v-model="form.is_only_cutout"
                         true-value="1"
                         false-value="0"
                         true-label="1"
                         false-label="0"
                         label="仅抠图模式"/>
        <span class="flex left" style="font-weight: normal; font-size: 12px; color: #666">
          <el-icon><QuestionFilled /></el-icon>
          如您勾选仅抠图模式，将只做抠图处理，不再自动生成详情页。
        </span>
        </div>
      </div>

      <template v-if="form.is_only_cutout == 0">
      <!-- 主图LOGO部分 -->
      <div class="logo-section flex left top" >
        <div class="section-title" style="margin-bottom: 0px;">
          <img src="@/assets/images/Photography/zhuangshi.png" style="width: 32px; height: 32px;" />
          主图LOGO：
        </div>
      </div>
      <div class="logo-section flex left top multi-line">
        <upload v-for="item,index in logoList"   :value="item" :key="item"
                v-show="item"
                @input="onRemove(index)"
                class="mar-right-10 upload-item"
                :class="{
                    active: item === form.logo_path
                }"
                @click.native="form.logo_path = item"
        ></upload>
        <upload @input="onInput"></upload>
      </div>

      <el-divider />
<!--      &lt;!&ndash; 图片抠图与货号图生成 &ndash;&gt;
      <div class="section">
        <div class="section-title">
          <img src="@/assets/images/Photography/zhuangshi.png" style="width: 32px; height: 32px;" />
          图片抠图与货号图生成
        </div>
        <div class="section-content">
          <div v-if="showTips" class="instruction-out flex top left">
            <img style="fill: #000" src="@/assets/images/xinxi.svg" />
            <ol class="instruction-list">
              <li>请在下方确认图片拍摄过程中的顺序，确保所有拍摄的图片的顺序一致。</li>
              <li>使用中英文语号分隔。</li>
              <li>图片的名称不能随意修改，否则无法正常生成详情页。</li>
              <li>现有图片名称有：俯视、侧视、后视、鞋底、内里</li>
            </ol>
            <el-icon @click="showTips = false" class="close-icon">
              <Close />
            </el-icon>
          </div>

          &lt;!&ndash; 货号文件夹 &ndash;&gt;
          &lt;!&ndash;    <div class="form-item">
            <div class="label">货号文件夹：</div>
            <div class="folder-warp">
              <div class="folder-input">
                <el-input style="width: 60%;" v-model="folderPath" type="textarea" :rows="2" readonly
                  placeholder="请选择货号文件夹" />
                <el-button class="check-button" type="primary" @click="selectFolder">
                  <img src="@/assets/images/Photography/wenjian.png" style="width: 14px; " />
                  选择目标文件夹</el-button>
              </div>
              <div class="hint">
                <el-icon>
                  <Warning />
                </el-icon> <text>选择货号的上级文件夹</text>
              </div>
            </div>
          </div>
         &ndash;&gt;

        </div>
      </div>
      <el-divider />-->

      <!-- 选择详情模板部分 -->
      <div class="template-section ">
        <div class="flex between">
          <div class="section-title">
            <img src="@/assets/images/Photography/zhuangshi.png" style="width: 32px; height: 32px;" />
            选择详情模版
          </div>
          <div class="template-pagination">
            <el-pagination background layout="prev, pager, next" v-model:current-page="queryParams.current"
              v-model:page-size.sync="queryParams.size" :total="totalPage" @current-change="onCurrentChange"
              @size-change="onSizeChange" />
          </div>
        </div>

        <div class="template-list">
          <div v-for="(template, index) in visibleTemplates" :key="index" class="template-item"
            @click="form.selectTemplate = template">
            <el-image :src="template.template_cover_image" fit="contain" class="cur-p"
              style="width: 100%; display: block;" />
            <div class="select-warp" :class="form.selectTemplate.id == template.id ? 'active' : ''">
              <el-icon color="#FFFFFF">
                <Select />
              </el-icon>
            </div>
            <div class="template-info">
              <span class="mar-left-10 chaochu_1">{{ template.template_name }}</span>
              <div class="template-view" @click="viewTemplate(template)">查看</div>
            </div>
          </div>
        </div>

        <div class="template-tips c-333 fs-14 line-20 te-l mar-top-20 flex left">
          <el-icon><WarningFilled /></el-icon>
          <span class="mar-left-10">该模版图片顺序说明：{{form.selectTemplate.template_image_order}}</span>
        </div>
      </div>
      <el-divider />

      <!-- 详情高级配置 -->
<!--      <div class="section">
        <div class="section-title">
          <img src="@/assets/images/Photography/zhuangshi.png" style="width: 32px; height: 32px;" />
          详情高级配置
        </div>
        <div class="section-content">
          &lt;!&ndash; 图片顺序 &ndash;&gt;
          <div class="form-item">
            <div class="label">图片顺序：</div>
            <el-input v-model="imageOrder" placeholder="请输入图片顺序" class="specific-page-input">
              <template #append>
                <el-button class="explain-btn" link type="primary">说明</el-button>
              </template>
            </el-input>
          </div>

          &lt;!&ndash; 同款检验 &ndash;&gt;
          &lt;!&ndash;   <div class="form-item">
            <div class="label">同款检验：</div>
            <el-checkbox v-model="checkSimilar">同款下货号必须齐全</el-checkbox>
          </div>
&ndash;&gt;
          &lt;!&ndash; 可指定页面独修改 &ndash;&gt;
          &lt;!&ndash;      <div class="form-item">
            <div class="label">可指定页面独修改：</div>
            <el-input v-model="specificPage" placeholder="请输入入需要单独修改的页面，示例：4:1 (需修改模版的编号：第一张)"
              class="specific-page-input">
              <template #append>
                <el-button class="explain-btn" link type="primary">说明</el-button>
              </template>
            </el-input>
          </div>
          &ndash;&gt;
        </div>
      </div>
      <el-divider />-->
      <!-- 详情资料准备部分 -->
      <div class="data-prep-section">
        <div class="flex-item left">
          <div class="section-title">
            <img src="@/assets/images/Photography/zhuangshi.png" style="width: 32px; height: 32px;" />
            详情资料准备 (2选1)

            <el-button v-if="form.dataType == '1'" type="text" class="mar-left-10 fs-16"  @click="downloadExcel">下载商品基础资料模版</el-button>
          </div>
        </div>

        <div class="flex-item left">
          <el-radio-group v-model="form.dataType" class="ml-4">
            <el-radio label="1" size="large">EXCEL文件选择</el-radio>
            <el-radio label="2" size="large">系统对接（和业务员联系）</el-radio>
          </el-radio-group>
        </div>
        <div v-if="form.dataType == '1'" class="excel-upload">
          <div class="flex bottom between">
            <div style="max-width: 160px;" class="mar-left-20">商品基础资料EXCEL文件选择：</div>
            <div class="flex bottom mar-left-20" style="flex-grow: 1;">
              <el-input type="textarea" v-model="form.excel_path" />
            </div>
            <el-button class="select-button button--primary1  mar-left-20" type="primary" @click="selectExcel">
              <img src="@/assets/images/Photography/wenjian.png" style="width: 16px; margin-right: 4px;" />
              选择</el-button>
          </div>
        </div>
      </div>
      </template>
    </div>
    <!-- 底部按钮 -->
    <div class="footer">
      <!-- <el-button class="button--primary1 footer-button" type="primary" @click="saveConfig">保存配置</el-button> -->
      <!--  <el-button class="button--primary1 footer-button" type="primary" @click="startProcess">开始处理</el-button>  -->
      <el-button class="button--primary1 footer-button" type="primary" @click="generate">开始生成</el-button>
    </div>
  </div>


  <loading-dialog v-if="loadingDialogVisible" v-model="loadingDialogVisible" :requesting="requesting" :progress="progress" :message="message"
    :disabled-button="disabledButton" @button-click="handleComplete">
    <template v-if="partErrList && partErrList.length > 0" #errList>
      <div v-for="(item, idx) in partErrList" :key="idx">
        <span>{{ item.goods_art_no }}</span>：<span>{{ item.info }}</span>
      </div>
    </template>

  </loading-dialog>
  <el-dialog v-model="dialogVisible">
    <img style="width: 100%;" :src="dialogImageUrl" alt="Preview Image" />
  </el-dialog>



</template>

<script lang="ts" setup>

import { getCompanyTemplatesApi } from '@/apis/other'
import tokenInfo from '@/stores/modules/token';
import useUserInfo from "@/stores/modules/user";
import { useRoute, useRouter } from 'vue-router'


import { ElMessage, ElMessageBox } from 'element-plus'

import headerBar from '@/components/header-bar/index.vue'
import { ref, computed, reactive, onMounted } from 'vue';
import { Select } from '@element-plus/icons-vue'
import upload from '@/components/upload'
import client from "@/stores/modules/client";
import icpList from '@/utils/ipc'
const clientStore = client();
import { getRouterUrl } from '@/utils/appfun'

import { Close, Warning } from '@element-plus/icons-vue'
import LoadingDialog from '@/views/Photography/components/LoadingDialog.vue'


import { useCheckInfo } from '@/composables/userCheck';
useCheckInfo();

const showTips = ref(true)

const folderPath = ref('') //货号文件夹
// const reportMode = ref('normal') // 抠图模式
const imageOrder = ref('俯视、侧视、后跟、鞋底、内里、组合、组合2、组合3') // 图片顺序
const checkSimilar = ref(false) // 同款检验
const specificPage = ref('')  // 可指定页面独修改

// 路由和状态管理初始化
const route = useRoute();
const router = useRouter();

// 完成目录
const completeDirectory = ref('')


const loadingDialogVisible = ref(false)
const progress = ref(0)
const message = ref('正在为您处理，请稍后')
const disabledButton = ref(true)

let templates = ref([])
let goods_art_nos = ref([])
let partErrList = ref([])
const excel_template_url = ref('')
// 是否正在请求接口
const requesting = ref(false)





// 定义一个定时器变量
const INTERVAL = ref<number | NodeJS.Timeout | null>(null);




// 状态变量
const totalPage = ref(0);
const itemsPerPage = 4; // 每页显示的模板数量
const dialogVisible = ref(false);
const dialogImageUrl = ref('');
const queryParams = reactive({ // 分页查询参数
  size: 1,
  current: 1,
})
const form = reactive({
  selectTemplate: {}, //选中的模板
  dataType: '1', // 1: 选择excel文件 2: 系统对接
  logo_path: '', // 主图LOGO
  excel_path: '', // 商品基础资料EXCEL文件选择
  is_only_cutout:0, //是否仅抠图模式
})
onMounted(() => {
  const goods_art_data = route.query.goods_art_nos
  goods_art_nos.value = Array.isArray(goods_art_data) ? goods_art_data : [goods_art_data]
  getCompanyTemplates()
  getLogolist()
})


// 计算属性，获取当前页可见的模板
const visibleTemplates = computed(() => {
  const startIndex = (queryParams.current - 1) * itemsPerPage;
  const data = templates.value.slice(startIndex, startIndex + itemsPerPage);
  return data
});
// 查看模板详情
const viewTemplate = (template) => {
  // 展示大图
  dialogVisible.value = true
  dialogImageUrl.value = template.template_preview_image

};
// 获取模版列表
const getCompanyTemplates = async () => {
  const { data } = await getCompanyTemplatesApi()
  templates.value = data.list
  // 默认选中第一个模板
  if (templates.value.length > 0) {
    form.selectTemplate = templates.value[0]
  }
  excel_template_url.value = data.excel_template_url
  // 计算总页数
  totalPage.value = Math.ceil(templates.value.length / itemsPerPage);
}

const downloadExcel =  () => {
  const a = document.createElement('a')
  a.href = excel_template_url.value,
  a.download = '商品基础资料模版'
  document.body.appendChild(a)
  a.click()
  setTimeout(() => {
    document.body.removeChild(a);
  }, 1000);
}




const onCurrentChange = (page) => {
  queryParams.current = page;
};
const onSizeChange = (data) => {
};



// 开始生成操作
const generate = async function () {
  if(form.is_only_cutout == 0 ){

    if ( form.dataType == '1'  && !form.excel_path) {
      ElMessage.error('请上传商品基础资料')
      return
    }
  }
  const tokenInfoStore = tokenInfo();
  const token = tokenInfoStore.getToken; // 使用 getToken() 获取 token
  let temp_list = []
  templates.value.map(item => {
    temp_list.push({
      template_id: item.template_id,
      template_local_classes: item.template_local_classes,
    })
  })
  const params = {
    goods_art_no: JSON.parse(JSON.stringify(goods_art_nos.value)),
    is_only_cutout: form.is_only_cutout || '0',
    logo_path: form.logo_path || '',
    temp_name: form.selectTemplate.template_id || '',
    excel_path: form.dataType == '1' ? form.excel_path : '',
    template_image_order: form.selectTemplate.template_image_order,
    temp_list,
    token,
  }
  // 开启进度弹窗
  requesting.value =  false
  console.log("params", "color:#3f7cff", params);
  partErrList.value = []
  message.value = '正在为您处理，请稍后'
  progress.value = 0
  openLoadingDialog(goods_art_nos.value.length * 10)
  clientStore.ipc.removeAllListeners(icpList.generate.generatePhotoDetail);
  clientStore.ipc.send(icpList.generate.generatePhotoDetail, params);
  clientStore.ipc.on(icpList.generate.generatePhotoDetail, (event, result) => {
    console.log('result', result)
    requesting.value =  true
    clientStore.ipc.removeAllListeners(icpList.generate.generatePhotoDetail);
    clearInterval(INTERVAL.value)
    if (result.code === 0) {
      const { output_folder, list } = result.data
      const allSuccess = list.every(item => item.success);
      const allFailure = list.every(item => !item.success);
      if (allSuccess) {
        console.log("全部成功")
        handleSuccess(output_folder, '全部生成成功')
      } else if (allFailure) {
        console.log("全部失败");
        handleFailure(list)
      } else {
        console.log("部分成功，部分失败");
        handlePartSuccess(output_folder, list)
      }
    } else {
      console.log('code全部生成失败')
      handleFail(result.msg)
    }
    //生成失败 (接口请求失败)
    function handleFail(errorMsg: string) {
      loadingDialogVisible.value = false
      disabledButton.value = false
      if (errorMsg) {
        ElMessage.error(errorMsg)
      }
    }

    // 全部生成成功
    function handleSuccess(href, loadingMsg) {
      completeDirectory.value = href
      progress.value = 100
      disabledButton.value = false
      message.value = loadingMsg
      handleComplete()
    }
    // 部分成功
    function handlePartSuccess(output_folder: string, partSuccessList) {
      let errorList = []
      partSuccessList.map(item => {
        if (!item.success) {
          errorList.push(item)
        }
      })
      partErrList.value = errorList
      handleSuccess(output_folder, '部分货号生成失败')
    }
    // 全部生成失败
    function handleFailure(partSuccessList) {
      let errorList = []
      partSuccessList.map(item => {
        if (!item.success) {
          errorList.push(item)
        }
      })
      partErrList.value = errorList
      completeDirectory.value = ''
      progress.value = 100
      disabledButton.value = true
      message.value = '全部货号生成失败'
    }
  });
}
const openLoadingDialog = (timer: number) => {
  loadingDialogVisible.value = true

  disabledButton.value = true
  // 根据传入的秒数计算每次增加的进度值
  const step = 100 / timer
  INTERVAL.value = setInterval(() => {
    if (progress.value < 100) {
      progress.value = Math.min(Math.round(progress.value + step),100)
    }
  }, 1000)
}


//logo
const logoList = ref([])
const onInput = (value) => {
  addLogo(value)
}
const onRemove = (index) => {
  if(logoList.value[index] === form.logo_path){
    form.logo_path = ''
  }
  clientStore.ipc.send(icpList.generate.deleteLogo,{
    path:logoList.value[index]
  });
  logoList.value.splice(index, 1)
  clientStore.ipc.on(icpList.generate.deleteLogo, async (event, result) => {
    console.log('deleteLogo');
    console.log(result);

    clientStore.ipc.removeAllListeners(icpList.generate.deleteLogo);
  })
}

const getLogolist = async () => {
  clientStore.ipc.send(icpList.generate.getLogoList);
  clientStore.ipc.on(icpList.generate.getLogoList, async (event, result) => {
    logoList.value = result.data || []
    console.log('getLogoList')
    console.log(result.data)
    if(logoList.value.length){
      form.logo_path = logoList.value[0]
    }
    clientStore.ipc.removeAllListeners(icpList.generate.getLogoList);
  })
}


const addLogo = async (path) => {
  console.log(path);
  clientStore.ipc.send(icpList.generate.addLogo,{
    logo_path:path
  });
  clientStore.ipc.on(icpList.generate.addLogo, async (event, result) => {
    console.log(result);

    if (result.code === 0) {
        console.log("添加成功")
      console.log(result)
      if(result.data.logo){
        form.logo_path = result.data.logo
        if(logoList.value.indexOf(result.data.logo) <0){
          logoList.value.push(result.data.logo)
        }
      }
    }
    clientStore.ipc.removeAllListeners(icpList.generate.addLogo);
  })
}


function selectExcel() {
  clientStore.ipc.removeAllListeners(icpList.utils.openFile);
  clientStore.ipc.send(icpList.utils.openFile, {
    filters: [
      { name: '支持xls,xlsx', extensions: ['xlsx', 'xls'] }
    ],
    title: "选择基础文件资料"
  });
  clientStore.ipc.on(icpList.utils.openFile, async (event, result) => {
    form.excel_path = result
    clientStore.ipc.removeAllListeners(icpList.utils.openFile);
  })
}


const Router = useRouter()
//打开主图详情
function openPhotographySeniorDetail() {

  const { href } = Router.resolve({
    name: 'PhotographySeniorDetail'
  })

  clientStore.ipc.removeAllListeners(icpList.utils.openMain);
  let params = {
    title: '详情高级配置',
    width: 1000,
    height: 630,
    frame: true,
    id: "PhotographySeniorDetail",
    url: getRouterUrl(href)
  }
  clientStore.ipc.send(icpList.utils.openMain, params);
}




const handleComplete = () => {
  loadingDialogVisible.value = false
  // 这里可以添加打开目录的逻辑
  clientStore.ipc.removeAllListeners(icpList.utils.shellFun);
  let params = {
    action: 'openPath',
    params: completeDirectory.value?.replaceAll('/', '\\')
  }
  clientStore.ipc.send(icpList.utils.shellFun, params);
}

const selectFolder = () => {
  clientStore.ipc.removeAllListeners(icpList.utils.openDirectory);
  clientStore.ipc.send(icpList.utils.openDirectory);
  clientStore.ipc.on(icpList.utils.openDirectory, async (event, result) => {
    folderPath.value = result
    clientStore.ipc.removeAllListeners(icpList.utils.openDirectory);
  })
}


</script>

<style lang="scss" scoped>
.detail-container {
  background-color: #EAECED;
  width: 100%;
  min-width: 600px;
  padding: 20px;
  overflow: hidden;
  min-height: calc(100vh - 30px);

}

.logo-section,
.template-section,
.data-prep-section {
  margin-bottom: 20px;
}

.logo-section {
  .upload-item {
    border: 2px solid rgba(0,0,0,0);

  }
  .active {
    border: 2px solid #2957FF;
    border-radius: 6px;
    overflow: hidden;;
  }

  &.multi-line {
    flex-direction: row; // 默认横向排列
    flex-wrap: wrap; // 允许换行
    align-items: flex-start; // 对齐方式调整为顶部对齐

    .upload-item {
      margin-bottom: 10px; // 每行之间增加间距
      width: 90px; // 每行显示 4 个元素，减去外边距
      box-sizing: border-box; // 确保宽度计算包含 padding 和 border
    }
  }
}
.logo-upload {
  border: 1px dashed #ccc;
  border-radius: 5px;
  padding: 50px 0;
  text-align: center;
  cursor: pointer;
}

.template-pagination button {
  margin-right: 5px;
}

.template-pagination span {
  display: inline-block;
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  border: 1px solid #ccc;
  border-radius: 5px;
  margin-right: 5px;
  cursor: pointer;
}


.template-list {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  margin-top: 10px;


  .template-item {
    width: calc(25% - 18px);
    border: 1px solid #ccc;
    border-radius: 10px;
    cursor: pointer;
    background: #f0f0f0;
    position: relative;
    overflow: hidden;

    .template-info {
      position: absolute;
      bottom: 0;
      left: 0;
      background: rgba($color: #000000, $alpha: .3);
      width: 100%;
      height: 36px;
      line-height: 36px;
      color: #eee;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .template-view {
        background: #DFE2E3;
        color: #3366FF;
        height: 30px;
        line-height: 30px;
        padding: 0 10px;
        border-radius: 4px;
        margin-right: 10px;
        font-size: 14px;
      }
    }
  }
}



.excel-upload {
  width: 100%;
  background: #F7F7F7;
  padding: 20px 0;
}

.generate-button {
  padding: 10px 20px;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.select-button {
  background: #DFE2E3 !important;
  color: #3366FF !important;
  height: 30px;
  line-height: 30px;
  padding: 0 10px;
  border-radius: 4px;
  margin-right: 10px;
  font-size: 14px;
  font-weight: 550;

}

.select-warp {
  width: 18px;
  height: 18px;
  border-radius: 4px;
  background-color: #fff;
  position: absolute;
  top: 10px;
  left: 10px;

  &.active {
    background-color: #1677FF;
  }
}

.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: bold;
  margin-bottom: 16px;
  color: #474747;
}



.section {
  margin-bottom: 24px;

  .section-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: bold;
    margin-bottom: 16px;
    color: #474747;
  }

  .section-content {
    padding-left: 16px;
  }
}

.instruction-out {
  background: #EAF3FF;
  border-radius: 4px;
  border: 1px solid #CBE1FF;
  padding: 10px 20px;
  width: 80%;
  position: relative;

  .instruction-list {
    margin: 0px 0 0 10px;
    padding-left: 20px;
    padding-right: 40px;
    width: 100%;

    li {
      margin-bottom: 4px;
      text-align: left;
      font-size: 14px;
    }
  }

  .close-icon {
    position: absolute;
    top: 12px;
    right: 19px;
  }
}



.form-item {
  margin: 16px 0;
  display: flex;


  .label {
    min-width: 120px;
    margin-right: 12px;
  }

  .folder-warp {
    width: 100%;
    display: flex;
    flex-direction: column;

    .folder-input {
      flex: 1;
      display: flex;
      align-items: center;

      .check-button {
        background: #DFE2E3;
        border-radius: 6px;
        padding: 6px 12px;
        color: #2957FF;
        margin-left: 20px;
      }

    }
  }

  .hint {
    text-align: left;
    color: #999;
    margin-top: 6px;
    font-size: 14px;
    color: #FF4C00;
    font-style: normal;
  }

  .specific-page-input {
    flex: 1;
  }
}

.image-order {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.footer {
  display: flex;
  justify-content: center;
  margin-top: 24px;

  .footer-button {
    padding: 10px 20px;
  }
}

.explain-btn {
  padding-left: 20px;
  padding-right: 20px;
  color: #2957FF !important;
}
</style>
