<template>
  <div class="app-container">
    <el-form ref="postForm" :rules="formRules" :model="formData" label-width="150px">
      <!-- <el-form-item label="发票号：" prop="input_num">
        <el-col :span="6">
          <el-input v-model="formData.input_num" 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="input_fee">
        <el-col :span="6">
          <el-input v-model.number="formData.input_fee" placeholder="请输入开票金额"/>
        </el-col>
      </el-form-item>
      <!-- <el-form-item label="税额：" prop="input_tax">
        <el-col :span="6">
          <el-input v-model.number="formData.input_tax" placeholder="请输入税额"/>
        </el-col>
      </el-form-item>
      <el-form-item label="含税金额：" prop="tax_fee">
        <el-col :span="6">
          <el-input v-model.number="formData.tax_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="product_list">
        <el-col :span="20">
          <el-button type="primary" size="mini" @click="dialogNew">新增产品</el-button>
          <el-table :data="formData.product_list" border style="width: 100%;" size="mini">
            <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="product_ton" align="center"></el-table-column>
            <el-table-column label="单价" prop="product_fee" align="center"></el-table-column>
            <el-table-column label="操作" align="center" width="180">
              <template slot-scope="scope">
                <el-button class="ml-10" type="primary" size="mini" @click="handleAlter(scope.row)"> 修改 </el-button>
                <el-button class="ml-10" size="mini" type="primary" @click="handleDelete(scope.$index)"> 删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </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>
    <!-- 录入信息弹窗 -->
    <el-dialog :title="flag?'修改产品':'新增产品'" :visible.sync="dialog" center width="400px" @close="closeDialog">
      <el-form :model="entryInfo" label-width="100px" :rules="formRules" ref="postForm2" >
        <el-form-item label="产品类型：" prop="product_type">
          <el-select v-model="entryInfo.product_type" placeholder="请选择" clearable>
            <el-option label="散装" value="type-00"></el-option>
            <el-option label="袋装" value="type-01"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品种编号：" prop="variety_num">
          <el-select v-model="entryInfo.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-form-item>
        <el-form-item label="出厂数量(吨)：" prop="product_ton">
          <el-input v-model.number="entryInfo.product_ton" placeholder="请输入出厂吨数"/>
        </el-form-item>
        <el-form-item label="单价：" prop="product_fee">
          <el-input v-model.number="entryInfo.product_fee" placeholder="请输入单价"/>
        </el-form-item>
      </el-form>
      <el-button style="margin-left:140px" type="primary" @click="addList">{{flag?'修改':'添加'}}</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { checkRqInput, checkNum } from '@/utils/validate'
import { getData, createInput  } from '@/api/order/index'

export default {
  name: 'ArticleDetail',
  data() {
    return {
      list: [], 
      formData: {
        product_list: []
      },
      // 校验规则
      formRules: {
        input_num: [ checkRqInput ],
        supplier_id: [ checkRqInput ],
        input_fee: [ checkRqInput, checkNum ],
        input_tax: [ checkRqInput, checkNum ],
        tax_fee: [ checkRqInput, checkNum ],
        ticket_time: [ checkRqInput ],
        product_list: [ checkRqInput ],
        product_type: [ checkRqInput ],
        variety_num: [ checkRqInput ],
        product_ton: [ checkRqInput, checkNum ],
        product_fee: [ checkRqInput, checkNum ],
      },
      dialog: false,
      entryInfo: {},  // 录入数据
      flag: false,  // 标记是修改还是新增
      varietyList: [], // 品种列表
      supplierList: [], // 供应商列表
      keeyMag: {}, // 中间存储数据
    }
  },
  created() {
    this.fetchVariety()
    this.fetchSupplier()
  },
  methods: {
    // 获取品种列表
    fetchVariety() {
      this.$http(getData("data-01"), (res) => {
        this.varietyList = res.data
      })
    },
    // 获取供应商列表
    fetchSupplier() {
      this.$http(getData("data-02"), (res) => {
        this.supplierList = res.data
      })
    },
    dialogNew() {
      this.flag = false
      this.dialog = true
      this.entryInfo = {}
    },
    // 新增
    addList() {
      this.$refs['postForm2'].validate((valid) => {
        if (valid) {
          if (this.flag) {
            this.dialog = false
            return ''
          }
          if (valid) {
            let obj =  Object.assign({}, this.entryInfo)
            this.formData.product_list.push(obj)
            this.dialog = false
          } else {
            return false
          }
        }else {
          return false
        }
      })
    },
    // 修改
    handleAlter(row) {
      this.flag = true
      this.dialog = true
      this.entryInfo = row
      // this.keeyMag =  Object.assign({}, this.entryInfo)
      for( var key in this.entryInfo ){
        this.keeyMag [key]=this.entryInfo[key]
      }
    },
    handleDelete(index) {
      this.$confirm('删除', `确认删除吗`, () => {
        this.formData.product_list.splice(index, 1)
      })
    },
    // 配送产品
    getProductType(value) {
      return value=="type-00"?"散装":(value=="type-01"?"袋装":"")
    },
    // 提交表单
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          // 新增
          this.$http(createInput(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)
    },
    // 关闭弹窗
    closeDialog() {
      for( var key in this.keeyMag ){
        this.entryInfo [key]=this.keeyMag[key]
      }
    },
  }
}
</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>
