<template>
  <div class="app-container">
    <div>
      <el-button type="primary" icon="Back" @click="handleBack" style="margin-bottom: 10px"
        >返回</el-button
      >
    </div>

    <el-card shadow="never">
      <div class="cardHeader">
        <div class="left">
          <img class="iconBox" src="@/assets/images/icon1.png" alt="" />
          <div class="rightBox">
            <div class="num">{{ processInfo.fnaNo || '--' }}</div>
            <div class="bottom">
              <div class="status">{{ processInfo.status || '--' }}</div>
              <div class="time">
                <span class="iconfont icon-yanqiweiwancheng"></span>
                <span
                  >{{ parseTime(processInfo.createTime) }} 由{{
                    processInfo.customerName || '--'
                  }}创建</span
                >
              </div>
            </div>
          </div>
        </div>
        <div class="right">
          <el-button
            :disabled="!(processInfo.customerBizId && processInfo.fnaFormBizId)"
            type="success"
            :icon="Check"
            >{{ processInfo.appointmentBizId ? '已预约' : '立即预约' }}</el-button
          >
          <el-button
            :disabled="!(processInfo.customerBizId && processInfo.fnaFormBizId)"
            type="primary"
            plain
            >生成副本</el-button
          >
        </div>
      </div>
      <div class="tabsBox">
        <el-tabs v-model="activeName" class="demo-tabs" :before-leave="beforeTabLeave">
          <el-tab-pane v-for="tab in tabsList" :key="tab.name" :label="tab.label" :name="tab.name">
            <div class="tabPaneBox">
              <div v-if="tab.name === 'overview'" class="overviewBox">
                <div
                  class="oneItem"
                  v-for="item in tabsList.filter(item => item.id !== -1 && item.status)"
                  :key="item.id"
                  @click="handleStep(item)"
                >
                  <div
                    class="circle"
                    :class="{
                      finfishCircle: item.status == '1',
                      unFinishCircle: item.status == '0'
                    }"
                  >
                    <el-icon v-if="item.status == '1'"><Check /></el-icon>
                    <span v-else>{{ item.id }}</span>
                  </div>
                  <div
                    class="title"
                    :class="{
                      finfishTitle: item.status == '1',
                      unFinishTitle: item.status == '0'
                    }"
                  >
                    {{ item.title }}
                  </div>
                  <div
                    class="status"
                    :class="{
                      finfishStatus: item.status == '1',
                      unFinishStatus: item.status == '0'
                    }"
                  >
                    {{ item.status == '1' ? '已完成' : '未完成' }}
                  </div>
                  <div class="operation">
                    <el-icon class="editIcon" v-if="item.status == '0'"><EditPen /></el-icon>
                    <div
                      :class="{
                        finfishOperation: item.status == '1',
                        unFinishOperation: item.status == '0'
                      }"
                    >
                      {{ item.status == '1' ? '点击查看详情' : '去填写' }}
                    </div>
                  </div>
                </div>
              </div>
              <Customer
                v-if="tab.name === 'customer'"
                :activeName="activeName"
                :fearthStatus="route.query.type"
                :customerBizId="processInfo.customerBizId"
                @handleSuccess="handleSuccess"
              />
              <div v-if="tab.name === 'fnaform'">FNA表单内容</div>
              <div v-if="tab.name === 'appointment'">关联预约内容</div>
              <div v-if="tab.name === 'newpolicy'">关联新单内容</div>
              <div v-if="tab.name === 'policy'">关联保单内容</div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-card>
  </div>
</template>
<script setup name="FnaEdit">
import useUserStore from '@/store/modules/user'
import { addFna, getProcessDetail, updateProcess } from '@/api/sign/fna'
import Customer from './components/customer'
import { Check } from '@element-plus/icons-vue'
import { ref } from 'vue'
const userStore = useUserStore()
const { proxy } = getCurrentInstance()
const route = useRoute()
const router = useRouter()
const activeName = ref('overview')
const processInfo = ref({
  fnaNo:'暂无',
  status:'未完成',
  createTime: proxy.parseTime(new Date()),
  customerName: userStore.name
}) // 流程详情信息
const updateStatus = ref(false)
const tabsList = ref([
  {
    label: '总览',
    name: 'overview',
    id: -1
  },
  {
    label: '客户资料',
    name: 'customer',
    id: 1,
    status: '0',
    key: 'customerBizId'
  },
  {
    label: 'FNAform',
    name: 'fnaform',
    id: 2,
    status: '0',
    key: 'fnaFormBizId'
  },
  {
    label: '关联预约',
    name: 'appointment',
    id: 3,
    status: '0',
    key: 'appointmentBizId'
  },
  {
    label: '关联新单',
    name: 'newpolicy',
    id: 4,
    status: '0',
    key: 'policyBizld'
  },
  {
    label: '关联保单',
    name: 'policy',
    id: 5,
    status: '0',
    key: 'policyNo'
  }
])


const { csf_fna_status } = proxy.useDict('csf_fna_status')
// 更新流程
const processUpdate = (data, status) => {
  updateProcess(data).then(async res => {
    if (res.code == 200) {
      updateStatus.value = false
      // 获取流程详情
      getProcessInfo(processInfo.value.fnaBizId, status)
    }
  })
}
// 新建流程
const getAddInfo = () => {
  addFna({ remark: '' }).then(res => {
    if (res.code == 200) {
      // 获取流程详情
      getProcessInfo(res.data.fnaBizId)
    }
  })
}
// 获取流程详情
function getProcessInfo(fnaBizId, changeTab) {
  getProcessDetail(fnaBizId).then(res => {
    if (res.code == 200) {
      processInfo.value = res.data
      tabsList.value.forEach(item => {
        if (res.data[item.key] && item.status) {
          item.status = '1'
        } else {
          item.status = '0'
        }
      })
      csf_fna_status._object.csf_fna_status.forEach(item => {
        if (item.value == res.data.status) processInfo.value.status = item.label
      })
      if (changeTab) {
        activeName.value = changeTab
      }
    }
  })
}
// Tab切换前的验证
const beforeTabLeave = (activeTabName, oldTabName) => {
  // 如果切换到总览，始终允许
  if (activeTabName === 'overview' || activeTabName === 'customer') {
    return true
  }

  // 获取当前要切换到的tab
  const targetTab = tabsList.value.find(tab => tab.name === activeTabName)
  // 如果目标tab不存在，阻止切换
  if (!targetTab) {
    return false
  }
  // 检查前一项状态
  const prevTabIndex = tabsList.value.findIndex(tab => tab.name === activeTabName) - 1
  if (prevTabIndex >= 0) {
    const prevTab = tabsList.value[prevTabIndex]
    // 如果前一项存在且未完成，阻止切换
    if (prevTab.status === '0') {
      proxy.$modal.confirm(`请先填写${prevTab.label}`, { showCancel: '0', title: '填写提示' })
      return false
    }
  }

  return true
}

// 处理步骤点击
const handleStep = item => {
  // 总览页可以直接切换
  if (item.id === -1 || item.name === 'customer') {
    activeName.value = item.name
    return
  }
  // 检查前一项状态
  const prevTabIndex = tabsList.value.findIndex(tab => tab.id === item.id) - 1
  if (prevTabIndex >= 0) {
    const prevTab = tabsList.value[prevTabIndex]

    // 如果前一项存在且未完成，阻止切换
    if (prevTab.status === '0') {
      proxy.$modal.confirm(`请先填写${prevTab.label}`, { showCancel: false })
      return
    }
  }

  // 允许切换
  activeName.value = item.name
}
const handleBack = () => {
  router.go(-1)
}

if (route.query.type == 'add') {
  // getAddInfo()
} else if (route.query.type == 'edit') {
  getProcessInfo(route.query.fnaBizId)
}
const handleSuccess = info => {
  switch (info.tab) {
    case 'customer':
      if (info.type == 'add') {
        //客户提交成功，更新流程
        processUpdate(
          { fnaBizId: processInfo.value.fnaBizId, customerBizId: info.customerBizId },
          'fnaform'
        )
      } else {
        processUpdate({ fnaBizId: processInfo.value.fnaBizId, customerBizId: info.customerBizId })
      }

      break
    case 'fnaform':
      break
    case 'newpolicy':
      break
    case 'policy':
      break
    default:
      break
  }
}
// handleDomData()
// 1.通过新建流程进来的，要请求创建/csf/api/Fna/add拿到头部dom等信息
//2.每次进来要请求根据id获取/csf/api/Fna/get/vo，根据流程BizId获取进行到哪一步了，还可以拿到头部dom等信息
//3.每个模块保存完要重新请求/csf/api/Fna/get/vo，还要再请求/csf/api/Fna/update，更新后端数据库
</script>
<style lang="scss" scoped>
::v-deep .el-card {
  border: none !important;
}
::v-deep .el-input-group__append,
.el-input-group__prepend {
  background-color: #fff !important;
}

.app-container {
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 84.5px);
  padding: 20px;
  background-color: #f2f3f5;
  .cardHeader {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    .left {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .iconBox {
        width: 40px;
        height: 40px;
        margin-right: 5px;
        background-color: #e8f3ff;
        border-radius: 3px;
        text-align: center;
        padding: 5px;
      }
      .rightBox {
        margin-left: 10px;
        display: flex;
        flex-direction: column;
        .num {
          font-size: 14px;
        }
        .bottom {
          display: flex;
          align-items: center;
          margin-top: 3px;
          .status {
            font-size: 11px;
            color: #00b42a;
            background-color: #e8ffea;
            padding: 4px 6px;
            border-radius: 2px;
            margin-right: 10px;
          }

          .time {
            font-size: 12px;
            color: #86909c;
            .icon-yanqiweiwancheng {
              margin-right: 5px;
              font-size: 12px;
            }
          }
        }
      }
    }
  }
  .tabsBox {
    .tabButton {
      box-shadow: 0 -1px 14px #00000014;
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      padding-right: 20px;
      padding-top: 20px;
      .sumbitBtn {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 16px;
        height: 60px;
        background-color: #165dff;
        color: #fff;
        padding: 0 30px;
        .buttonIcon {
          font-size: 16px;
          color: #fff;
        }
      }
    }
  }
  .tabPaneBox {
    height: calc(100vh - 317px);
    overflow-y: auto;
    padding-right: 10px;
  }
  .overviewBox {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .oneItem {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      .circle {
        width: 35px;
        height: 35px;
        box-sizing: border-box;
        border-radius: 50%;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-bottom: 5px;
      }
      .el-icon {
        font-size: 16px;
        font-weight: 600;
      }
    }
    .finfishCircle {
      background-color: #e8f3ff;
      color: #165dff;
    }
    .todoCircle {
      background-color: #165dff;
      color: #fff;
    }
    .unFinishCircle {
      background-color: #f2f3f5;
      color: #4e5969;
    }
    .title {
      font-size: 14px;
      margin-bottom: 5px;
    }
    .finfishTitle {
      color: #1d2129;
      font-size: 14px;
    }
    .unFinishTitle {
      color: #4e5969;
      font-size: 13px;
    }
    .status {
      font-size: 12px;
      margin-bottom: 8px;
      color: #86909c;
    }
    .finfishStatus {
      color: #165dff;
    }
    .unFinishStatus {
      color: #4e5969;
    }
    .operation {
      display: flex;
      align-items: center;
      font-size: 12px;
      .editIcon {
        margin-right: 3px;
        color: #165dff;
        font-size: 12px;
      }
    }
    .finfishOperation {
      color: #4e5969;
    }
    .unFinishOperation {
      color: #165dff;
    }
  }
}
</style>
