<template>
  <div>
    <basicDetails
        ref="details"
        :titleText="mainData.enterprisename"
        formPath="salerPrivatecustomer"
        :editData="mainData"
        :mainAreaData="mainAreaData"
        turnPageId="20221012164402"
        idname="sa_customersid"
        tags=""
        :tabs="['详细信息','联系人信息','客户地址','开票信息','项目商机','报价单','关联合同','账户余额','银行卡信息','线索']"
        @pageChange="pageChange"
        @onEditSuccess="onEditSuccess">
      <div slot="tags">
        <!-- 使用此插槽可不传[tags] -->
        <!-- :tabs="['经销商团队','代理区域','营销类别','账户余额']" -->
<!--        <tagPanl slot="tags" :data="tags" :id="mainData.sa_customersid" @onSuccess="queryMainData"/>-->
      </div>
      <div slot="customOperation" >
        <!-- 此区域提供了自定义操作按钮 -->
        <statusChange class="inline-16" v-if="tool.checkAuth($route.name,'statusChange')" :disabled="mainData.disabled" :id="mainData.sa_customersid"  @statusChangeSuccess="statusChangeSuccess"></statusChange>
        <share class="inline-16" v-if="tool.checkAuth($route.name,'share') && mainData.sourcepoolname === ''" :disabled="mainData.disabled" :id="mainData.sa_customersid" @onshow="onSuccess" @shareSuccess="onSuccess"></share>
        <toBack class="inline-16" v-if="tool.checkAuth($route.name,'back') && mainData.sa_customerpoolid_source !== 0" :disabled="mainData.disabled" :data="mainData" @backSuccess="onSuccess"></toBack>
        <to_void class="inline-16" v-if="tool.checkAuth($route.name,'toVoid')" :id="mainData.sa_customersid" :disabled="mainData.disabled" @onshow="onSuccess" @toVoidSuccess="onSuccess"></to_void>
        <recovery class="inline-16" v-if="tool.checkAuth($route.name,'recovery') " :data="mainData" @onshow="onSuccess" @backSuccess="onSuccess"></recovery>
      </div>
      <div slot="slot0" >
        <detailed :data="mainData" ref="baseDetail"></detailed>
      </div>
      <div slot="slot1" class=" normal-panel"  >
        <contacts :data="mainData"   @onSuccess="onSuccess" v-if="flag"></contacts>
      </div>
      <div slot="slot2" class=" normal-panel">
        <address_manage :data="mainData"  v-if="flag"></address_manage>
      </div>
      <div slot="slot3" class=" normal-panel">
        <financial :mainData="mainData"  v-if="flag"></financial>
      </div>
      <div slot="slot4" class="normal-panel">
        <projectChange :data="mainData"  v-if="flag"></projectChange>
      </div>
      <div slot="slot5" class="normal-panel">
        <quotedPrice :data="mainData"  v-if="flag"></quotedPrice>
      </div>
      <div slot="slot6" class="normal-panel">
        <contract :data="mainData"  v-if="flag"></contract>
      </div>
      <div slot="slot7" class="normal-panel">
        <accountMoney :data="mainData"  v-if="flag"></accountMoney>
      </div>
      <div slot="slot8" class="normal-panel">
        <bankCard :data="mainData" v-if="flag"></bankCard>
      </div>
      <div slot="slot9" class="normal-panel">
        <followLeads :data="mainData"  v-if="flag"></followLeads>
      </div>
    </basicDetails>
  </div>
</template>

<script>
import detailed from './modules/detailedData'
import contacts from './modules/contacts/list'
import address_manage from './modules/addressManage/list'
import transactionUser from "./modules/operation/transactionUser"
import lock from './modules/operation/lock'
import unlock from './modules/operation/unlock'
import share from './modules/operation/share'
import send_back from './modules/operation/sendBack'
import to_void from './modules/operation/toVoid'
import transaction_status from './modules/operation/transactionStatus'
import statusChange from './modules/operation/statusChange'
import recovery from './modules/operation/recovery'
import financial from './modules/financialInfo/index.vue'
import projectChange from './modules/projectChange/list'
import quotedPrice from './modules/quotedPrice/list'
import followLeads from './modules/followLeads/index'
import toBack from "@/SDrpManagement/salerPrivatecustomer/detail/modules/operation/toBack";
import contract from './modules/contract/index'
import accountMoney from './modules/accountMoney/index'
import bankCard from './modules/bankCard/index'
export default {
  name: "detail",
  data() {
    return {
      mainData:{
        disabled:false
      },
      mainAreaData:[],
      userInfo:JSON.parse(window.sessionStorage.getItem('active_account')),
      tags:{},
      visible:false,
      dialogFormVisible:false,
      dialogFormLeader:false,
      form:{
        status:'',
        userid:''
      },
      users:[],
      flag:false,
      disabled:false
    }
  },
  components:{
    detailed,
    contacts,
    address_manage,
    transactionUser,
    lock,
    unlock,
    share,
    send_back,
    to_void,
    transaction_status,
    recovery,
    financial,
    projectChange,
    quotedPrice,
    followLeads,
    statusChange,
    toBack,
    contract,
    accountMoney,
    bankCard
  },
  watch:{
   async mainData(val) {
      const res = await this.$api.requested({
        "id": 20220930103501,
        "content": {
          "ownertable":"sa_customers",
          "ownerid":this.$route.query.id
        }
      })
     console.log(res.data[0].team,'数据团队')
      if (this.mainData.leader.length !== 0){
        if (JSON.parse(window.sessionStorage.getItem('active_account')).userid === this.mainData.leader[0].userid){
          /*是负责人*/
          this.mainData.disabled = true
        }else {
          let flag = 0
          for (var i=0;i<res.data[0].team.length;i++){
            if (res.data[0].team[i].userid === JSON.parse(window.sessionStorage.getItem('active_account')).userid){
              if (res.data[0].team[i].editable === 1){
                this.mainData.disabled = true
                flag =1
                break
              }else {
                flag = 0
                break
              }
            }
          }
          /*不是负责人*/
          flag === 0?this.mainData.disabled = false:this.mainData.disabled = true
        }
      }
      console.log("权限结果", this.mainData.disabled)
    }
  },
  methods:{
    async queryMainData(id) {
      const res = await this.$api.requested({
        "id": 20221012164302,
        "content": {
          "sa_customersid": id
        },
      })
     /* this.mainData = res.data*/
      this.mainData = Object.assign({},this.mainData,res.data)
      this.$refs['details'].param.content.type = this.$route.query.tabIndex
      this.$refs['details'].param.content.isExport = false
      this.changeDataStructure()
      this.flag = true
      console.log(this.mainData,"输出数据用户id")
    },
    show(){
      this.visible = false
    },
    changeDataStructure() {
      let that = this
      this.mainAreaData = [
        {
          label:'客户编号',
          value:this.mainData.sa_customersid
        },
        {
          label:'erp编号',
          value:this.mainData.erpagentnum
        },
        {
          label:'客户名称',
          value:this.mainData.enterprisename
        },
        {
          label:'客户类型',
          value:this.mainData.type
        },
        {
          label:'客户等级',
          value:this.mainData.grade === 0?'':this.mainData.grade
        },
        {
          label:'客户分类',
          value:this.mainData.customergrade
        },
        {
          label:'成交状态',
          value:this.mainData.tradingstatus,
          style:function () {
            let style = {}
            switch (that.mainData.tradingstatus) {
              case '已成交':
                style = {color:'#fa8c16'}
                break;
              case '未成交':
                style = {color:'#3874f6'}
                break;
              case '多次成交':
                style = {color:'#333333'}
                break;
              default:
                break;
            }
            return style
            
          }
        },
        {
          label:'合作状态',
          value:this.mainData.status,
          style:function () {
            let style = {}
            switch (that.mainData.status) {
              case '潜在':
                style = {color:'#3874f6'}
                break;
              case '合作中':
                style = {color:'#52c41a'}
                break;
              case '已终止':
                style = {color:'#999999'}
                break;
              default:
                break;
            }
            return style
            
          }
        },
        {
          label:'来源公海',
          value:this.mainData.sourcepoolname
        },
        {
          label:'负责人',
          value:this.mainData.leader.length === 0?"--":this.mainData.leader[0].name
        },
        {
          label:'归属经销商',
          value:this.mainData.agentname
        },
      ]
    },
    // 监听切换数据,上一页,下一页
    pageChange (id,rowindex,tabIndex) {
      this.flag = false
      tabIndex = this.$route.query.tabIndex
      this.$router.replace({path:'/customerDetail',query:{id:id,rowindex:rowindex,tabIndex:tabIndex}})
      this.queryMainData(id)
    },
    onSuccess(){
      this.visible = false
      this.queryMainData(this.$route.query.id)
      /*this.$emit('onSuccess')*/
    },
    statusChangeSuccess () {
      this.$refs.details.refreshTag()
      this.$refs.baseDetail.basicData(this.$route.query.id)
      this.$refs.baseDetail.systemData(this.$route.query.id)
      this.queryMainData(this.$route.query.id)
    },
    onEditSuccess(){
      this.statusChangeSuccess()
    }
  },
  mounted () {
    this.queryMainData(this.$route.query.id)
  },
  created() {

  }
}
</script>

<style scoped>

</style>