<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="采购申请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="['purchasePlanId', {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="['demandPlanId', {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="['purchaseOrderId', {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="['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="['projectId', {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="['money', {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="['moneyType', {rules: [{required: true, message: '币种不能为空'}]}]"
                                                placeholder="请选择">
                                                                                        <a-select-option
                                                    v-for="(label,value) in moneyTypeMap"
                                                    :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-select
                                                v-decorator="['feeType', {rules: [{required: true, message: '费用类别不能为空'}]}]"
                                                placeholder="请选择">
                                                                                        <a-select-option
                                                    v-for="(label,value) in feeTypeMap"
                                                    :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-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="['exchangeRate', {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="['accountMoney', {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="['dollarExchangeRate', {rules: [{required: true, message: '汇率不能为空'}]}]"/>
                                                                                                        </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 {addPurchaseDispatchFee, updatePurchaseDispatchFee } from '@/api/purchase/purchase-dispatch-fee'

    export default {
        name: 'BasePurchaseDispatchFee',
        data() {
            return {
                confirmLoading: false,
                modalTitle: null,
                form: this.$form.createForm(this),
                visible: false,
            // 下拉框map
                            moneyTypeMap:{}
            ,
                            feeTypeMap:{}
            ,
                    }
        },
        props: {},
        created() {
            // 下拉框map
                            this.moneyTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_DISPATCH_FEE_MONEY_TYPE)
                            this.feeTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_DISPATCH_FEE_FEE_TYPE)
                    },
        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',
                                                                                                                'purchaseApplyId',
                                                                                                                'purchasePlanId',
                                                                                                                'demandPlanId',
                                                                                                                'purchaseOrderId',
                                                                                                                'companyId',
                                                                                                                'projectId',
                                                                                                                'money',
                                                                                                                'moneyType',
                                                                                                                'feeType',
                                                                                                                'delFlag',
                                                                                                                                                                                                                                                                                                                                                                                                                                        'exchangeRate',
                                                                                                                'accountMoney',
                                                                                                                'dollarExchangeRate',
                                                    ])))
                })
            },
            save() {
                const {form: {validateFieldsAndScroll}} = this
                this.confirmLoading = true
                validateFieldsAndScroll((errors, values) => {
                    if (errors) {
                        this.confirmLoading = false
                        return
                    }
                    // 日期处理
                                        if (this.BaseTool.String.isBlank(values.id)) {
                        addPurchaseDispatchFee(values)
                                .then(() => {
                                    this.handleCancel(values)
                                }).catch(() => {
                            this.confirmLoading = false
                        })
                    } else {
                        updatePurchaseDispatchFee(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>
