<template>
  <div>
    <basicDetails
      ref="details"
      :titleText="`单号:${mainData.sonum}`"
      :oldFormPath="{edit:'SDrpManagement/salerToolBorrowing/modules'}"
      :editData="mainData"
      :mainAreaData="mainAreaData"
      turnPageId="20230114105002"
      idname="sa_orderid"
      ownertable="sa_order"
      delApiId="20230116100002"
      :statusCheck="[{key:'status',value:'审核'},{key:'status',value:'交期待确认'},{key:'status',value:'提交'},{key:'status',value:'交期确认'}]"
      :tabs="['借用单明细']"
      @pageChange="pageChange"
      @onEditSuccess="onEditSuccess">
      <div slot="customOperation" class="inline-16">
          <el-button v-if="tool.checkAuth($route.name,'confirmDate')" :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>
      </div>
      <div slot="slot0" >
        <toolList ref="tool" :data="mainData" @queryDetail="queryMainData" @closeTool="onEditSuccess"></toolList>
      </div>
      <div slot="slot1" >
      </div>
    </basicDetails>
  </div>
</template>

<script>
import toolList from './tabs/toolList.vue'
import confirmDate from '../modules/confirmDate.vue'
import {mapGetters} from "vuex";

export default {
  name: "detail",
  data() {
    return {
      mainData:{},
      mainAreaData:{},
      options:[],
      value:''
    }
  },
  components:{
    toolList,
    confirmDate
  },
  provide () {
    return {
      thisDetail:()=> this 
    }
  },
  methods:{
    async orderreviewtype () {
      const res = await this.$store.dispatch('optiontypeselect','orderreviewtype')
      this.options = res.data
      this.value = res.data[0].value
    },
    onEditSuccess(){
      this.queryMainData()
      this.$refs.tool.listData()
    },
    async queryMainData(id) {
      const res = await this.$api.requested({
        "id":20230114140402,
        "content": {
          "sa_orderid": this.$route.query.id
        }
      })
      this.mainData = res.data
      this.changeDataStructure()
      this.orderreviewtype()
    },
    changeDataStructure() {
      let that = this
      this.mainAreaData = [
        {
          label:'单据日期',
          value:this.mainData.billdate
        },
        {
          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.enterprisename
        },
        {
          label:'合计数量',
          value:this.mainData.qty * this.mainData.toolcount
        },
        {
          label:'合计金额',
          value:this.tool.formatAmount(this.mainData.amount,2)
        },
        {
          label:'借用合同',
          value:this.mainData.contract_title?this.mainData.contract_title:'无合同'
        },
        {
          label:'审核日期',
          value:this.mainData.checkdate
        },
        {
          label:'收货人',
          value:this.mainData.name
        },
        {
          label:'收货地址',
          value:this.mainData.province + this.mainData.city + this.mainData.county + this.mainData.address
        },

        {
          label:'备注',
          value:this.mainData.remarks
        }
      ]
    },
    // 监听切换数据,上一页,下一页
    pageChange (id,rowindex,tabIndex) {
      this.$refs['details'].param.content.isManage = 0
      tabIndex = this.$route.query.tabIndex
      this.$router.replace({path:'/toolBorrowing_salerDetail',query:{id:id,rowindex:rowindex,tabIndex:tabIndex}})
      this.queryMainData(id)
    },
    onSuccess(){
      this.visible = false
      this.queryMainData(this.$route.query.id)
      this.$emit('onSuccess')
    },
    // 提交审核订单
    onSubmit (type) {
      this.$confirm(`是否${type}该订单`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await this.$api.requested({
          "id": type === '审核'?20230114161402:20230114160002,
          "content": {
            "sa_orderid": this.mainData.sa_orderid,
            "sys_enterpriseid": this.mainData.sys_enterpriseid,
            "sa_accountclassid": this.mainData.accountclass.sa_accountclassid,
            "reviewtype":type === '审核'?this.value:''
          },
        })
        this.tool.showMessage(res,async ()=>{
          this.visible = false
          this.queryMainData()
        })
      }).catch((err) => {
        this.$message({
          type: 'info',
          message: '取消'
        });          
      });
    },
    async confirmdate () {
      const res = await this.$api.requested({
        "id": 20230129154102,
        "content": {
          "sa_orderid": this.$route.query.id
        }
      })
      this.tool.showMessage(res,()=>{
        this.queryMainData()
      })
    },
  },
  mounted () {
    this.queryMainData(this.$route.query.id)
  },
  created() {
  }
}
</script>

<style scoped>

</style>