<template>
  <!-- 销售订单页面 -->
  <div class="salePlan">
    <div class="top">
      <el-input placeholder="请输入内容" v-model="input" clearable> </el-input>
      <el-button type="primary" class="btn" @click="onclick">
        <i class="el-icon-search"></i>查询
      </el-button>
      <el-button type="primary" @click="refresh">
        <i class="el-icon-refresh"></i>刷新
      </el-button>
    </div>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <!-- 已审批 -->
      <el-tab-pane label="已审批" name="four">
        <dilTable v-bind.sync="option4" ref="table" max-height="300">
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
              <el-button
                @click="coproductSendClick(scope.row)"
                type="text"
                size="small"
                >副产品派车</el-button
              >
              <el-button
                @click="checkSaleOrderDetails(scope.row.saleOrderId)"
                type="text"
                size="small"
                >运单详情</el-button
              >
              <el-button
                @click="detailclick(scope.row)"
                type="text"
                size="small"
                >物资详情</el-button
              >
            </template>
          </el-table-column>
          <!-- 物资详情抽屉 -->
          <el-table-column type="expand" width="1">
            <template slot-scope="props">
              <el-form label-position="center" inline class="demo-table-expand">
                <div v-if="false">{{ props }}</div>
                <div>
                  <el-table :data="tableData" border>
                    <el-table-column
                      v-for="(item, i) in tableHead"
                      :key="i"
                      :prop="item.prop"
                      :label="item.label"
                      :width="item.width"
                    ></el-table-column>
                  </el-table>
                </div>
              </el-form>
            </template>
          </el-table-column>
        </dilTable>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import { getCookie } from '@/utils/util.js'
export default {
  name: 'saleOrder',
  data() {
    return {
      activeName: 'four',
      input: '',
      carrierId: null,
      carrierName: null,
      option4: {
        // 表格请求数据的地址
        requestUrl: ''
      },
      //记录旧的row对象 (销售公司已审批)
      oldRow: '',
      //记录上一个展开的点击次数，单数为展开状态，复数为闭合状态 (销售公司已审批)
      oldRowCount: 1,
      tableHead: [
        {
          prop: 'materialName',
          label: '物资名称',
          width: 150
        },
        {
          prop: 'specificationModel',
          label: '规格型号',
          width: 150
        },
        {
          prop: 'materialNumber',
          label: '物资件数',
          width: 100
        },
        {
          prop: 'materialWeight',
          label: '物资重量',
          width: 100
        }
      ],
      //(销售公司已审批)
      tableData: []
    }
  },
  created() {
    this.initUrl()
  },
  methods: {
    checkSaleOrderDetails(saleOrderId) {
      console.log(saleOrderId)
      this.$router.push('saleOrderOODetails/' + saleOrderId)
    },
    initUrl() {
      if (getCookie('orgCode') == 'chengyunshang') {
        this.option4.requestUrl =
          '/api/v1/ams/getSaleOrderListByCarrierSsoId?apiId=409&carrierSsoId=' +
          getCookie('userId')
      } else {
        this.option4.requestUrl =
          '/api/v1/ams/getSaleOrderListByCarrierSsoId?apiId=409'
      }
    },
    refresh() {
      this.$router.go(0)
    },
    handleClick(tab, event) {
      console.log(tab, event)
    },
    onclick() {
      if (this.activeName == 'four') {
        if (getCookie('orgCode') == 'chengyunshang') {
          this.option4.requestUrl =
            '/api/v1/ams/getSaleOrderListByCarrierSsoId?apiId=409&carrierSsoId=' +
            getCookie('userId') +
            '&con=' +
            this.input
        } else {
          this.option4.requestUrl =
            '/api/v1/ams/getSaleOrderListByCarrierSsoId?apiId=409' +
            '&con=' +
            this.input
        }
      }
    },
    coproductSendClick(row) {
      this.$router.push('/addSaleOrderCoproductSendCarrier/' + row.saleOrderId)
      this.axios
        .post('/api/v1/ams/getSaleOrderStatus?saleNumber=' + row.saleNumber)
        .then(res => {
          console.log(res.data.data)
          if (res.data.data == 7) {
            this.$message.error('该订单在金蝶已关闭,无法派车')
            return
          } else {
          }
        })
    },
    // -------查看物资详情 (已审批)
    detailclick(row) {
      // 记录重复点击次数
      if (this.oldRow === row) {
        this.oldRowCount += 1
      }
      // 切换当前详情表
      this.$refs.table.toggleRowExpansion(row)
      // 打开前关闭上一个详情表
      if (this.oldRow != '') {
        if (this.oldRow != row) {
          if (this.oldRowCount % 2 === 1) {
            this.$refs.table.toggleRowExpansion(this.oldRow)
          } else {
            this.oldRowCount = 1
          }
        } else {
          this.oldRow = null
          return
        }
      }
      // 重置上一个点击对象
      this.oldRow = row
      // 根据销售订单id查询物资信息
      this.axios
        .post('/api/v1/ams/getSaleMaterialList?saleOrderId=' + row.saleOrderId)
        .then(res => {
          this.tableData = res.data.data
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.salePlan {
  .top {
    padding: 1.25rem 0.375rem;
    .el-input {
      width: 20%;
      margin-right: 1.25rem;
    }
  }
}
</style>
