<template>
  <div>
    <basicDetails
        ref="details"
        :titleText="mainData.promnum"
        :editData="mainData"
        :mainAreaData="mainAreaData"
        :turnPageId="20221230144803"
        :delApiId="20221230144903"
        idname="sa_promotionid"
        ownertable="sa_promotion"
        tags=""
        :tabs="['促销模块','授权范围','详细信息']"
        :statusCheck="[{key:'status',value:'审核'},{key:'status',value:'发布'}]"
        @pageChange="pageChange"
        @onEditSuccess="queryMainData($route.query.id)">
      <div slot="customOperation">
        <Edit class="inline-16" :data="mainData" v-if="tool.checkAuth($route.name,'update') && mainData.status == '新建'" @onSuccess="queryMainData"></Edit>
        <Check 
          :data="mainData" 
          class="inline-16" 
          @onSuccess="queryMainData" 
          v-if="mainData.status != '发布'"
        />
        <Send :data="mainData" class="inline-16" @onSuccess="queryMainData" v-if="mainData.status != '关闭' || mainData.status != '新建'"/>
        <Close :data="mainData" class="inline-16" @onSuccess="queryMainData"/>
      </div>
      <div slot="slot0" >
        <promotionModel v-if="mainData" :data="mainData" ref="model">
          <addModel v-if="tool.checkAuth($route.name,'promotionModel')" class="inline-16" @onSuccess="$refs.model.listData()" slot="addModel"/>
          <calc-price v-if="tool.checkAuth($route.name,'promotionModel')" slot="calcPrice" @onSuccess="$refs.model.listData()"></calc-price>
          <template v-slot:editModel="scope">
              <el-button :disabled="!isEdit" v-if="tool.checkAuth($route.name,'promotionModel')" class="inline-16" size="mini" type="text" @click="$refs.model.currentModel=scope.data">编 辑</el-button>
          </template>
          <template v-slot:product="scope">
                <productDetail v-if="tool.checkAuth($route.name,'promotionModel')" :data="scope.data" ref="productDetail">
                  <template v-slot:promotionPrice="scope">
                      <promotion-price :data="scope.data" ref="promotionPrice">
                        <!-- <template v-slot:editPrice="scope2">
                            <el-button type="text" size="mini" @click="$refs.promotionPrice.currentProduct = scope2.data">编 辑</el-button>
                        </template> -->
                      </promotion-price>
                  </template>
                  <template v-slot:addProduct="scope">  
                    <add-product :data="scope.data" @onSuccess="$refs.productDetail.listData(scope.data.sa_promotion_itemgroupid)"></add-product>
                  </template>
                  <template v-slot:delProduct="scope">
                    <el-button :disabled="!isEdit" type="text" size="mini" @click="productDel(scope.data)">删 除</el-button>
                  </template>
                </productDetail>
          </template>
          <template v-slot:delModel="scope">
              <delete-btn 
                :disabled="!isEdit"
                v-if="tool.checkAuth($route.name,'promotionModel')"
                message="确定删除当前促销模块吗?"
                class="inline-16"
                :id="scope.data.sa_promotion_itemgroupid" 
                nameId="20230102112403" 
                nameKey="sa_promotion_itemgroupids" 
                @deleteSuccess="$refs.model.listData()"
              ></delete-btn>
          </template>
        </promotionModel>
      </div>
      <div slot="slot1">
        <prowerArea ref="prowerArea">
          <template v-slot:editArea="scope">
              <el-button v-if="tool.checkAuth($route.name,'prowerArea')" class="inline-16" size="mini" type="text" @click="$refs.prowerArea.currentArea=scope.data">编 辑</el-button>
          </template>
          <template v-slot:delArea="scope">
            <delete-btn
               v-if="tool.checkAuth($route.name,'prowerArea')"
              message="确定删除当前授权吗?"
              class="inline-16"
              :id="scope.data.sa_promotion_authid" 
              nameId="20230102103003" 
              nameKey="sa_promotion_authids"
              @deleteSuccess="$refs.prowerArea.listData()"></delete-btn>
          </template>
          <add-prower-area v-if="tool.checkAuth($route.name,'prowerArea')" @onSuccess="$refs.prowerArea.listData()" slot="addProwerArea"></add-prower-area>
        </prowerArea>
      </div>
      <div slot="slot2" >
        <base-info v-if="detailInfo" :data="detailInfo"></base-info>
      </div>
    </basicDetails>
  </div>
</template>

<script>
import Edit from '@/Form/promotion/edit'

import BaseInfo from '@/components/detailInfo/index'
import Check from '../components/check'
import Send from '../components/send'
import Close from '../components/close'
import promotionModel from './productModel/index'
import addModel from './productModel/add'
import productDetail from './productModel/productDetail'
import promotionPrice from './productModel/promotionPrice'
import calcPrice from './productModel/calcPrice'
import addProduct from './productModel/addProduct'

import prowerArea from './prowerArea/index'
import addProwerArea from './prowerArea/addPowerArea'
export default {
  name: "index",
  components:{Edit,BaseInfo,Check,Send,Close,promotionModel,addModel,productDetail,promotionPrice,calcPrice,addProduct,prowerArea,addProwerArea},
  data(){
    return {
      mainData:'',
      mainAreaData:[],
      detailInfo:'',
      isEdit:false
    }
  },
  provide () {
    return {
      isEdit: () => this.isEdit
    }
  },
  methods:{
    async queryMainData(id){
      const res = await this.$api.requested({
        "id": "20221230144703",
        "version":1,
        "content": {
          "sa_promotionid":this.$route.query.id
        }
      })
      this.mainData = res.data
      console.log(this.mainData);
      this.isEdit = this.mainData.status == '新建' ? true : false
      this.changeDataStructure()
    },
    changeDataStructure() {
      let that = this
      this.mainAreaData = [
        {
          label:'方案编号',
          value:this.mainData.promnum
        },
        {
          label:'方案名称',
          value:this.mainData.promname
        },
        {
          label:'促销类型',
          value:this.mainData.type
        },
        {
          label:'订货账号',
          value:this.mainData.accountname
        },
        {
          label:'活动开始时间',
          value:this.mainData.begdate 
        },
        {
          label:'活动结束时间',
          value:this.mainData.enddate
        },
        {
          label:'品牌',
          value:this.mainData.brandname
        },
        {
          label:'领域',
          value:this.mainData.tradefield
        },
        {
          label:'起订数量',
          value:this.mainData.orderminqty
        },
        {
          label:'起订金额',
          value:this.mainData.orderminamount
        },
        {
          label:'订单增量',
          value:this.mainData.orderaddqty
        },
        {
          label:'备注',
          value:this.mainData.remarks
        },
        {
          label:'状态',
          value:this.mainData.status,
          style:function () {
              let style = {}
              switch (that.mainData.status) {
                case '新建':
                  style = {color:'#000000'}
                  break;
                case '审核':
                  style = {color:'#52C41A'}
                  break;
                case '发布':
                  style = {color:'#d90a0a'}
                  break;
                case '关闭':
                  style = {color:'#b2c0ea'}
                  break;
              }
              return style
            }
        },
      ]
      this.detailInfo = {
        baseInfo: [
          {
            label:'方案编号',
            value:this.mainData.promnum
          },
          {
            label:'方案名称',
            value:this.mainData.promname
          },
          {
            label:'促销类型',
            value:this.mainData.type
          },
          {
            label:'订货账号',
            value:this.mainData.accountname
          },
          {
            label:'活动开始时间',
            value:this.mainData.begdate 
          },
          {
            label:'活动结束时间',
            value:this.mainData.enddate
          },
          {
            label:'品牌',
            value:this.mainData.brandname
          },
          {
            label:'领域',
            value:this.mainData.tradefield
          },
          {
            label:'起订数量',
            value:this.mainData.orderminqty
          },
          {
            label:'起订金额',
            value:this.tool.formatAmount(this.mainData.orderminamount,2)
          },
          {
            label:'订单增量',
            value:this.mainData.orderaddqty
          },
          {
            label:'备注',
            value:this.mainData.remarks
          },
          {
            label:'状态',
            value:this.mainData.status,
            style:function () {
              let style = {}
              switch (that.mainData.status) {
                case '新建':
                  style = {color:'#000000'}
                  break;
                case '审核':
                  style = {color:'#52C41A'}
                  break;
                case '发布':
                  style = {color:'#d90a0a'}
                  break;
                case '关闭':
                  style = {color:'#b2c0ea'}
                  break;
              }
              return style
            }
          },
        ],
        systemInfo: [
          {label:'录入人',value:this.mainData.createby},
          {label:'录入时间',value:this.mainData.createdate},
          {label:'审核人',value:this.mainData.checkby},
          {label:'审核时间',value:this.mainData.checkdate},
          {label:'发布人',value:this.mainData.sendby},
          {label:'发布时间',value:this.mainData.senddate},
        ]
      }
    },
    async productDel (data) {
      console.log(data,'111');
      
      let res = await this.$api.requested({
        "id":20230102142603,
        "content": {
          "sa_promotion_itemsids":[data.sa_promotion_itemsid]
        }
      })
      this.tool.showMessage(res,() => {
        this.$refs.productDetail.listData(data.sa_promotion_itemgroupid)
      })
    },
    // 监听切换数据,上一页,下一页
    pageChange (id,rowindex) {
      this.flag = false
      this.$router.replace({path:'/promotionDetail',query:{id:id,rowindex:rowindex}})
      this.queryMainData(id)
    },
    onSuccess(){
      this.queryMainData(this.$route.query.id)
    }
  },
  created() {
    this.queryMainData(this.$route.query.id)
  }
}
</script>

<style scoped>

</style>