<template>
  <div>
    <el-dialog
      :title="!id ? '新增': display ? '详情' : '新增或修改'"
      width="50%"
      :close-on-click-modal="false"
      :visible.sync="visible">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
        <el-form-item label="物品名称" prop="name">
          <el-input v-model="dataForm.name" :disabled="display" placeholder="物品名称"></el-input>
        </el-form-item>
        <el-form-item label="型号及规格" prop="specification">
          <el-input v-model="dataForm.specification" :disabled="display" placeholder="型号及规格"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="cnt">
          <el-input-number v-model="dataForm.cnt" :disabled="display" :min="1"/>
        </el-form-item>
        <el-form-item label="单位" prop="unitName">
          <el-input v-model="dataForm.unitName" :disabled="display" placeholder="单位"></el-input>
        </el-form-item>
        <el-form-item label="不含税单价" prop="price">
          <el-input-number v-model="dataForm.price" :disabled="display" :precision="1" :step="1" :min="0"/>
        </el-form-item>
        <el-form-item label="含税单价" prop="taxPrice">
          <el-input-number v-model="dataForm.taxPrice" :disabled="display" :precision="1" :step="1" :min="0"/>
        </el-form-item>
        <el-form-item label="含税总价" prop="taxAmount">
          <span>{{ (dataForm.cnt*dataForm.taxPrice).toFixed(2) }}</span>
        </el-form-item>
        <el-form-item label="税率" prop="taxRateVal">
          <el-input-number :step="1" :min="0" v-model="dataForm.taxRateVal" :disabled="display"/>
          %
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input type="textarea" v-model="dataForm.notes" :disabled="display" placeholder="备注"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!display" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'add-material',
    data () {
      return {
        visible: false,
        display: false,
        dataList: [],
        transferData: {},
        dataForm: {},
        id: 0,
        taxRateVal: 0,
        dataRule: {
          name: [{ required: true, message: '物品名称不能为空', trigger: 'blur' }],
          specification: [{ required: true, message: '型号及规格不能为空', trigger: 'blur' }],
          unitName: [{ required: true, message: '单位不能为空', trigger: 'blur' }]
        }
      }
    },
    methods: {
      async init (id, display, transferData) {
        this.dataForm = {
          cnt: 1,
          price: 0,
          taxPrice: 0,
          taxRateVal: 0
        }
        this.visible = true
        this.id = id || Math.round(Math.random() * 1000000)
        this.display = display
        if (!id) return
        this.dataForm = transferData
        this.dataForm.taxRateVal = this.dataForm.taxRate ? (Number(this.dataForm.taxRate * 100)).toFixed(0) : 0
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataForm.detailId = this.id
            this.dataForm.taxAmount = (this.dataForm.cnt * this.dataForm.taxPrice).toFixed(2)
            this.dataForm.taxRate = this.dataForm.taxRateVal ? (Number(this.dataForm.taxRateVal / 100)).toFixed(2) : 0
            this.visible = false
            this.$emit('addItem', this.dataForm)
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
