<template>
    <a-modal
            :title="modalTitle"
            :width="800"
            :visible="visible"
            :confirmLoading="confirmLoading"
                    class="ant-modal2"
                    @cancel="handleCancel"
    >
        <a-form :form="form">

            <a-form-item v-show="false">
                <a-input v-decorator="['id']" type="hidden"/>
            </a-form-item>

                            <row-list :col="2">
                                                                                                                    <row-item>

                                <a-form-item
                                        label="供应商主键"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['supplierId', {rules: [{required: true, message: '供应商主键不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="供应商名称"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['supplierName', {rules: [{required: true, message: '供应商名称不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="项目ID"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['projectId', {rules: [{required: true, message: '项目ID不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="项目名称"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['projectName', {rules: [{required: true, message: '项目名称不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="公司ID"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['companyId', {rules: [{required: true, message: '公司ID不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="公司名称"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['companyName', {rules: [{required: true, message: '公司名称不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="采购单ID"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['orderId', {rules: [{required: true, message: '采购单ID不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="采购合同ID"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['contractId', {rules: [{required: true, message: '采购合同ID不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="编号"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['no', {rules: [{required: true, message: '编号不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="类别"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="名称"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['name', {rules: [{required: true, message: '名称不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="规格"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['specs', {rules: [{required: true, message: '规格不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="单位"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                                                                <a-select
                                                v-decorator="['unit', {rules: [{required: true, message: '单位不能为空'}]}]"
                                                placeholder="请选择">
                                                                                        <a-select-option
                                                    v-for="(label,value) in unitMap"
                                                    :key="value"
                                                    :label="label"
                                                    :value="parseInt(value)">{{ label }}
                                            </a-select-option>
                                        </a-select>
                                                                    </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="单价"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                            <a-input-number
                                                style="width: 100%"
                                                :min="0"
                                                :step="0.01"
                                                :formatter="BaseTool.Amount.formatter"
                                                :parser="BaseTool.Amount.parser"
                                                v-decorator="['price', {rules: [{required: true, message: '单价不能为空'}]}]"/>
                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="数量"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                            <a-input-number
                                                style="width: 100%"
                                                :min="0"
                                                :step="0.01"
                                                :formatter="BaseTool.Amount.formatter"
                                                :parser="BaseTool.Amount.parser"
                                                v-decorator="['quantity', {rules: [{required: true, message: '数量不能为空'}]}]"/>
                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="总价"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                            <a-input-number
                                                style="width: 100%"
                                                :min="0"
                                                :step="0.01"
                                                :formatter="BaseTool.Amount.formatter"
                                                :parser="BaseTool.Amount.parser"
                                                v-decorator="['total', {rules: [{required: true, message: '总价不能为空'}]}]"/>
                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="删除标志"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                                                                                                                                                                                                                                                    </row-list>
                    </a-form>
        <template slot="footer">
            <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
        </template>
    </a-modal>
</template>

<script>
    import pick from 'lodash.pick'
    import {addSupplierGoodsRecord, updateSupplierGoodsRecord } from '@/api/purchase/supplier-goods-record'

    export default {
        name: 'BaseSupplierGoodsRecord',
        data() {
            return {
                confirmLoading: false,
                modalTitle: null,
                form: this.$form.createForm(this),
                visible: false,
            // 下拉框map
                            unitMap:{}
            ,
                    }
        },
        props: {},
        created() {
            // 下拉框map
                            this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SUPPLIER_GOODS_RECORD_UNIT)
                    },
        methods: {
            base(record) {
                this.visible = true
                // 如果是空标识添加
                if (this.BaseTool.Object.isBlank(record)) {
                    this.modalTitle = '添加'
                    return
                }
                this.modalTitle = '编辑'
                const {form: {setFieldsValue}} = this
                // 日期处理
                                this.$nextTick(() => {
                    setFieldsValue(Object.assign(pick(record, [
                                                                                    'id',
                                                                                                                'supplierId',
                                                                                                                'supplierName',
                                                                                                                'projectId',
                                                                                                                'projectName',
                                                                                                                'companyId',
                                                                                                                'companyName',
                                                                                                                'orderId',
                                                                                                                'contractId',
                                                                                                                'no',
                                                                                                                'type',
                                                                                                                'name',
                                                                                                                'specs',
                                                                                                                'unit',
                                                                                                                'price',
                                                                                                                'quantity',
                                                                                                                'total',
                                                                                                                'delFlag',
                                                                                                                                                                                                                                                                                                                                                                            ])))
                })
            },
            save() {
                const {form: {validateFieldsAndScroll}} = this
                this.confirmLoading = true
                validateFieldsAndScroll((errors, values) => {
                    if (errors) {
                        this.confirmLoading = false
                        return
                    }
                    // 日期处理
                                        if (this.BaseTool.String.isBlank(values.id)) {
                        addSupplierGoodsRecord(values)
                                .then(() => {
                                    this.handleCancel(values)
                                }).catch(() => {
                            this.confirmLoading = false
                        })
                    } else {
                        updateSupplierGoodsRecord(values)
                                .then(() => {
                                    this.handleCancel(values)
                                }).catch(() => {
                            this.confirmLoading = false
                        })
                    }
                })
            },
            handleCancel(values) {
                this.visible = false
                this.confirmLoading = false
                this.form.resetFields()
                if (this.BaseTool.Object.isNotBlank(values)) {
                    this.$emit('ok', values)
                }
            }

        }
    }
</script>
