<template>
  <div>
    <basicDetails
        ref="details"
        :titleText="mainData.enterprisename"
        formPath="publicCustomer"
        :editData="mainData"
        :mainAreaData="mainAreaData"
        :turnPageId="20221011133602"
        idname="sa_customersid"
        :tabs="['详细信息','联系人信息','客户地址','开票信息','项目商机','报价单','关联合同','账户余额','银行卡信息','线索']"
        @pageChange="pageChange"
        @onEditSuccess="onEditSuccess">
      <div slot="tags">
        <tagPanl slot="tags" :data="tagObj" :id="mainData.sa_customersid" @onSuccess="queryMainData"/>
        <!-- 使用此插槽可不传[tags] -->
        <!-- :tabs="['经销商团队','代理区域','营销类别','账户余额']" -->
      </div>
      <div slot="customOperation">
        <!-- 此区域提供了自定义操作按钮 -->
        <div class="inline-16" v-if="tool.checkAuth($route.name,'receive')  && length === 0 && mainData.freeget === 1">
          <receive  :id="mainData.sa_customersid" @receiveSuccess="receiveSuccess"></receive>
        </div>
        <div class="inline-16" v-if="tool.checkAuth($route.name,'distribution') && length === 0">
          <distribution  :data="mainData"  @setSuccess="onSuccess"></distribution>
        </div>
<!--        <div class="inline-16" v-if="tool.checkAuth($route.name,'toVoid')">
          <to_void  :id="mainData.sa_customersid"  @toVoidSuccess="onSuccess"></to_void>
        </div>-->
        <el-button  type="primary" size="mini" v-if="tool.checkAuth($route.name,'deletePublic')" @click="deleteData" class="inline-16">删 除</el-button>
      </div>
      <div slot="slot0" >
        <detailed :data="mainData" :length="length" ref="detailed"></detailed>
      </div>
      <div slot="slot1">
        <contacts :data="mainData"></contacts>
      </div>
      <div slot="slot2">
        <address_manage :data="mainData"></address_manage>
      </div>
      <div slot="slot3" class=" normal-panel">
        <financial :mainData="mainData"  ></financial>
      </div>
      <div slot="slot4" class="normal-panel">
        <projectChange :data="mainData" ></projectChange>
      </div>
      <div slot="slot5" class="normal-panel">
        <quotedPrice :data="mainData"  ></quotedPrice>
      </div>
      <div slot="slot6" class="normal-panel">
        <contract :data="mainData" ></contract>
      </div>
      <div slot="slot7" class="normal-panel">
        <accountMoney :data="mainData"  ></accountMoney>
      </div>
      <div slot="slot8" class="normal-panel">
        <bankCard :data="mainData" ></bankCard>
      </div>
      <div slot="slot9" class="normal-panel">
        <followLeads :data="mainData"  ></followLeads>
      </div>
<!--      <div slot="slot3" >
        <bankCard :data="mainData" ></bankCard>
      </div>-->
      <!-- <div slot="slot0" class="container normal-panel">
        <el-button size="small" type="primary" @click="createAgentAccount" class="normal-margin">创建经销商主账号</el-button>
        <tableLayout :layout="tablecols" :data="mainData.hrs" :custom="false" height="300px"></tableLayout>
      </div>
      <div slot="slot1" class="container normal-panel">
        <salescope :data="mainData"></salescope>
      </div>
      <div slot="slot2" class="container normal-panel">
        <SaleClass :data="mainData"></SaleClass>
      </div>
      <div slot="slot3" class="container normal-panel">
        <accountMoney :mainData="mainData"></accountMoney>
      </div> -->
    </basicDetails>
  </div>
</template>

<script>
import detailed from './detail/detailedData'
import contacts from './detail/contacts/list'
import address_manage from './detail/addressManage/list'
import receive from './detail/operation/receive'
import distribution from './detail/operation/distribution'
import to_void from './detail/operation/toVoid'
import bankCard from './detail/bankCard/index'
import financial from '@/SDrpManagement/salerPrivatecustomer/detail/modules/financialInfo/index.vue'
import projectChange from '@/SDrpManagement/salerPrivatecustomer/detail/modules/projectChange/list'
import quotedPrice from '@/SDrpManagement/salerPrivatecustomer/detail/modules/quotedPrice/list'
import contract from '@/SDrpManagement/salerPrivatecustomer/detail/modules/contract/index'
import accountMoney from '@/SDrpManagement/salerPrivatecustomer/detail/modules/accountMoney/index'
import followLeads from '@/SDrpManagement/salerPrivatecustomer/detail/modules/followLeads/index'
export default {
  data () {
    return {
      mainData:{
        disabled:true
      },
      mainAreaData:[],
      customerType:'',
      customerLead:'',
      arealist:[],
      publicList:[],
      tagObj:{},
      visible:false,
      flag:false,
      length:''
    }
  },
  provide () {
    return {
      customerType:() => this.customerType,
      customerLead:() => this.customerLead,
      arealist:() => this.arealist,
      publicList:() => this.publicList
    }
  },
  components:{
    detailed,contacts,address_manage,receive,distribution,to_void,bankCard,
    financial,projectChange,quotedPrice,contract,accountMoney,followLeads
  },
  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 getSysOption() {
      /* 获取客户类型 */
      let res = await this.$store.dispatch('optiontypeselect','customertypemx')
      this.customerType = res.data
      /* 获取客户等级 */
      let res2 = await this.$store.dispatch('optiontypeselect','customergrade')
      this.customerLead = res2.data
    },
    /* 获取省市县 */
    async query_arealist() {
      const res = await this.$api.requested({
        "classname": "system.tools",
        "method": "query_arealist",
        "content": {
        }
      })
      this.arealist = this.tool.createMenu(res.data)
    },
    /* 获取公海数据 */
    async getPublicList() {
      const res = await this.$api.requested({
      "id": 20221009100702,
      "content": {
          "pageNumber": 1,
          "pageSize": 999999,
          "where": {
          }
      },
    })
      this.publicList = res.data
    },
    /* 获取数据标签 */
    async getDataTag (id) {
      let res = await this.$api.requested({
        "id": 20220929085401,
        "content": {
            "ownertable":"sa_customers",
            "ownerid":id
        }
      })
      this.tagObj = res.data
      
    },
    async queryMainData (id) {
     
      const res = await this.$api.requested({
        "id": "20221012164302",
        "content": {
          "sa_customersid":this.$route.query.id
        }
      })
       this.$refs.details.param.content.where.sa_customerpoolid = this.$route.query.tabIndex
      this.$refs.details.param.content.isExport = false
      this.mainData = Object.assign({},this.mainData,res.data)
      this.length = this.mainData.leader.length
      console.log(this.length,"长度")
      this.getDataTag(this.mainData.sa_customersid)
      this.changeDataStructure()
    },
    // 监听切换数据,上一页,下一页
    pageChange (id,rowindex,tabIndex) {
      tabIndex = this.$route.query.tabIndex
      this.$router.replace({path:'/PublicCustomerDetail',query:{id:id,rowindex:rowindex,tabIndex:tabIndex}})
      this.queryMainData(id)
    },
    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['leader'].length !== 0 ? '已分配' : '待分配',
          style:function () {
            let style = {}
            style = that.mainData['leader'].length !== 0  ? {color:'#333333'} : {color:'#3874f6'}
            return style
          }
        },
        {
          label:'所属公海',
          value:this.mainData.poolname
        },
        {
          label:'负责人',
          value:this.mainData.leader.length === 0?"--":this.mainData.leader[0].name
        },
      ]
    },
    onSuccess(){
      this.visible = false
      /*this.queryMainData(this.$route.query.id)*/
      this.$store.dispatch('changeDetailDrawer',false)
      this.$emit('onSuccess')
    },
    receiveSuccess(){
      this.$store.dispatch('changeDetailDrawer',false)
    },
    /*删除客户*/
    deleteData(){
      this.$confirm('是否确认彻底删除数据，一旦删除，无法恢复?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal:false,
        type: 'warning'
      }).then(async() => {
        const res = await this.$api.requested({
          "id": 20230103131204,
          "content": {
            "sys_object": 2,//1-线索,2-客户,3-合同,4-项目商机,5-报价单
            "ownerids": [this.mainData.sa_customersid]
          }
        })
        this.tool.showMessage(res,()=>{
          this.$store.dispatch('changeDetailDrawer',false)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    onEditSuccess(){
      this.$refs.detailed.basicData(this.$route.query.id)
      this.$refs.detailed.systemData(this.$route.query.id)
      this.queryMainData(this.$route.query.id)
    }
  },
  mounted () {
  },
  created () {
    // 验证权限
    this.queryMainData(this.$route.query.id)
    this.getSysOption()
    this.query_arealist()
    this.getPublicList()
  },
}

</script>
<style>
</style>