<template>
  <div class="app-container">
    <el-form ref="postForm" :rules="formRules" :model="formData" label-width="150px">
      <el-form-item label="路线类型" prop="route_type">
        <el-col :span="6">
          <el-select v-model="formData.route_type" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in routeList" 
              :key="index"
              :label="item.data_name" :value="item.data_id" >
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="供应商名称：" prop="supplier_id" :rules="formRules.checkRqSelect" v-if="formData.route_type == 'type-01' || formData.route_type == 'type-02'">
        <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 lab:rules="formRules.checkRqSelect"el="中转站名称：" prop="transit_id"  v-if="formData.route_type == 'type-02' || formData.route_type == 'type-03'">
        <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" :rules="formRules.checkRqSelect" v-if="formData.route_type == 'type-01' || formData.route_type == 'type-03'">
        <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="cash_fee">
        <el-col :span="6">
          <el-input v-model="formData.cash_fee" placeholder="请输入运费单价"/>
        </el-col>
      </el-form-item>
      <el-form-item label="开始时间：" prop="start_time">
        <el-col :span="6">
          <el-date-picker
            v-model="formData.start_time"
            :picker-options="pickerOptionsStart"
            type="date"
            placeholder="请选择开始时间"
            value-format="yyyy-MM-dd"
            @change="changeEnd"/>
        </el-col>
      </el-form-item>
      <el-form-item label="结束时间：" prop="end_time">
        <el-col :span="6">
          <el-date-picker
            v-model="formData.end_time"
            :picker-options="pickerOptionsEnd"
            type="date"
            placeholder="请选择结束时间"
            value-format="yyyy-MM-dd"
            @change="changeStart"/>
        </el-col>
      </el-form-item>
      <el-form-item label="备注：">
        <el-col :span="6">
          <el-input v-model="formData.post_note" placeholder="请输入备注"/>
        </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, checkRqSelect } from '@/utils/validate'
import { getData, createDelivery, detailPostCash, createPostCash, updatePostCash, getTransitList } from '@/api/order/index'

export default {
  name: '',
  // 配合路由配置的props属性实现在组件中通过props获取参数
  props: {
    id: {
      type: String,
      default() {
        return this.$route.params.id;
      }
    }
  },
  data() {
    return {
      formData: {
        route_type: 'type-01',
        supplier_id: '',
        transit_id: '',
        site_id: '',
        cash_fee: '',
        start_time: '',
        end_time: '',
        post_note: ''
      },
      // 校验规则
      formRules: {
        checkRqInput,
        checkRqSelect,
        route_type: [ checkRqInput ],
        // supplier_id: [ checkRqInput ],
        // site_id: [ checkRqInput ],
        // transit_id: [ checkRqInput ],
        cash_fee: [ checkRqInput, checkNum ],
        start_time: [ checkRqInput ],
        end_time: [ checkRqInput ],
      },
      routeList: [{
        data_id: 'type-01',
        data_name: '供应商-工地'
      },{
        data_id: 'type-02',
        data_name: '供应商-中转站'
      },{
        data_id: 'type-03',
        data_name: '中转站-工地'
      }],
      supplierList: [], // 供应商列表
      transitList: [], // 中转站列表
      siteList: [], // 工地列表
      // 限制开始时间 
      pickerOptionsStart: {},  
      pickerOptionsEnd: {},
    }
  },
  created() {
    this.fetchSupplier()
    this.fetchSite()
    this.fetchTransit()
    this.fetchDetail()
  },
  methods: {
    // 获取供应商列表
    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(detailPostCash(id), (res) => {
          if (res.code == 0) {
            this.formData = res.data
            if (!this.formData.route_type) {
              this.formData.route_type = 'type-01'
            }
          }
        })
      }
    },
    // 提交表单
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          if (this.formData.route_type == 'type-01') {
            this.formData.transit_id = ''
          } else if (this.formData.route_type == 'type-02') {
            this.formData.site_id = ''
          } else if (this.formData.route_type == 'type-03') {
            this.formData.supplier_id = ''
          }
          // 修改
          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>
