<!--
 * @Author: hisense.liangjunhua
 * @Date: 2022-06-13 10:22:27
 * @LastEditors: hisense.liangjunhua
 * @LastEditTime: 2022-06-17 19:31:27
 * @Description: 应用上架
-->
<template>
  <div class="box">
    <div class="header">能力上架申请</div>
    <div class="top">
      <div
        v-for="(nav, index) in navList"
        :key="nav"
        :class="[index <= navList2.indexOf(showView) ? 'finish' : '']"
      >
        <span class="bg-box">
          <span>{{ index + 1 }}</span>
        </span>
        <span>{{ nav.name }}</span>
        <div class="line" v-if="index !== navList.length - 1"></div>
      </div>
    </div>
    <div class="vue-box">
      <put-on-the-shelf
        v-if="showView === '基本信息' && refData.name"
        :refData="refData"
        :dataFrom="dataFrom"
        :videoList="videoList"
        :imgList="imgList"
        :externalField="['应用名称', '应用描述']"
      ></put-on-the-shelf>
      <put-on-the-shelf
        v-else-if="showView === '功能介绍'"
        :refData="refData"
        :dataFrom="dataFrom"
        :configure="gnjs"
      ></put-on-the-shelf>
      <put-on-the-shelf
        v-else-if="showView === '关联组件'"
        :refData="refData"
        :dataFrom="dataFrom"
        :configure="glzj"
      ></put-on-the-shelf>
      <put-on-the-shelf
        v-else-if="showView === '部署与应用'"
        :refData="refData"
        :dataFrom="dataFrom"
        :externalField="['共享条件', '共享类型']"
        :configure="bs"
      ></put-on-the-shelf>
      <!-- <application-essential-information
        v-if="showView === '基本信息' && refData.name"
        :refData="refData"
        :dataFrom="dataFrom"
        :videoList="videoList"
        :imgList="imgList"
      ></application-essential-information>
      <application-function-introduction
        v-else-if="showView === '功能介绍'"
        :refData="refData"
        :dataFrom="dataFrom"
      ></application-function-introduction>
      <application-associated-components
        v-else-if="showView === '关联组件'"
        :refData="refData"
        :dataFrom="dataFrom"
      ></application-associated-components>
      <application-deployment-use
        v-else-if="showView === '部署与应用'"
        :refData="refData"
        :dataFrom="dataFrom"
        :fileList="fileList"
        :imgList="imgList"
      ></application-deployment-use> -->
    </div>
    <div class="btn" :class="showView === '基本信息' ? 'first' : ''">
      <button v-show="showView !== '基本信息'" @click="back()">上一步</button>
      <button v-show="showView === '部署与应用'" @click="preview()">
        预览
      </button>
      <button v-show="showView !== '部署与应用'" @click="next()">下一步</button>
      <button v-show="showView === '部署与应用'" @click="submit()">提交</button>
    </div>
  </div>
</template>
<script setup>
  // import ApplicationEssentialInformation from '@/views/capacityOnTheShelf/components/ApplicationEssentialInformation.vue'
  // import ApplicationFunctionIntroduction from '@/views/capacityOnTheShelf/components/ApplicationFunctionIntroduction.vue'
  // import ApplicationAssociatedComponents from '@/views/capacityOnTheShelf/components/ApplicationAssociatedComponents.vue'
  // import ApplicationDeploymentUse from '@/views/capacityOnTheShelf/components/ApplicationDeploymentUse.vue'
  import PutOnTheShelf from '@/views/capacityOnTheShelf/components/PutOnTheShelf.vue'
  import { ref, onBeforeUnmount } from 'vue'
  import { useRouter } from 'vue-router'
  import { getCategoryTree } from '@/api/personalCenter'
  import { shangjiainsert, shangjiaapply } from '@/api/personalCenter'
  import { getUser, getUserInfo } from '@/api/home'
  import { message } from 'ant-design-vue'
  import mybus from '@/myplugins/mybus'
  const showView = ref('基本信息')
  const router = useRouter()
  const navList = ref([])
  const navList2 = ref([])
  const fileList = ref({})
  const imgList = ref({})
  const videoList = ref({})
  const data = ref({})
  const refData = ref({})
  const submitFlag = ref(true)
  const dataFrom = ref({
    infoList: [],
    delFlag: 0,
    type: '应用资源',
    deptId: '',
  })
  const gnjs = ref([
    {
      name: '功能介绍',
      list: [
        {
          name: '功能名称',
          field: 'name',
          type: 'input',
          note1: '',
        },
        {
          name: '功能描述',
          field: 'desc',
          type: 'textArea',
          note1: '',
        },
        {
          name: '功能图片',
          field: 'img',
          type: 'image',
          note1: '',
        },
      ],
    },
  ])
  const glzj = ref([
    {
      name: '关联组件',
      list: [
        {
          name: '关联组件名称',
          field: 'name',
          type: 'input',
          note1: '',
        },
        {
          name: '关联组件描述',
          field: 'desc',
          type: 'textArea',
          note1: '',
        },
        {
          name: '关联组件地址',
          field: 'url',
          type: 'input',
          note1: '',
        },
      ],
    },
  ])
  const bs = ref([
    {
      name: '常见问题',
      list: [
        {
          name: '问题',
          field: 'question',
          type: 'textArea',
          note1: '',
        },
        {
          name: '答复',
          field: 'answer',
          type: 'textArea',
          note1: '',
        },
      ],
    },
  ])
  getUser().then((res) => {
    dataFrom.value.deptId = res.data.data.deptId
  })
  const back = () => {
    navList.value.forEach((val, index) => {
      if (val.key === showView.value && index > 0) {
        showView.value = navList2.value[index - 1]
        refData.value = data.value.filter(
          (item) => item.name === showView.value
        )[0]
        console.log('表单数据=============>', dataFrom.value)
      }
    })
  }
  const next = () => {
    let i = -1
    navList.value.forEach((val, index) => {
      if (val.key === showView.value && index < navList.value.length - 1) {
        i = index + 1
      }
    })
    if (i !== -1) {
      showView.value = navList2.value[i]
      refData.value = data.value.filter(
        (item) => item.name === showView.value
      )[0]
      console.log('表单数据=============>', dataFrom.value, refData.value)
    }
  }
  const submit = () => {
    console.log('提交数据=============>', dataFrom.value)
    if (submitFlag.value) {
      submitFlag.value = false
      dataFrom.value.infoList = dataFrom.value.infoList.filter(
        (item) => item.attrValue !== ''
      )
      shangjiainsert(dataFrom.value).then((res) => {
        const instanceId = res.data.data
        dataFrom.value.id = res.data.data
        getUser().then((res6) => {
          getUserInfo(res6.data.data.id).then((res2) => {
            const deptId = res2.data.data.deptId
            const userId = res2.data.data.id
            const userName = res2.data.data.realName
            const params = {
              instanceId: instanceId,
              deptId: deptId,
              userId: userId,
              userName: userName,
              resourceDTO: [dataFrom.value],
            }
            shangjiaapply(params).then((res3) => {
              console.log('res3', res3)
              if (res3.data.code == 0) {
                message.success('上架成功！')
                submitFlag.value = true
                window.setTimeout(() => {
                  window.close()
                }, 1000)
              }
            })
          })
        })
      })
    }
  }
  // 预览
  const preview = () => {
    dataFrom.value.infoList = dataFrom.value.infoList.filter(
      (item) => item.attrValue !== ''
    )
    console.log('预览==============>', dataFrom.value)
    window.sessionStorage.setItem('preview', JSON.stringify(dataFrom.value))
    const newpage = router.resolve({
      path: '/details',
    })
    window.open(newpage.href, '_blank')
  }
  const init = () => {
    getCategoryTree().then((res) => {
      // console.clear()
      res.data.data = res.data.data.filter(
        (item) => item.name === '应用资源一'
      )[0]
      navList.value = []
      navList2.value = []
      res.data.data.children.forEach((val) => {
        console.log(val)
        navList.value.push({
          name: val.name,
          key: val.name,
        })
        navList2.value.push(val.name)
      })
      data.value = res.data.data.children
      refData.value = data.value.filter(
        (item) => item.name === showView.value
      )[0]
      console.log('所有编目结构==============>', res.data.data)
    })
  }
  init()
  mybus.on('chageDataFrom', (obj) => {
    console.log(obj, dataFrom.value.infoList)
    dataFrom.value.infoList = dataFrom.value.infoList.filter(
      (item) => item.attrType !== obj.attrType
    )
    if (obj.attrValue == [] || obj.attrValue == '[]' || obj.attrValue == null) {
      obj.attrValue = ''
    }
    dataFrom.value.infoList.push(obj)
  })
  mybus.on('chageFileList', (obj) => {
    fileList.value = obj
  })
  mybus.on('chageImgList', (obj) => {
    imgList.value = obj
  })
  mybus.on('chageVideoList', (obj) => {
    videoList.value = obj
  })
  //datafrom外面数据
  mybus.on('chageDataFromDwon', (obj) => {
    switch (obj.attrType) {
      case '应用名称':
        dataFrom.value.name = obj.attrValue
        break
      case '应用描述':
        dataFrom.value.description = obj.attrValue
        break
      case '共享条件':
        dataFrom.value.shareCondition = obj.attrValue
        break
      case '共享类型':
        dataFrom.value.shareType = obj.attrValue
        break
      case '服务接口':
        dataFrom.value.apiUrl = obj.attrValue
        break
      case '接口请求方式':
        dataFrom.value.apiMethodType = obj.attrValue
        break
      case '访问地址':
        dataFrom.value.link = obj.attrValue
        break
      case '部门联系人':
        dataFrom.value.deptContacts = obj.attrValue
        break
      case '部门联系人电话':
        dataFrom.value.deptPhone = obj.attrValue
        break
    }
    // console.log('dataFrom.value', dataFrom.value)
  })
  onBeforeUnmount(() => {
    mybus.off('chageDataFrom')
    mybus.off('chageFileList')
    mybus.off('chageImgList')
    mybus.off('chageDataFromDwon')
  })
</script>
<style lang="less" scoped>
  .box {
    position: relative;
    top: 64px;
    margin: 15px 400px;
    padding: 10px;
    background: #fff;
    .header {
      font-size: 28px;
      font-weight: 600;
    }
    .vue-box {
      padding: 0 100px;
    }
    .top {
      margin: 10px 20px 0;
      padding: 15px 30px;
      background: #edf4fc;
      display: flex;
      justify-content: space-between;
      div {
        font-size: 18px;
        color: #999;
        display: flex;
        justify-content: center;
        align-items: center;
        .bg-box {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 45px;
          height: 45px;
          border-radius: 50%;
          border: 1px solid rgb(214, 214, 214);
          margin-right: 10px;
          transition: all 0.3s ease;
          span {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 30px;
            height: 30px;
            border-radius: 50%;
            border: 1px solid #999;
          }
        }
        .line {
          margin-left: 10px;
          width: 280px;
          height: 1px;
          background: #999;
        }
      }
      .finish {
        color: #0087ff;
        .bg-box {
          background: #9ccefa;
          color: #fff;
          border: 1px solid #9ccefa;
          span {
            background: #0087ff;
            border: 1px solid #0087ff;
          }
        }
        .line {
          background: #0087ff;
        }
      }
    }
    .btn {
      display: flex;
      justify-content: space-between;
      padding: 10px 400px 0;
      button {
        cursor: pointer;
        width: 80px;
        height: 35px;
        text-align: center;
        color: #fff;
        border: none;
        border-radius: 6px;
        background: #0087ff;
      }
      button:nth-of-type(1) {
        color: #0087ff;
        background: #e1edfa;
      }
      button:nth-of-type(2) {
        background: #01c5dc;
      }
    }
    .first {
      justify-content: center;
    }
  }
</style>
