<template>
  <div>
    <basicDetails
        v-if="mainData"
        ref="details"
        :titleText="mainData.enterprisename_customer"
        :editData="mainData"
        :mainAreaData="mainAreaData"
        turnPageId="20221101094502"
        idname="sat_orderclueid"
        tags=""
        :tabs="['跟进记录','详细信息']"
        :justsaler="1"
        @pageChange="pageChange"
        @onEditSuccess="queryMainData($route.query.id)"
        @detailCreate="detailCreate">
      <div slot="customOperation">
        <edit class="inline-16" v-if="Object.keys(mainData).length > 0 && tool.checkAuth($route.name,'update')" :disabled="!isHandle" :data="mainData" @onSuccess="queryMainData"/>
        <follow :detailInfo="mainData" 
          v-if="tool.checkAuth($route.name,'follow')"
          @updataList="queryMainData();$refs.followDetail.getFollowDetail()" @onSuccess="onFollow" :disabled="!isHandle" :data="mainData"></follow>
        <!-- <move :data="mainData" 
          @updataList="queryMainData" 
          :teamList="teamList()"  
          v-if="userInfo.isAuth == 1 ? mainData.status == '待跟进' || mainData.status == '跟进中' : ''"></move> -->
        <changeProject v-if="mainData.isproject == 0 && tool.checkAuth($route.name,'changeProject')" :data="mainData" :disabled="!isHandle" class="inline-16" @onChangeProject="queryMainData($route.query.id)"/>
        <changeCustomer v-if="mainData.iscustomer == 0 && tool.checkAuth($route.name,'changeCutomer')" :data="mainData" :disabled="!isHandle" class="inline-16" @onChangeCustomer="onChangeCustomer"></changeCustomer>
        <el-button :type="!isHandle || mainData.status === '已无效'?'':'primary'" v-if="tool.checkAuth($route.name,'goBack') && mainData.status != '已转化' && mainData.isprivate !== 1" :disabled="!isHandle || mainData.status === '已无效'" class="inline-16" size="mini" @click="goBack">退 回</el-button>
        <noNull v-if="tool.checkAuth($route.name,'noNull') && mainData.status != '已转化' " class="inline-16" :data="mainData" :disabled="!isHandle" @onSuccess="$store.dispatch('changeDetailDrawer',false);$router.replace('/orderclue')" :id="mainData.sat_orderclueid"/>
        <Del v-if="tool.checkAuth($route.name,'delete') && mainData.status != '已转化'" class="inline-16" :data="mainData" :disabled="!isHandle" :idName="20221123193702" @onSuccess="$store.dispatch('changeDetailDrawer',false);$router.replace('/orderclue')" :id="mainData.sat_orderclueid" idKey="sat_orderclueids"/>
      </div>
      <div slot="slot1" class="container normal-panel">
        <BaseInfo :detailInfo="detailInfo"/>
      </div>
      <div slot="slot0">
        <followDetail ref="followDetail"/>
      </div>
    </basicDetails>
  </div>
</template>

<script>
import BaseInfo from '@/HDrpManagement/projectChange/modules/modules/baseInfo/baseInfo'
import followDetail from '@/SManagement/orderclue_detail/components/followDetail'
import follow from '@/SManagement/orderclue/components/follow'
import move from '@/SManagement/orderclue/components/move'
import edit from '@/SManagement/orderclue/components/edit'
import Del from './components/delete'
import noNull from './components/noNull'
import changeProject from './components/changeProject'
import changeCustomer from './components/changeCustomer'
export default {
  name: "detail",
  inject:['teamList'],
  data() {
    return {
      mainData:{},
      mainAreaData:{},
      detailInfo:{},
      userInfo:JSON.parse(window.sessionStorage.getItem('userInfo')),
      isHandle:''
    }
  },
  components:{
    BaseInfo,
    followDetail,
    follow,
    move,
    Del,
    noNull,
    changeProject,
    changeCustomer,
    edit
  },
  watch: {
   mainData(val) {
      let is 
      if (Object.keys(JSON.parse(window.sessionStorage.getItem('userInfo'))).length > 0) {
        console.log('负责人逻辑');
        
        if(this.mainData.leader.length <= 0 ) return
        if(JSON.parse(window.sessionStorage.getItem('userInfo')).userid == this.mainData.leader[0].userid) {
          is = true
        } else {
          is = false
        }
      } else {
        console.log('不是负责人逻辑');
        is = true
      }
      this.isHandle = is
      console.log(this.isHandle,'权限');
      
   }
  },
  methods:{
    detailCreate (param) {
      param.content.isAll = 0
    },
    async queryMainData(id) {
      const res = await this.$api.requested({
        "classname": "saletool.orderclue.web.orderclue",
        "method": "selectDetail",
        "content": {
            "sat_orderclueid": this.$route.query.id
        }
      })
      this.mainData = res.data
      this.$refs.details.param.content.type = this.$route.query.tabIndex
      console.log(this.mainData,"销售线索手机号数据");
      
      this.changeDataStructure()
    },
    changeDataStructure() {
      var that = this
      this.mainAreaData = [
        {
          label:'客户名称',
          value:this.mainData.enterprisename_customer
        },
        {
          label:'联系人',
          value:this.mainData.name
        },
        {
          label:'手机号',
          value:this.mainData.phonenumber
        },
        {
          label:'来源',
          value:this.mainData.cluesource
        },
        {
          label:'市场活动',
          value:this.mainData.campaign_name
        },
        {
          label:'负责人',
          value:this.mainData.leader.length > 0 && this.mainData.leader[0].name
        },
        {
          label:'分配状态',
          value:this.mainData.allocationstatus
        },
        {
          label:'跟进状态',
          value:this.mainData.status,
          style:function () {
            let style = {}
            switch (that.mainData.status) {
              case '已过期':
                style = {color:'#333'}
                break;
              case '跟进中':
                style = {color:'#52C41A'}
                break;
              case '待跟进':
                style = {color:'#3874f6'}
                break;
              case '已无效':
                style = {color:'#333333'}
                break; 
              case '已转化':
                style = {color:'#fa8c16'}
                break; 
              default:
                break;
            }
            return style
            
          }
        },
        {
          label:'跟进次数',
          value:this.mainData.followcount
        },
      ]
      this.detailInfo = {
        baseInfo: [
          {
            label:'客户',
            value:this.mainData.enterprisename_customer
          },
          {
            label:'联系人',
            value:this.mainData.name
          },
          {
            label:'手机号',
            value:this.mainData.phonenumber
          },
          {
            label:'省市县',
            value:`${this.mainData.province}-${this.mainData.city}-${this.mainData.county}`
          },
          {
            label:'地址',
            value:this.mainData.address
          },
          {
            label:'项目名称',
            value:this.mainData.projectname
          },
          {
            label:'项目规模',
            value:this.mainData.scale
          },
          {
            label:'总投资额(百万)',
            value:this.mainData.totalinvestment
          },
          {
            label:'造价(百万)',
            value:this.mainData.costofconstruction
          },
          {
            label:'预计开工时间',
            value:this.mainData.begdate_due
          },
          {
            label:'预计完工时间',
            value:this.mainData.enddate_due
          },
          {
            label:'市场活动',
            value:this.mainData.campaign_name
          },
          {
            label:'来源',
            value:this.mainData.cluesource
          },
          {
            label:'线索概况',
            value:this.mainData.notes
          },
          {
            label:'所属业务员',
            value:this.mainData.salename
          },
          {
            label:'负责人',
            value:this.mainData.leader.length > 0 && this.mainData.leader[0].name
          },
          {
            label:'分配状态',
            value:this.mainData.allocationstatus,
            style:function () {
              let style = {}
              switch (that.mainData.status) {
                case '已分配':
                  style = {color:'#333333'}
                  break;
                case '待分配':
                  style = {color:'#3874f6'}
                  break;
                default:
                  break;
              }
              return style

            }
          },
          // {
          //   label:'负责人',
          //   value:this.mainData.leader[0].name
          // },
          
          {
            label:'跟进状态',
            value:this.mainData.status,
            style:function () {
              let style = {}
              switch (that.mainData.status) {
                case '已过期':
                  style = {color:'#333'}
                  break;
                case '跟进中':
                  style = {color:'#52C41A'}
                  break;
                case '待跟进':
                  style = {color:'#3874f6'}
                  break;
                case '已无效':
                  style = {color:'#333333'}
                  break;
                case '已转化':
                  style = {color:'#fa8c16'}
                  break;
                default:
                  break;
              }
              return style

            }
          },

        ],
        systemInfo: [
          {label:'创建人',value:this.mainData.createBy},
          {label:'创建时间',value:this.mainData.createDate},
          {label:'分配人',value:this.mainData.assignedBy},
          {label:'分配时间',value:this.mainData.assignedDate},
          {label:'最近编辑人',value:this.mainData.editBy},
          {label:'最近编辑时间',value:this.mainData.editDate},
          {label:'最近跟进人',value:this.mainData.followBy},
          {label:'最近跟进时间',value:this.mainData.followDate},
          {label:'无效原因',value:this.mainData.invalidreason},
          {label:'作废原因',value:this.mainData.deletereason},
          {label:'跟进次数',value:this.mainData.followcount === 0?'0':this.mainData.followcount},
          /*{label:'转化次数',value:this.mainData.isproject === 0 && this.mainData.iscustomer === 0?'0':this.mainData.isproject + this.mainData.iscustomer },*/
          {label:'转手次数', value:this.mainData.leader[0].leadernum === 0?'0':this.mainData.leader[0].leadernum},

        ]
      }
    },
    goBack () {
      this.$confirm('退回该线索后无法撤销,是否继续','提示',{
        "confirmButtonText":'确定',
        "cancelButtonText":'取消'
      }).then(async confirm => {
        let res = await this.$api.requested({
          "id":20221207160802,
          "content": {
            sat_orderclueid: this.mainData.sat_orderclueid
          }
        })
        this.tool.showMessage(res , () => {
          this.$store.dispatch('changeDetailDrawer',false)
          this.$router.push('/orderclue')
        })
      })
    },
    // 监听切换数据,上一页,下一页
    pageChange (id,rowindex,tabIndex) {
      this.flag = false
      tabIndex = this.$route.query.tabIndex
      this.$router.replace({path:'/orderclue_detail',query:{id:id,rowindex:rowindex,tabIndex:tabIndex}})
      this.queryMainData(id)
    },
    onChangeCustomer(){
      this.queryMainData(this.$route.query.id)
    },
    onFollow(){
      this.$refs.followDetail.getFollowDetail()
      this.queryMainData()
    }
  },
  mounted () {
    this.queryMainData(this.$route.query.id)
  },
  created() {
  }
}
</script>

<style scoped>

</style>