<template>
    <div>
    <basicDetails
        ref="details"
        :titleText="`${mainData.billno}`"
        :editData="mainData"
        :mainAreaData="mainAreaData"
        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 class="inline-16" :type="mainData.status !== '复核'?'':'primary'" size="mini"  v-if="tool.checkAuth($route.name,'createBill') && (mainData.status == '新建' || mainData.status == '提交' || mainData.status == '复核')" :disabled="mainData.status !== '复核'" @click="addLogistics">{{$t(`生成物流单`)}}</el-button>
        <confirmSelection class="inline-16" v-if="tool.checkAuth($route.name,'submit') && mainData.status == '新建'" btnTitle="提交" :disabled="mainData.status !== '新建'" btnType="primary" content="是否确定提交发货单" confirmButtonText="确定" cancelButtonText="取消" paramId="2024120915192102" idName="sa_dispatchid" @onSuccess="onSuccess"></confirmSelection>
        <confirmSelection class="inline-16" v-if="tool.checkAuth($route.name,'revocation') && mainData.status == '提交'" btnTitle="撤回" :disabled="mainData.status !== '提交'" btnType="primary" content="是否确定撤回发货单" confirmButtonText="确定" cancelButtonText="取消" paramId="2024120915580002" idName="sa_dispatchid" @onSuccess="onSuccess"></confirmSelection>
        <el-button :type="mainData.status !== '审核' && mainData.status !== '提交'?'':'primary'" class="inline-16" size="mini"  v-if="tool.checkAuth($route.name,'check') && mainData.status !== '复核' && mainData.status !== '关闭'" :disabled="mainData.status !== '审核' && mainData.status !== '提交'" @click="onCheck">{{mainData.status === '审核'?$t('反审核'):mainData.status === '复核'?$t('反审核'):$t('审核')}}</el-button>
        <el-button style="margin-left: 0px"  class="inline-16" :type="mainData.status !== '审核' && mainData.status !== '复核'?'':'primary'" size="mini" v-if="tool.checkAuth($route.name,'recheck') && mainData.status !== '关闭'" :disabled="mainData.status !== '审核' && mainData.status !== '复核'" @click="onReCheck">{{mainData.status === '复核'?$t('反复核'):$t('复核')}}</el-button>
        <el-button :type="mainData.status != '新建' && mainData.status != '关闭'?'':'primary'" size="mini" @click="onDel" style="margin-left: 0px" class="inline-16" :disabled="mainData.status != '新建' && mainData.status != '关闭'">{{$t('删 除')}}</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
          }
        })
        if (res.data) 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.$t(this.mainData.status),
            style:function () {
              let style = that.tool.getStatusColor(that.mainData.status)
              return style
            }
          },
          {
            label:'发货数量',
            value:this.mainData.qty
          },
          {
            label:'发货金额',
            value:this.mainData.sumamount ? this.tool.formatAmount(this.mainData.sumamount,2) : '--',
          },
          {
            label:'运费状态',
            value:this.$t(this.mainData.freightstatus)
          },
          {
            label:'订单类型',
            value:this.mainData.type
          },
          {
          label:'回签单',
            value:this.mainData.signbackstatus
          },
          {
            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.$t(`确定`)}${this.mainData.status === '审核'?this.$t('反审核'):this.$t('审核')}${this.$t(`该发货单`)}`, this.$t('提示'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          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()
            this.$refs.product && this.$refs.product.listData()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: this.$t('已取消')
          });
        });
      },
      onReCheck() {
        this.$confirm(`${this.$t(`确定`)}${this.mainData.status === '复核'?this.$t('反复核'):this.$t('复核')}${this.$t(`该发货单`)}`, this.$t('提示'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          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: this.$t('已取消')
          });
        });
      },
      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}})
        })
      },
      onDel(){
        this.$confirm(this.$t('确定删除当前数据吗')+'?', this.$t('提示'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        }).then(async () => {
          const res = await this.$api.requested({
            "id":20221114135503,
            "content":{
              sa_dispatchids:[this.$route.query.id]
            }
          })
          if (res.code === 0){
            this.$message.error(res.data[0].errmsg);
          }else {
            this.$message({
              message: this.$t('删除成功'),
              type: 'success'
            })
            this.$store.dispatch('changeDetailDrawer',false);
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: this.$t('已取消删除')
          });
        });

      }
    },
    mounted () {

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

  <style scoped>

  </style>
