<template>
    <div>
    <basicDetails
        ref="details"
        :titleText="`${mainData.billno}`"
        :editData="mainData"
        :mainAreaData="mainAreaData"
        delApiId="20221114135503"
        turnPageId="20221114135403"
        idname="sa_dispatchid"
        ownertable="sa_dispatch"
        :statusCheck="[{key:'STATUS',value:'手工关闭'},{key:'STATUS',value:'审核'},{key:'STATUS',value:'复核'},{key:'STATUS',value:'关闭'}]"
        :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,'cancelBill')" :disabled="mainData.STATUS !== '复核'" @click="cancelBill">手工关闭</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 !== '新建'" @click="onReCheck">复核</el-button>
      </div>
      <div slot="slot2">
        <default-info :mainData="mainData"></default-info>
      </div>
      <div slot="slot0">
        <product-list ref="product" :data="mainData" @onSuccess="queryMainData"></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.agentnum
          },
          {
            label:'经销商名称',
            value:this.mainData.enterprisename
          },
          {
            label:'发货单号',
            value:this.mainData.billno?this.mainData.billno:'--'
          },
          {
            label:'订单号',
            value:this.mainData.sonum?this.mainData.sonum:'--'
          },
          {
            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.createdate,
          },
          {
            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(`复核后同步更新ERP，确定复核该发货单吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const res = await this.$api.requested({
            "id":20221114135803,
            "content": {
              "sa_dispatchid":this.$route.query.id,
              "isrecheck":true
            }
          })
          this.tool.showMessage(res,()=>{
            this.queryMainData()
          })
        }).catch((err) => {
          console.log(err)
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
      },
      async cancelBill () {
        this.$confirm(`确定关闭该发货单吗`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const res = await this.$api.requested({
            "id": "20230404134703",
            "content": {
              "sa_dispatchid":this.mainData.sa_dispatchid
            }
          })

          this.tool.showMessage(res,()=>{
            this.queryMainData()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        })
      }
    },
    mounted () {
      this.queryMainData(this.$route.query.id)
    },
    created() {
    }
  }
  </script>

  <style scoped>

  </style>
