<template>
  <div class="app-container">
    <el-form ref="postForm" :rules="formRules" :model="formData" label-width="150px">
      <!-- <el-form-item label="发票号：" prop="sell_num">
        <el-col :span="6">
          <el-input v-model="formData.sell_num" placeholder="请输入发票号"/>
        </el-col>
      </el-form-item> -->
      <el-form-item label="客户单位：" prop="client_id">
        <el-col :span="6">
          <el-select v-model="formData.client_id" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in clientList" 
              :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 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="sell_ton">
        <el-col :span="6">
          <el-input v-model.number="formData.sell_ton" placeholder="请输入吨数"/>
        </el-col>
      </el-form-item>
      <el-form-item label="单价：" prop="sell_fee">
        <el-col :span="6">
          <el-input v-model.number="formData.sell_fee" placeholder="请输入单价"/>
        </el-col>
      </el-form-item> -->
      <el-form-item label="开票时间：" prop="ticket_time">
        <el-col :span="6">
           <el-date-picker
            v-model="formData.ticket_time"
            type="datetime"
            placeholder="请选择开始时间"
            value-format="yyyy-MM-dd HH:mm:ss"/>
        </el-col>
      </el-form-item>
      <el-form-item label="开票金额：" prop="order_fee">
        <el-col :span="6">
          <el-input v-model.number="formData.order_fee" 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 } from '@/utils/validate'
import { getData, createSell  } from '@/api/order/index'

export default {
  name: 'ArticleDetail',
  data() {
    return {
      formData: {
        order_receipt: []
      },
      // 校验规则
      formRules: {
        sell_num: [ checkRqInput ],
        client_id: [ checkRqInput ],
        product_type: [ checkRqInput ],
        variety_num: [ checkRqInput ],
        sell_ton: [ checkRqInput, checkNum ],
        sell_fee: [ checkRqInput, checkNum ],
        ticket_time: [ checkRqInput ],
        order_fee: [ checkRqInput, checkNum ]
      },
      varietyList: [], // 品种列表
      clientList: [], // 供应商列表
    }
  },
  created() {
    this.fetchVariety()
    this.fetchClient()
  },
  methods: {
    // 获取品种列表
    fetchVariety() {
      this.$http(getData("data-01"), (res) => {
        this.varietyList = res.data
      })
    },
    // 获取客户单位列表
    fetchClient() {
      this.$http(getData("data-04"), (res) => {
        this.clientList = res.data
      })
    },
    // 提交表单
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          // 新增
          this.$http(createSell(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>
