<template>
  <div>
    <basicDetails
        ref="details"
        :titleText="mainData.billno"
        formPath="QuotedPrice"
        :editData="mainData"
        :mainAreaData="mainAreaData"
        turnPageId="20221020165103"
        delApiId="20221020165503"
        idname="sa_quotedpriceid"
        ownertable="sa_quotedprice"
        :statusCheck="[{key:'status',value:'审核'},{key:'status',value:'提交'}]"
        :tags="[]"
        :tabs="['产品明细折扣','产品类别折扣','详细信息','其他费用','历史报价']"
        @pageChange="pageChange"
        @onEditSuccess="queryMainData($route.query.id)">
      <div slot="tags">
        <!-- 使用此插槽可不传[tags] -->
        <!-- :tabs="['经销商团队','代理区域','营销类别','账户余额']" -->
      </div>
      <div slot="customOperation">
        <!-- 此区域提供了自定义操作按钮 -->
        <submit_to class="inline-16" v-if="tool.checkAuth($route.name,'submit') && mainData.status === '新建'" :disabled="mainData.disabled" :sa_quotedpriceid="mainData.sa_quotedpriceid" @onSubmit="onSuccess"></submit_to>
        <revoke class="inline-16" v-if="tool.checkAuth($route.name,'revoke') && mainData.status === '提交'" :disabled="mainData.disabled" :sa_quotedpriceid="mainData.sa_quotedpriceid" @onRevoke="onSuccess"></revoke>
        <examine_to class="inline-16" v-if="tool.checkAuth($route.name,'toExamine') && mainData.status === '提交'" :disabled="mainData.disabled" :sa_quotedpriceid="mainData.sa_quotedpriceid" @onExamine="onSuccess"></examine_to>
        <deApprove class="inline-16" v-if="tool.checkAuth($route.name,'deApprove') && mainData.status === '审核'" :disabled="mainData.disabled" :data="mainData" @onDeApprove="onSuccess"></deApprove>
        <copyTo class="inline-16" v-if="tool.checkAuth($route.name,'copyTo') " :data="mainData.sa_quotedpriceid" :disabled="mainData.disabled"></copyTo>
        <toVoid class="inline-16" v-if="tool.checkAuth($route.name,'toVoid') && mainData.status === '新建'" :disabled="mainData.disabled" :data="mainData.sa_quotedpriceid" ></toVoid>
      </div>
      <div slot="slot2" class=" normal-panel">
        <detailed :data="mainData" :totalPrice="totalPrice"  v-if="flag"></detailed>
      </div>
      <div slot="slot0" class=" normal-panel">
        <productInventory ref="productList" :data="mainData" v-if="flag" :disabled="mainData.disabled" @productSuccess="productSuccess"></productInventory>
      </div>
      <div slot="slot3" class=" normal-panel">
        <otherExpenses :status="mainData.status" :data="mainData" v-if="flag"></otherExpenses>
      </div>
      <div slot="slot4" class=" normal-panel">
        <historicalQuotation :data="mainData" v-if="flag"></historicalQuotation>
      </div>
      <div slot="slot1" class=" normal-panel">
        <itemClass :data="mainData" :disabled="mainData.disabled"></itemClass>
      </div>
    </basicDetails>
  </div>
</template>

<script>
import detailed from './modules/detailed/index'
import contacts from './modules/contacts/list'
import address_manage from './modules/addressManage/list'
import submit_to from './components/submitTo'
import revoke from './components/revoke'
import examine_to from './components/examineTo'
import project_change from './modules/projectChange/index'
import customer_assocalated from './modules/customerAssociated/index'
import productInventory from './modules/productInventory/index'
import toVoid from './components/toVoid'
import historicalQuotation from './modules/historicalQuotation/index'
import copyTo from "@/SDrpManagement/QuotedPrice/detail/components/copyTo";
import deApprove from './components/deApprove'
import otherExpenses from './modules/otherExpenses/index'
import itemClass from './modules/itemClass/index'
export default {
  name: "index",
  data(){
    return {
      mainData:{
        disabled:true,
      },
      mainAreaData:[],
      disabled:false,
      flag:false,
      totalPrice:0,
      userInfo:JSON.parse(window.sessionStorage.getItem('active_account'))
    }
  },
  components:{
    detailed,
    contacts,
    address_manage,
    submit_to,
    examine_to,
    project_change,
    customer_assocalated,
    productInventory,
    toVoid,
    historicalQuotation,
    copyTo,
    deApprove,
    revoke,
    otherExpenses,
    itemClass
  },
  watch:{
   async mainData(val) {
     const res = await this.$api.requested({
       "id": 20220930103501,
       "content": {
         "ownertable":"sa_quotedprice",
         "ownerid":this.$route.query.id
       }
     })
      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){
      this.$refs.details.$refs.group.queryGroup()
      const res = await this.$api.requested({
        "id": 20221020165203,
        "version":1,
        "content": {
          nocache:true,
          "sa_quotedpriceid":id
        }
      })
      /*this.mainData = res.data*/
      this.mainData = Object.assign({},this.mainData,res.data)
      this.productList()
      /*this.$refs.productList.productData()*/
      this.$nextTick(()=>{
        this.$refs.productList.productData()
      })


     /* this.changeDataStructure()*/
     /* this.$refs.detailed.detailedList(this.mainData)*/
      this.flag = true
    },
    // 监听切换数据,上一页,下一页
    pageChange (id,rowindex) {
      this.flag = false
      this.$router.replace({path:'/quotedPriceDetail',query:{id:id,rowindex:rowindex}})
      this.queryMainData(id)
    },
    changeDataStructure(){
      let that = this
      this.mainData.quotedpricetype === '项目报价'?
      this.mainAreaData = [
        {
          label:'报价类型',
          value:this.mainData.quotedpricetype
        },
        {
          label:'项目名称',
          value:this.mainData.projectname
        },
        {
          label:'项目编号',
          value:this.mainData.projectnum
        },

        {
          label:'报价日期',
          value:this.mainData.billdate
        },
        {
          label:'有效期',
          value:this.mainData.begdate?this.mainData.begdate + '至' + this.mainData.enddate:"--"
        },
        {
          label:'客户名称',
          value:this.mainData.enterprisename
        },
        {
          label:'联系人',
          value:this.mainData.contactsname
        },
        {
          label:'手机号',
          value:this.mainData.contactsphonenumber
        },
        {
          label:'折扣(%)',
          value:this.mainData.discountrate !== 0?Math.round((this.mainData.discountrate * 100)*100)/100:'--'
        },
        {
          label:'总金额(元)',
          value:this.totalPrice ? this.tool.formatAmount(this.totalPrice,2) : '--'
        },

        {
          label:'是否特价',
          value:this.mainData.specialoffer === 1?'是':'否'
        },
        {
          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:'#333333'}
                break;
              default:
                break;
            }
            return style
            
          }
        },

        /*{
          label:'邮箱',
          value:this.mainData.email
        }*/
      ]:this.mainAreaData = [
            {
              label:'报价类型',
              value:this.mainData.quotedpricetype
            },
            {
              label:'报价日期',
              value:this.mainData.billdate
            },
            {
              label:'有效期',
              value:this.mainData.begdate?this.mainData.begdate + '至' + this.mainData.enddate:"--"
            },
            {
              label:'客户名称',
              value:this.mainData.enterprisename
            },
            {
              label:'联系人',
              value:this.mainData.contactsname
            },
            {
              label:'手机号',
              value:this.mainData.contactsphonenumber
            },
            {
              label:'折扣(%)',
              value:this.mainData.discountrate !== 0?Math.round((this.mainData.discountrate * 100)*100)/100:'--'
            },
            {
              label:'总金额(元)',
              value:this.totalPrice ? this.tool.formatAmount(this.totalPrice,2) : '--'
            },

            {
              label:'是否特价',
              value:this.mainData.specialoffer === 1?'是':'否'
            },
            {
              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:'#333333'}
                    break;
                  default:
                    break;
                }
                return style

              }
            },

            /*{
              label:'邮箱',
              value:this.mainData.email
            }*/
          ]
    },
    /*产品配置信息*/
    async productList(){
      const res = await this.$api.requested({
        "id": 20221021095503,
        "content": {
          "sa_quotedpriceid":this.$route.query.id     //sat_notice_classid<=0时 为新增
        }
      })
      this.totalPrice = 0
      this.mainData.specialoffer = 0
      res.data.forEach((item)=> {
        if (item.discountDifferenceAmount < 0){
          this.mainData.specialoffer = 1
        }
        this.totalPrice = this.totalPrice + item.amount
      })
      /*this.queryMainData(this.$route.query.id)*/
      this.totalPrice = Math.round(this.totalPrice * 100) / 100
      this.changeDataStructure()
    },
    onSuccess(){
      this.queryMainData(this.$route.query.id)

    },
    productSuccess(){
      /*this.queryMainData(this.$route.query.id)*/
      this.productList()
    }

  },
  mounted() {
    this.queryMainData(this.$route.query.id)
  }
}
</script>

<style scoped>

</style>