<template>
    <div>
    <basicDetails
        ref="details"
        :titleText="`${mainData.billno}`"
        :editData="mainData"
        :mainAreaData="mainAreaData"
        delApiId="20221114135503"
        turnPageId="20221114135403"
        idname="sa_dispatchid"
        ownertable="sa_dispatch"
        tags=""
        :tabs="['发货明细','erp上传信息','基础信息']"
        @pageChange="pageChange"
        @onEditSuccess="queryMainData($route.query.id)">
      <div slot="tags">
      </div>
      <div slot="customOperation">
        <Edit class="inline-16" v-if="tool.checkAuth($route.name,'update') && mainData.STATUS == '新建'" :data="mainData" @onSuccess="onSuccess" />
        <el-button type="primary" size="mini"  v-if="tool.checkAuth($route.name,'createBill')" :disabled="mainData.STATUS !== '复核'" @click="addLogistics">生成物流单</el-button>
        <el-button type="primary" size="mini"  v-if="tool.checkAuth($route.name,'check')" :disabled="mainData.STATUS !== '新建' && mainData.STATUS !== '审核'" @click="onCheck">{{mainData.STATUS === '审核'?'反审':mainData.STATUS === '复核'?'反审':'审核'}}</el-button>
        <el-button  class="inline-16" type="primary" size="mini" v-if="tool.checkAuth($route.name,'recheck')" :disabled="mainData.STATUS !== '审核' && mainData.STATUS !== '复核'" @click="onReCheck">{{mainData.STATUS === '复核'?'反复核':'复核'}}</el-button>
      </div>
      <div slot="slot2" >
        <default-info :mainData="mainData"></default-info>
      </div>
      <div slot="slot0" >
        <product-list ref="product" :data="mainData"></product-list>
      </div>
      <div slot="slot1" >
        <erpInfo ref="erp" :data="mainData"></erpInfo>
      </div>
    </basicDetails>
  </div>
</template>

<script>
import Edit from '../modules/edit'
import productList from '../modules/productlist.vue'
import erpInfo from '../modules/erpInfo.vue'
import defaultInfo from '../modules/defaultInfo.vue'
  export default {
    name: "detail",
    data() {
      return {
        mainData:{},
        mainAreaData:{}
      }
    },
    components:{
      Edit,
      productList,
      defaultInfo,
      erpInfo
    },
    methods:{
      async queryMainData(id) {
        const res = await this.$api.requested({
          "id": "20221114135303",
          "version":1,
          "content": {
            "sa_dispatchid":this.$route.query.id
          }
        })
        this.mainData = res.data
        console.log(this.mainData)
        this.changeDataStructure()
      },
      changeDataStructure() {
        let that = this
        this.mainAreaData = [
          {
            label:'发货单号',
            value:this.mainData.billno
          },
          {
            label:'订单号',
            value:this.mainData.sonum
          },
          {
            label:'ERP单号',
            value:this.mainData.erpbillno
          },
          {
            label:'发货日期',
            value:this.mainData.billdate
          },
          {
            label:'经销商编号',
            value:this.mainData.agentnum
          },
          {
            label:'经销商名称',
            value:this.mainData.enterprisename
          },
          {
            label:'项目名称',
            value:this.mainData.projectname
          },
          {
            label:'状态',
            value:this.mainData.STATUS,
            style:function () {
              let style = {}
              switch (that.mainData.STATUS) {
                case '新建':
                  style = {color:"#000000"}
                  break;
                case '提交':
                  style = {color:"#d90a0a"}
                  break;
                case '审核':
                  style = {color:"#52C41A"}
                  break;
                case '复核':
                  style = {color:"#3874f6"}
                  break;
                case '关闭':
                  style = {color:"#b2c0ea"}
                  break;
                default:
                  break;
              }
              return style
            }
          },
          {
            label:'发货数量',
            value:this.mainData.qty
          },
          {
            label:'发货金额',
            value:this.mainData.sumamount ? this.tool.formatAmount(this.mainData.sumamount,2) : '--',
          },
          {
            label:'运费状态',
            value:this.mainData.freightstatus
          },
          {
            label:'备注',
            value:this.mainData.remarks
          },
        ]
      },
      // 监听切换数据,上一页,下一页
      pageChange (id,rowindex,tabIndex) {
        this.flag = false
        tabIndex = this.$route.query.tabIndex
        this.$router.replace({path:'/dispatchdetail',query:{id:id,rowindex:rowindex,tabIndex:tabIndex}})
        this.queryMainData(id)
      },
      onSuccess(){
        this.visible = false
        this.queryMainData(this.$route.query.id)
        this.$refs.product && this.$refs.product.listData()
        console.log('触发');
        
        this.$emit('onSuccess')
      },
      onCheck () {
        this.$confirm(`确定${this.mainData.STATUS === '审核'?'反审核':'审核'}该发货单`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const res = await this.$api.requested({
            "id": this.mainData.STATUS === '审核'?20221114135703:20221114135603,
            "version":1,
            "content": { 
                "sa_dispatchid":this.$route.query.id
            }
          })
          this.tool.showMessage(res,()=>{
            this.queryMainData()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
      },
      onReCheck() {
        this.$confirm(`确定${this.mainData.STATUS === '复核'?'反复核':'复核'}该发货单`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const res = await this.$api.requested({
            "id":20221114135803,
            "version":1,
            "content": { 
              "sa_dispatchid":this.$route.query.id,
              "isrecheck":this.mainData.STATUS === '复核'?false:true
            }
          })
          this.tool.showMessage(res,()=>{
            this.queryMainData()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
      },
      async addLogistics () {
        const res = await this.$api.requested({
          "id": "20230216185104",
          "content": {
            "sa_dispatchid":this.mainData.sa_dispatchid
          }
        })
        
        this.tool.showMessage(res,()=>{
          let route = this.$route
          this.oldRoute = {path:route.path,query:route.query}
          this.$store.dispatch('setHistoryRouter',this.oldRoute)
          this.$router.replace({path:'/logisticsdetail',query:{id:res.data[0].sa_logisticsid,rowindex:res.data[0].rowindex}})
        })
      }
    },
    mounted () {
      this.queryMainData(this.$route.query.id)
    },
    created() {
    }
  }
  </script>
  
  <style scoped>
  
  </style>