<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="['demandPlanId', {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="['purchasePlanId', {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="['purchaseApplyId', {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="['purchaseOrderId', {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-input-number
                                                style="width: 100%"
                                                :min="1"
                                                v-decorator="['type', {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="['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="['integratedPrice', {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="['totalMoney', {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="['taxRate', {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="['taxMoney', {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="['brand', {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="['suitableModel', {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="['vin', {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="['engineNum', {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="['discount', {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="['boughtQuantity', {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="['arrivedQuantity', {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="['notArriveQuantity', {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="['usedMoney', {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="['dispatchedQuantity', {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="['notDispatchQuantity', {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="['producePlace', {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="['weight', {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="['volume', {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="['dimension', {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="['supplierId', {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="['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-number
                                                style="width: 100%"
                                                :min="0"
                                                :step="0.01"
                                                :formatter="BaseTool.Amount.formatter"
                                                :parser="BaseTool.Amount.parser"
                                                v-decorator="['supplyPrice', {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="['supplyTotalMoney', {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="['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="['remark', {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="['status', {rules: [{required: true, message: '状态不能为空'}]}]"
                                                placeholder="请选择">
                                                                                        <a-select-option
                                                    v-for="(label,value) in statusMap"
                                                    :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-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 {addPurchaseList, updatePurchaseList } from '@/api/purchase/purchase-list'

    export default {
        name: 'BasePurchaseList',
        data() {
            return {
                confirmLoading: false,
                modalTitle: null,
                form: this.$form.createForm(this),
                visible: false,
            // 下拉框map
                            unitMap:{}
            ,
                            statusMap:{}
            ,
                    }
        },
        props: {},
        created() {
            // 下拉框map
                            this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_LIST_UNIT)
                            this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_LIST_STATUS)
                    },
        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',
                                                                                                                'demandPlanId',
                                                                                                                'purchasePlanId',
                                                                                                                'purchaseApplyId',
                                                                                                                'purchaseOrderId',
                                                                                                                'no',
                                                                                                                'type',
                                                                                                                'name',
                                                                                                                'specs',
                                                                                                                'unit',
                                                                                                                'price',
                                                                                                                'integratedPrice',
                                                                                                                'quantity',
                                                                                                                'totalMoney',
                                                                                                                'taxRate',
                                                                                                                'taxMoney',
                                                                                                                'brand',
                                                                                                                'suitableModel',
                                                                                                                'vin',
                                                                                                                'engineNum',
                                                                                                                'discount',
                                                                                                                'boughtQuantity',
                                                                                                                'arrivedQuantity',
                                                                                                                'notArriveQuantity',
                                                                                                                'usedMoney',
                                                                                                                'dispatchedQuantity',
                                                                                                                'notDispatchQuantity',
                                                                                                                'producePlace',
                                                                                                                'weight',
                                                                                                                'volume',
                                                                                                                'dimension',
                                                                                                                'supplierId',
                                                                                                                'supplierName',
                                                                                                                'supplyPrice',
                                                                                                                'supplyTotalMoney',
                                                                                                                'contractId',
                                                                                                                'remark',
                                                                                                                'status',
                                                                                                                '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)) {
                        addPurchaseList(values)
                                .then(() => {
                                    this.handleCancel(values)
                                }).catch(() => {
                            this.confirmLoading = false
                        })
                    } else {
                        updatePurchaseList(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>
