<template>
  <div class="app-container">
    <el-form ref="postForm" :rules="formRules" :model="formData" label-width="150px">
      <el-form-item label="订单编号" prop="delivery_no">
        <el-col :span="6">
          <el-input v-model="formData.delivery_no" placeholder="请输入订单编号" disabled/>
        </el-col>
      </el-form-item>
      <el-form-item label="车辆号码" prop="driver_account">
        <el-col :span="6">
          <el-input v-model="formData.driver_account" placeholder="请输入车辆号码"/>
        </el-col>
      </el-form-item>
      <el-form-item label="联系电话" prop="driver_phone">
        <el-col :span="6">
          <el-input v-model="formData.driver_phone" placeholder="请输入联系电话"/>
        </el-col>
      </el-form-item>
      <el-form-item label="供应商名称：" prop="supplier_id">
        <el-col :span="6">
          <el-select v-model="formData.supplier_id" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in supplierList" 
              :key="index"
              :label="item.data_name" :value="item.data_id" >
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="中转站名称：" prop="transit_id">
        <el-col :span="6">
          <el-select v-model="formData.transit_id" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in transitList" 
              :key="index"
              :label="item.transit_name" :value="item.transit_id" >
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="工地名称：" prop="site_id">
        <el-col :span="6">
          <el-select v-model="formData.site_id" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in siteList" 
              :key="index"
              :label="item.data_name" :value="item.data_id" >
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="产品类型" prop="product_type">
        <el-col :span="6">
          <el-select v-model="formData.product_type" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in productTypeList" 
              :key="index"
              :label="item.data_name" :value="item.data_id" >
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="品种编号" prop="variety_num">
        <el-col :span="6">
          <el-select v-model="formData.variety_num" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in varietyList" 
              :key="index"
              :label="item.data_name" :value="item.data_id" >
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="出厂数量(吨)" prop="order_ton">
        <el-col :span="6">
          <el-input v-model="formData.order_ton" placeholder="请输入出厂数量(吨)"/>
        </el-col>
      </el-form-item>
      <el-form-item label="实收数量(吨)" prop="over_ton">
        <el-col :span="6">
          <el-input v-model="formData.over_ton" placeholder="请输入实收数量(吨)"/>
        </el-col>
      </el-form-item>
      <el-form-item label="运费" prop="post_fee">
        <el-col :span="6">
          <el-input v-model="formData.post_fee" placeholder="请输入运费"/>
        </el-col>
      </el-form-item>
      <el-form-item label="配送时间：" prop="create_time">
        <el-col :span="6">
          <el-date-picker
            v-model="formData.create_time"
            :picker-options="pickerOptionsStart"
            type="datetime"
            placeholder="请选择时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="changeEnd"/>
        </el-col>
      </el-form-item>
      <el-form-item align="center">
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { checkRqInput, checkNum } from '@/utils/validate'
import { getData, createDelivery, detailPostCash, createPostCash, updatePostCash, getTransitList, detailDelivery } from '@/api/order/index'

export default {
  name: '',
  // 配合路由配置的props属性实现在组件中通过props获取参数
  props: {
    id: {
      type: String,
      default() {
        return this.$route.params.id;
      }
    }
  },
  data() {
    return {
      formData: {
        supplier_id: '',
        transit_id: '',
        site_id: '',
        cash_fee: '',
        start_time: '',
        end_time: '',
        post_note: ''
      },
      // 校验规则
      formRules: {
        supplier_id: [ checkRqInput ],
        site_id: [ checkRqInput ],
        cash_fee: [ checkRqInput, checkNum ],
        start_time: [ checkRqInput ],
        end_time: [ checkRqInput ],
      },
      supplierList: [], // 供应商列表
      transitList: [], // 中转站列表
      varietyList: [], // 品种列表
      siteList: [], // 工地列表
      productTypeList: [
        {
          data_id: 'type-00',
          data_name: '散装'
        },
        {
          data_id: 'type-01',
          data_name: '袋装'
        }
      ],
      // 限制开始时间 
      pickerOptionsStart: {},  
      pickerOptionsEnd: {},
    }
  },
  created() {
    this.fetchSupplier()
    this.fetchSite()
    this.fetchVariety()
    this.fetchTransit()
    this.fetchDetail()
  },
  methods: {
    // 获取供应商列表
    fetchVariety() {
      this.$http(getData("data-01"), (res) => {
        this.varietyList = res.data
      })
    },
    // 获取供应商列表
    fetchSupplier() {
      this.$http(getData("data-02"), (res) => {
        this.supplierList = res.data
      })
    },
    // 获取工地列表
    fetchSite() {
      this.$http(getData("data-03"), (res) => {
        this.siteList = res.data
      })
    },
    // 获取工地列表
    fetchTransit() {
      this.$http(getTransitList(), (res) => {
        this.transitList = res.data
      })
    },
    // 获取企业详情
    fetchDetail() {
      const id = this.$props.id
      if (id) {
        this.$http(detailDelivery(id), (res) => {
          if (res.code == 0) {
            this.formData = res.data
          }
        })
      }
    },
    // 提交表单
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          // 修改
          if (this.formData.post_id) {
            this.$http(updatePostCash(this.formData), res => {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.$router.go(-1)
            })
          } else {
            // 新增
            this.$http(createPostCash(this.formData), res => {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.$router.go(-1)
            })
          }
        } else {
          return false
        }
      })
    },
    // 取消
    onCancel() {
      this.$message({
        message: '已取消!',
        type: 'warning'
      })
      this.$router.go(-1)
    },
    // 结束时间限制开始时间
    changeStart() {
      let self = this
      if (!this.formData.end_time) {
        this.pickerOptionsStart = {
          disabledDate: {}
        }
        return
      }
      this.pickerOptionsStart = Object.assign({}, self.pickerOptionsStart, {
        disabledDate(time) {
          return time.getTime() > new Date(self.formData.end_time).getTime()
        }
      })
    },
    // 开始时间控制结束时间
    changeEnd() {
      let self = this
      if (!this.formData.start_time) {
        this.pickerOptionsEnd = {
          disabledDate: {}
        }
        return
      }
      this.pickerOptionsEnd = Object.assign({}, self.pickerOptionsEnd, {
        disabledDate(time) {
          return time.getTime() < new Date(self.formData.start_time).getTime()
        }
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
