<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="编号"
                                        :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-select
                                                v-decorator="['type', {rules: [{required: true, message: '类别不能为空'}]}]"
                                                placeholder="请选择">
                                                                                        <a-select-option
                                                    v-for="(label,value) in typeMap"
                                                    :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
                                                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="['stock', {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 {addSupplierGoodsList, updateSupplierGoodsList } from '@/api/purchase/supplier-goods-list'

    export default {
        name: 'BaseSupplierGoodsList',
        data() {
            return {
                confirmLoading: false,
                modalTitle: null,
                form: this.$form.createForm(this),
                visible: false,
            // 下拉框map
                            typeMap:{}
            ,
                            unitMap:{}
            ,
                    }
        },
        props: {},
        created() {
            // 下拉框map
                            this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SUPPLIER_GOODS_LIST_TYPE)
                            this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SUPPLIER_GOODS_LIST_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',
                                                                                                                'no',
                                                                                                                'type',
                                                                                                                'name',
                                                                                                                'specs',
                                                                                                                'unit',
                                                                                                                'price',
                                                                                                                'stock',
                                                                                                                '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)) {
                        addSupplierGoodsList(values)
                                .then(() => {
                                    this.handleCancel(values)
                                }).catch(() => {
                            this.confirmLoading = false
                        })
                    } else {
                        updateSupplierGoodsList(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>
