//配送明细
<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div class="filter-container">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="订单编号" >
          <el-input v-model="searchForm.order_num" placeholder="请输入订单编号" />
        </el-form-item>
        <!-- <el-form-item label="配送司机" >
          <el-input v-model="searchForm.driver_name" placeholder="请输入配送司机" />
        </el-form-item> -->
        <el-form-item label="车辆号码" >
          <el-input v-model="searchForm.driver_account" placeholder="请输入车辆号码" />
        </el-form-item>
        <template>
          <div style="display:inline-block;">
            <span class="g66 f700 font-14">配送时间</span>
              <el-date-picker
                v-model="searchForm.start_time"
                :picker-options="pickerOptionsStart"
                type="datetime"
                placeholder="请选择开始时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                @change="changeEnd"/>
              <span>~</span>
              <el-date-picker
                v-model="searchForm.end_time"
                :picker-options="pickerOptionsEnd"
                type="datetime"
                placeholder="请选择结束时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                @change="changeStart"/>
          </div>
        </template>
        <el-form-item label="供应商名称" >
          <el-input v-model="searchForm.supplier_name" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="工地名称" >
          <el-input v-model="searchForm.site_name" placeholder="请输入工地名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="fetchData">搜索</el-button>
          <el-button :loading="downloadLoading" class="ml-10" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <el-table v-loading="listLoading" :data="firmList" border fit  highlight-current-row style="width: 99%;">
      <el-table-column label="ID" prop="delivery_id" align="center" width="80"></el-table-column>
      <el-table-column label="订单编号" prop="order_num" align="center"></el-table-column>
      <!-- <el-table-column label="配送司机" prop="driver_name" align="center"></el-table-column> -->
      <el-table-column label="车辆号码" prop="driver_account" align="center"></el-table-column>
      <el-table-column label="联系电话" prop="driver_phone" align="center"></el-table-column>
      <el-table-column label="供应商名称" prop="supplier_name" align="center"></el-table-column>
      <el-table-column label="中转站名称" prop="transit_name" align="center"></el-table-column>
      <el-table-column label="工地名称" prop="site_name" align="center"></el-table-column>
      <el-table-column label="类型" prop="product_type" align="center">
        <template slot-scope="scope">{{getProductType(scope.row.product_type)}}</template>
      </el-table-column>
      <el-table-column label="品种编号" prop="variety_num" align="center"></el-table-column>
      <el-table-column label="出厂数量(吨)" prop="order_ton" align="center"></el-table-column>
      <el-table-column label="实收数量(吨)" prop="over_ton" align="center"></el-table-column>
      <el-table-column label="运费" prop="post_fee" align="center"></el-table-column>
      <el-table-column label="配送时间" prop="create_time" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="220">
        <template slot-scope="scope">
          <router-link :to="'/distributionManagement/deliveryEdit/'+scope.row.delivery_id">
            <el-button class="mr-10" type="primary" size="mini">编辑</el-button>
          </router-link>
          <el-button :disabled="scope.row.post_fee!==''" size="mini" type="primary"
          @click="handleEntry(scope.row.delivery_id)"> 
            {{scope.row.post_fee!==''?'已录入':'运费录入'}} 
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 翻页 -->
    <pagination v-show="total>0" 
      :total="total" 
      :page.sync="searchForm.page" 
      :page_len.sync="searchForm.page_len"
      :limit.sync="searchForm.limit" 
      @pagination="fetchData" />
    <!-- 录入信息弹窗 -->
    <el-dialog title="录入信息" :visible.sync="dialogEntry" center width="340px" @close="closeDialog">
      <el-form :model="entryInfo" label-width="60px" :rules="formRules" ref="postForm" >
        <el-form-item label="运费：" prop="post_fee">
          <el-input v-model="entryInfo.post_fee" placeholder="请输入运费"/>
        </el-form-item>
      </el-form>
      <el-button style="margin-left:110px" type="primary" @click="submitForm">保存</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { checkNum } from '@/utils/validate'
import { tinyDelivery, savePost, exportTiny } from '@/api/order/index'
import { baseUrl } from '@/utils/env.js'

export default {
  data() {
    return {
      firmList: [],  // 列表
      total: 0,  // 总页数
      listLoading: true,
      // 搜索相关数据
      searchForm: {
        page: 1,
        page_len: 10,
        craet_time: '',
        // 'transit name':'惠州中转站',
        enterprise_id:1
      },
      downloadLoading: false,  // 导出按钮状态
      dialogEntry: false, // 录入信息弹窗
      entryInfo: {}, // 录入信息
      // 校验规则
      formRules: {
        post_fee: [ checkNum ]
      },
      // 限制开始时间
      pickerOptionsStart: {},  
      pickerOptionsEnd: {},
      baseUrl,
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取配送列表
    fetchData(obj={}) {
      if(obj.page) {
        this.searchForm.page = obj.page
        this.searchForm.page_len = obj.page_len
      }
      this.searchForm.start_time = this.$route.query.start_time || this.searchForm.start_time;
      this.$http(tinyDelivery(this.searchForm), (res) => {
        if (res.code === 0) {
          this.firmList = res.data.result;  // 将结果数组赋值给 firmList
          // console.log('获取到的企业列表:', this.firmList);
          
          this.total = res.data.data_sum;  // 更新 total 为 data_sum
          this.listLoading = false;
        } else {
          console.error('获取数据出错:', res.message);
          this.listLoading = true;
        }
      }).catch(error => {
        console.error('请求失败:', error);
      });
    },
    // 录入信息
    handleEntry(id) {
      this.entryInfo.detail_id = id
      this.dialogEntry = true
    },
    // 配送产品
    getProductType(value) {
      return value=="type-00"?"散装":(value=="type-01"?"袋装":"")
    },
    // 保存录入信息
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          this.$http(savePost(this.entryInfo), res => {
            this.$message({
              message: '成功',
              type: 'success'
            })
            this.fetchData()
            this.dialogEntry = false
          })
        } else {
          return false
        }
      })
    },
    // 导出
    handleDownload() {
      this.$http(exportTiny(this.searchForm), (res) => {
        window.location.href = this.baseUrl + res.data.url
      })
    },
    // 结束时间限制开始时间
    changeStart() {
      let self = this
      if (!this.searchForm.end_time) {
        this.pickerOptionsStart = {
          disabledDate: {}
        }
        return
      }
      this.pickerOptionsStart = Object.assign({}, self.pickerOptionsStart, {
        disabledDate(time) {
          return time.getTime() > new Date(self.searchForm.end_time).getTime()
        }
      })
    },
    // 开始时间控制结束时间
    changeEnd() {
      this.$route.query.start_time = ''
      let self = this
      if (!this.searchForm.start_time) {
        this.pickerOptionsEnd = {
          disabledDate: {}
        }
        return
      }
      this.pickerOptionsEnd = Object.assign({}, self.pickerOptionsEnd, {
        disabledDate(time) {
          return time.getTime() < new Date(self.searchForm.start_time).getTime()
        }
      })
    },
    // 关闭弹窗
    closeDialog() {
      this.$refs['postForm'].resetFields();
    },
  }
}
</script>

<style lang='scss' scoped>

</style>
