<!-- 委外 -->
<template>
    <div>
        <div class="my-title">委外</div>
        <!-- 表单 -->
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="auto">
          <el-form-item label="供应商" prop="supplierId">
            <supplier-component v-model="dataForm.supplierId" :supplier-id.sync="dataForm.supplierId"></supplier-component>
          </el-form-item>
          <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-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-form-item label="含税总价" prop="taxAmount">
            <span>{{dataForm.cnt * dataForm.taxPrice}}</span>
          </el-form-item>
          <el-form-item label="税率" prop="taxRate">
            <el-input-number style="width: 160px" v-model="dataForm.taxRate" :step="1" :precision="1"/>&nbsp;%
          </el-form-item>
        </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 { updatePurCommissionDetail } from '@/api/sale'
  import SupplierComponent from '../common/supplier-component'

export default {
    name: 'outsource-pop',
    components: {
      SupplierComponent
    },
    computed: {
      orgId: {
        get () { return this.$store.state.user.orgId }
      }
    },
    data () {
      return {
        id: 0,
        dataForm: {
          price: 1,
          taxPrice: 1,
          taxRate: 1,
          cnt: 1
        },
        dataRule: {
          supplierId: [{ required: true, message: '请选择供应商', trigger: 'change' }],
          price: [{ required: true, message: '不含税单价不能为空', trigger: 'change' }],
          taxPrice: [{ required: true, message: '含税单价不能为空', trigger: 'change' }],
          taxRate: [{ required: true, message: '税率不能为空', trigger: 'change' }]
        }
      }
    },
    methods: {
      onChose () {
        this.$emit('onChose')
      },
      async init (id, cnt) {
        this.dataForm.purComDetailId = id
        this.dataForm.cnt = cnt
      },
      validateField (type) {
        this.$refs.dataForm.validateField(type)
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            updatePurCommissionDetail(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>
