<template>
  <div>
    <basicDetails
        ref="details"
        :titleText="`${mainData.sonum}`"
        :editData="mainData"
        :mainAreaData="mainAreaData"
        turnPageId="20221224180302"
        idname="sa_orderid"
        ownertable="sa_order"
        delApiId="20221108152102"
        tags=""
        :statusCheck="[{key:'status',value:'审核'},{key:'status',value:'提交'},{key:'status',value:'关闭'}]"
        :tabs="['基本信息','订单明细','订单进度','居间费明细','发货单','物流单','发票记录','收支明细']"
        @pageChange="pageChange"
        @onEditSuccess="queryMainData($route.query.id)">
        <div slot="customOperation" class="inline-16">
          <Edit class="inline-16" v-if="tool.checkAuth($route.name,'update') && mainData.status == '新建'" :data="mainData" @onSuccess="queryMainData" />
          <el-button :disabled="mainData.status !== '交期待确认'"  type="primary" size="mini" @click="confirmdate">确认交期</el-button>
          <el-button v-if="tool.checkAuth($route.name,'submit')" :disabled="mainData.status !== '新建'"  type="primary" size="mini" @click="onSubmit('提交')">提 交</el-button>
          <el-button v-if="tool.checkAuth($route.name,'insert')"  type="primary" size="mini" @click="onCopy">复 制</el-button>
      </div>
      <div slot="slot0">
        <default-info :mainData="mainData"></default-info>
      </div>
      <div slot="slot1">
        <product-list :data="mainData" ref="prod"></product-list>
      </div>
      <div slot="slot2">
        <orderprogress ref="prod"></orderprogress>
      </div>
      <div slot="slot3">
        <rebat></rebat>
      </div>
      <div slot="slot4">
        <dispatch></dispatch>
      </div>
      <div slot="slot5">
        <logistics></logistics>
      </div>
      <div slot="slot6">
        <invoiceTable></invoiceTable>
      </div>
      <div slot="slot7">
        <revenue :data="mainData"></revenue>
      </div>
    </basicDetails>
    <el-dialog append-to-body title="退回原因" :visible.sync="dialogVisible" width="400px">
      <el-input v-model="backreason" type="textarea" placeholder="请输入退回原因"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="onRebanck" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Edit from '../modules/edit'
import defaultInfo from './tabs/defaultInfo.vue';
import productList from '../modules/productlist.vue'
import rebat from './tabs/rebatesettlement.vue'
import orderprogress from './tabs/orderpProgress.vue'
import logistics from './tabs/logistics.vue'
import dispatch from './tabs/dispatch.vue'
import invoiceTable from './tabs/invoiceTable.vue'
import revenue from './tabs/revenueAndExp.vue'

export default {
  name: "detail",
  data() {
    return {
      mainData:{},
      mainAreaData:{},
      backreason:'',
      dialogVisible:false
    }
  },
  provide() {
    return {
      thisDetail: () => this
    }
  },
  components:{
    Edit,
    defaultInfo,
    productList,
    rebat,
    orderprogress,
    dispatch,
    logistics,
    invoiceTable,
    revenue
  },
  methods:{
    async queryMainData(id) {
      return new Promise(async (re,rj) => {
        const res = await this.$api.requested({
          "id":'20221108151302',
          "content": {
            "sa_orderid": this.$route.query.id
          }
        })
        this.mainData = res.data
        console.log(this.mainData);
        
        this.changeDataStructure()
        re()
      })
      
    },
    changeDataStructure() {
      let that = this
      this.mainAreaData = [
        {
          label:'订单类型',
          value:this.mainData.type
        },
        {
          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:"#e09a1a"}
                break;
              case '交期确认':
                style = {color:"#3874f6"}
                break;
              case '审核':
                style = {color:"#52C41A"}
                break;
              case '关闭':
                style = {color:"#b2c0ea"}
                break;
              default:
                break;
            }
            return style
          }
        },
        {
          label:'单据日期',
          value:this.mainData.createdate
        },
        {
          label:'合计数量',
          value:this.mainData.qty,
          style:function () {
            let style = {color:"#ff0000"}
            return style
          }

        },
        {
          label:'合计金额',
          value:this.mainData.amount,
          style:function () {
            let style = {color:"#ff0000"}
            return style
          }
        },
        {
          label:'折前金额',
          value:this.mainData.defaultamount,
          style:function () {
            let style = {color:"#ff0000"}
            return style
          }
        },
        {
          label:'企业名称',
          value:this.mainData.enterprisename
        },
        {
          label:'备注',
          value:this.mainData.remarks
        }
      ]
    },
    // 监听切换数据,上一页,下一页
    pageChange (id,rowindex,tabIndex) {
      this.$router.replace({path:'/orderdetail',query:{id:id,rowindex:rowindex}})
      this.queryMainData(id)
    },
    // 提交审核订单
    onSubmit (type) {
      this.$confirm(`是否${type}该订单`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await this.$api.requested({
          "id": type === '审核'?20221108153502:20221108153402,
          "content": {
              "sa_orderid": this.mainData.sa_orderid,
              "sys_enterpriseid": this.mainData.sys_enterpriseid,
              "sa_accountclassid": this.mainData.accountclass.sa_accountclassid
          },
        })
        this.tool.showMessage(res,async ()=>{
          this.queryMainData()
        })
      }).catch((err) => {
        this.$message({
          type: 'info',
          message: err
        });          
      });
    },
    // 反审核订单
    onReturnCheck () {
      this.$confirm(`是否反审核该订单`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await this.$api.requested({
          "id": 20230102141502,
          "content": {
            "sa_orderid": this.mainData.sa_orderid,
          },
        })
        this.tool.showMessage(res,()=>{
          this.queryMainData()
        })
      }).catch((err) => {
        this.$message({
          type: 'info',
          message: err
        });          
      });
    },
    async onRebanck () {
      const res = await this.$api.requested({
        "id": 20221108161902,
        "content": {
          "sys_enterpriseid":this.mainData.sys_enterpriseid,
          "sa_accountclassid":this.mainData.accountclass.sa_accountclassid,
          "sa_orderid": this.mainData.sa_orderid,
          "backreason": this.backreason //退回原因,可选
        },
      })
      this.tool.showMessage(res,()=>{
        this.queryMainData()
        this.dialogVisible = false
      })
    },
    async closeOrder () {
      const res = await this.$api.requested({
        "id": 20221108164502,
        "content": {
          "sa_orderids":[this.$route.query.id]
        },
      })
      this.tool.showMessage(res,()=>{
        this.queryMainData()
        this.dialogVisible = false
      })
    },
    async confirmdate () {
      const res = await this.$api.requested({
        "id": 20221230094802,
        "content": {
          "sa_orderid": this.$route.query.id
        }
      })
      this.tool.showMessage(res,()=>{
        this.queryMainData()
      })
    },
    async onCopy () {
      const res = await this.$api.requested({
        "id": 20230102144502,
        "content": {
          "sa_orderid": this.$route.query.id
        }
      })
      this.tool.showMessage(res,()=>{
        let route = this.$route
        if (route.path !== '/taskDetails') {
          this.oldRoute = {path:route.path,query:route.query}
          this.$store.dispatch('setHistoryRouter',this.oldRoute)
        }
        this.$router.replace({path:'/agent_orderDetails',query:{id:res.data.sa_orderid,rowindex:res.data.rowindex}})
        this.queryMainData(this.$route.query.id)
        })
    },
  },
  mounted () {
    this.queryMainData(this.$route.query.id)
  },
  created() {
  }
}
</script>

<style scoped>
</style>