<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="tabsBox">
        <el-tabs v-model="activeName" type="card" 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 === 'status'">新单状态</div>
              <div v-if="tab.name === 'info'">

                <PolicyInfo
                  :activeName="activeName"
                  :policyBizId="route.query.policyBizId"
                  :dictTypeLists="dictTypeLists"
                  :policyInfo="policyInfo"
                />
              </div>
              <div v-if="tab.name === 'accessories'">
                <FileUpload
                  :activeName="activeName"
                  :policyBizId="route.query.policyBizId"


                />
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-card> -->
    <AppointmentEdit source='policyList' :policyDetailInfo="route.query"/>
  </div>
</template>
<script setup name="FollowUpDetail">
import AppointmentEdit from '@/views/sign/appointment/appointmentEdit'
import { onUnmounted } from 'vue'
import { getPolicyfollow, getPolicyStatus } from '@/api/sign/underwritingMain'
import useUserStore from '@/store/modules/user'
import useDictStore from '@/store/modules/dict'
import { addFna, getProcessDetail, updateProcess, getCustomerDetail } from '@/api/sign/fna'
import {
  listTenantUser,
  getInsuranceProductList,
  getAdditionalProductList,
  getAllCompanys,
  getClientUser
} from '@/api/common'
import PolicyInfo from './components/policyInfo'
import FileUpload from './components/fileUpload.vue'

import { Check } from '@element-plus/icons-vue'
import { ref } from 'vue'
const userStore = useUserStore()
const dictStore = useDictStore()
const { proxy } = getCurrentInstance()
const route = useRoute()
const router = useRouter()
const activeName = ref('status')
const processInfo = ref({
  fnaNo: '--',
  status: '未保存',
  createTime: proxy.parseTime(new Date()),
  customerName: userStore.name
}) // 流程详情信息
const updateStatus = ref(false)
const dictTypeLists = ref([])
const customerInfo = ref({})
const tabsList = ref([
  {
    label: '新单状态',
    name: 'status',
    id: 1,
    status: '0',
    key: 'customerBizId'
  },
  {
    label: '新单信息',
    name: 'info',
    id: 2,
    status: '0',
    key: 'fnaFormBizId'
  },
  {
    label: '新单附件',
    name: 'accessories',
    id: 3,
    status: '0',
    key: 'policyBizld'
  }
])
const policyInfo = ref({}) //新单信息详情
const policyStatusList = ref({}) //新单信息状态
const { csf_fna_status } = proxy.useDict('csf_fna_status')
// 获取各个流程所需要得字典数据
const getDictsData = async () => {
  // 获取租户用户列表
  const params1 = {
    tenantBizId: userStore.projectInfo.tenantBizId,
    pageNo: 1,
    pageSize: 10
  }
  const response1 = await listTenantUser(params1)
  if (response1.code == 200) {
    dictStore.setTenantUserList(response1.data.records)
  }
  const params2 = {
    loginTenantBizId: userStore.projectInfo.tenantBizId,
    pageNo: 1,
    pageSize: 10
  }

  const response2 = await getInsuranceProductList(params2)
  if (response2.code == 200) {
    response2.data.records = response2.data.records.map(item => {
      return {
        ...item,
        label: item.productName,
        value: item.productBizId
      }
    })
    dictStore.setInsureProductList(response2.data.records)
  }
  const params3 = {
    pageNo: 1,
    pageSize: 10
  }

  const response3 = await getAdditionalProductList(params3)
  if (response3.code == 200) {
    response3.data.records = response3.data.records.map(item => {
      return {
        ...item,
        label: item.productName,
        value: item.additionalProductBizId
      }
    })
    dictStore.setAdditionalProductList(response3.data.records)
  }
  const params4 = {
    pageNo: 1,
    pageSize: 10
  }

  const response4 = await getAllCompanys(params4)
  if (response4.code == 200) {
    response4.data.records = response4.data.records.map(item => {
      return {
        ...item,
        label: item.deptName,
        value: item.deptBizId
      }
    })
    dictStore.setInsureCompanyList(response4.data.records)
  }
  const params5 = {
    projectBizId: 'project_IbjfmMTYvNEBuh2S',
    tenantBizId: userStore.projectInfo.tenantBizId,
    pageNo: 1,
    pageSize: 10
  }

  const response5 = await getClientUser(params5)
  if (response5.code == 200) {
    response5.data.records = response5.data.records.map(item => {
      return {
        ...item,
        label: item.name,
        value: item.clientUserBizId
      }
    })
    dictStore.setClientUserList(response5.data.records)
  }
  // 请求每个流程中所涉及到的字典值数据
  proxy.useDictLists([
    'csf_employment',
    'sys_no_yes',
    'bx_currency_type',
    'csf_liquid_asset_type',
    'csf_premium_funding_source',
    'csf_customer_type',
    'csf_customer_title',
    'sys_gender',
    'csf_marriage',
    'csf_education',
    'csf_id_type',
    'csf_ap_apply_type',
    'csf_ap_meeting_point',
    'csf_ap_first_issue',
    'csf_ap_dividend',
    'csf_ap_frequency',
    'csf_ap_rel',
    'csf_ap_registration',
    'csf_ap_exercise',
    'csf_ap_risk',
    'csf_ap_movie',
    'csf_ap_game',
    'wj_question_first_category',
    'wj_question_second_category',
    'csf_ap_policy_transfer',
    'md_bank'
  ])
}
// 获取新单跟进详情数据
const getPolicyDetail = () => {
  getPolicyfollow(route.query.policyBizId).then(res => {
    if (res.code == 200) {
      console.log('新单跟进详情', res)
      policyInfo.value = res.data
    }
  })
  getPolicyStatus(route.query.policyBizId).then(res => {
    if (res.code == 200) {
      console.log('新单跟进状态', res)
      policyStatusList.value = res.data
    }
  })
}

// 更新流程
const processUpdate = (data, status) => {
  updateProcess(data).then(res => {
    if (res.code == 200) {
      updateStatus.value = false
      console.log('更新流程', res)

      // 获取流程详情
      getProcessInfo(data.fnaBizId, status)
    }
  })
}

// 获取流程详情
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 (processInfo.value.customerBizId) {
  //   getCustomerInfo(processInfo.value.customerBizId)
  // }
  // // 如果切换到总览，始终允许
  // 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)
}

// 获取客户详情
function getCustomerInfo(customerBizId) {
  getCustomerDetail(customerBizId).then(async res => {
    if (res.code == 200) {
      customerInfo.value = res.data
    }
  })
}
const handleSuccess = info => {
  switch (info.tab) {
    case 'customer':
      if (info.type == 'add') {
        //客户提交成功，更新流程
        processUpdate({ fnaBizId: info.fnaBizId, customerBizId: info.customerBizId }, 'fnaform')
      } else {
        processUpdate({ fnaBizId: processInfo.value.fnaBizId, customerBizId: info.customerBizId })
      }
      getCustomerInfo(info.customerBizId)
      break
    case 'fnaform':
      if (info.type == 'add') {
        //客户提交成功，更新流程
        processUpdate(
          {
            fnaBizId: processInfo.value.fnaBizId,
            customerBizId: info.customerBizId,
            fnaFormBizId: info.fnaFormBizId
          },
          'appointment'
        )
      } else {
        processUpdate({
          fnaBizId: processInfo.value.fnaBizId,
          customerBizId: info.customerBizId,
          fnaFormBizId: info.fnaFormBizId
        })
      }
      break
    case 'appointment':
      if (info.type == 'add') {
        getProcessInfo(processInfo.value.fnaBizId, 'newpolicy')
      } else {
      }
      break
    case 'newpolicy':
      break
    case 'policy':
      break
    default:
      break
  }
}
if (route.query.type == 'edit') {
  getPolicyDetail()
}
getDictsData()
</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); */
    height: calc(100vh - 215px);
    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>
