<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="请输入单据编号"/>
        </el-col>
      </el-form-item>
      <el-form-item label="绑定配送车牌号码：" prop="driver_id">
        <el-col :span="6">
          <el-select v-model="formData.driver_id" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in driverList" 
              :key="index"
              :label="item.driver_account" :value="item.data_id" >
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="模式" prop="order_pattern" :rules="formRules.checkRqSelect">
        <el-col :span="6">
          <el-select v-model="formData.order_pattern" placeholder="请选择" clearable>
            <el-option label="直达模式" :value="1"></el-option>
            <el-option label="中转模式" :value="2"></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 label="散装" value="type-00"></el-option>
            <el-option label="袋装" value="type-01"></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_name" >
            </el-option>
          </el-select>
        </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="order_ton">
        <el-col :span="6">
          <el-input v-model="formData.order_ton" 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" :rules="formRules.checkRqSelect" v-if="formData.order_pattern == 2">
        <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="order_receipt">
        <el-col :span="18">
          <uploadImg :img-list="formData.order_receipt" :extent="5" :list="true"/>
        </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 uploadImg from '../../components/common/uploadImg'
import { checkRqInput, checkNum, checkRqSelect } from '@/utils/validate'
import { getData, createDelivery, getTransitList  } from '@/api/order/index'
import { upload  } from '@/api/system/index'
import { baseUrl } from '@/utils/env.js'

export default {
  name: 'ArticleDetail',
  components: {
    uploadImg
  },
  data() {
    return {
      formData: {
        order_pattern: 1,
        order_receipt: []
      },
      // 校验规则
      formRules: {
        checkRqSelect,
        driver_id: [ checkRqInput ],
        product_type: [ checkRqInput ],
        variety_num: [ checkRqInput ],
        over_ton: [ checkRqInput, checkNum ],
        order_ton: [ checkRqInput, checkNum ],
        supplier_id: [ checkRqInput ],
        site_id: [ checkRqInput ],
        order_receipt: [ checkRqInput ],
        delivery_no: [ checkRqInput ],
      },
      driverList: [],  // 司机列表
      varietyList: [], // 品种列表
      supplierList: [], // 供应商列表
      transitList: [], // 中转站列表
      siteList: [], // 工地列表
      baseUrl,
      upload,
    }
  },
  created() {
    this.fetchDriver()
    this.fetchVariety()
    this.fetchSupplier()
    this.fetchSite()
    this.fetchTransit()
  },
  methods: {
    // 获取司机列表
    fetchDriver() {
      this.$http(getData("data-00"), (res) => {
        this.driverList = res.data
      })
    },
    // 获取品种列表
    fetchVariety() {
      this.$http(getData("data-01"), (res) => {
        this.varietyList = res.data
      })
    },
    // 获取供应商列表
    fetchSupplier() {
      this.$http(getData("data-02"), (res) => {
        this.supplierList = res.data
      })
    },
    // 获取中转站列表
    fetchTransit() {
      this.$http(getTransitList(), (res) => {
        this.transitList = res.data
      })
    },
    // 获取工地列表
    fetchSite() {
      this.$http(getData("data-03"), (res) => {
        this.siteList = res.data
      })
    },
    // 提交表单
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          // 新增
          this.$http(createDelivery(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)
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
