<!-- 采购操作 -->
<template>
  <div>
      <div class="my-title">采购</div>
      <!-- 表单 -->
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="auto">
        <el-row class="my-row">
          <el-col :span="12">
            <el-form-item ref="supplierCheckbox" label="供应商" prop="supplierId">
              <supplier-component v-model="dataForm.supplierId" :supplier-id.sync="dataForm.supplierId"></supplier-component>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item ref="supplierInput" label="其他供应商" prop="supplierOther">
              <el-input v-model="dataForm.supplierOther" placeholder="请输入其他供应商"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="my-row">
          <el-col :span="12">
            <el-form-item label="采购依据" prop="procurementBasis">
              <el-select
                v-model="dataForm.procurementBasis"
                placeholder="请选择">
                <el-option
                  v-for="item in optionsPB"
                  :key="item.code"
                  :label="item.value"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="不含税单价" prop="price">
              <el-input-number v-model="dataForm.price" :step="1" :min="0" :precision="1"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="my-row">
          <el-col :span="12">
            <el-form-item label="含税单价" prop="taxPrice">
              <el-input-number v-model="dataForm.taxPrice" :step="1" :min="0" :precision="1"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="含税总价" prop="procurementCode">
              <span>{{ (dataForm.cnt * dataForm.taxPrice).toFixed(1) }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="my-row">
          <el-col :span="12">
            <el-form-item label="税率" prop="taxRate">
              <el-input-number v-model="dataForm.taxRate" :step="1" :precision="1"/>&nbsp;%
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="onChose">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </div>
</template>

<script>
  import SupplierComponent from '../common/supplier-component'
  import { getDictList } from '@/api/dict'
  export default {
    name: 'purchase-operate',
    components: {
      SupplierComponent
    },
    data () {
      let validateSupplier = (rule, value, callback) => {
        if (!this.dataForm.supplierId && !this.dataForm.supplierOther) {
          callback(new Error('请至少填写一项供应商信息'))
        } else {
          // 任意值被填写，清除验证提示
          if (!this.dataForm.supplierId || !this.dataForm.supplierOther) {
            this.$refs.supplierCheckbox.clearValidate()
            this.$refs.supplierInput.clearValidate()
          }
          callback()
        }
      }
      return {
        id: 0,
        dataForm: {
          cnt: 1
        },
        dataRule: {
          supplierId: [{required: true, validator: validateSupplier, trigger: 'change'}],
          supplierOther: [{required: true, validator: validateSupplier, trigger: 'blur'}],
          procurementBasis: [{ required: true, message: '请选择采购依据', trigger: 'change' }],
          price: [{ required: true, message: '请输入不含税单价', trigger: 'change' }],
          taxPrice: [{ required: true, message: '请输入含税单价', trigger: 'change' }],
          taxRate: [{ required: true, message: '请输入税率', trigger: 'change' }]
        },
        optionsPB: [] // 采购依据（字典）
      }
    },
    methods: {
      onChose () {
        this.$emit('onChose')
      },
      // 获取采购依据字典
      getPbList () {
        getDictList({type: 'procurement_basis'}).then(({data}) => {
          if (data) {
            this.optionsPB = data
          }
        })
      },
      async init (row) {
        if (!row) return
        this.getPbList()
        this.dataForm = row
      },
      validateField (type) {
        this.$refs.dataForm.validateField(type)
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            // // 检查供应商
            // if (!this.dataForm.supplierId && !this.dataForm.supplierOther) {
            //   this.$message.error('请填写供应商')
            //   return
            // }
            this.$http({
              url: this.$http.adornUrl(`/biz-service/purchaseDetail/updatePurchaseDetail`),
              method: 'post',
              data: this.$http.adornData({...this.dataForm})
            }).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.onChose()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
