<template>
  <a-card :bordered="false" :title="modalTitle" v-show="visible">
    <a-row :gutter="48" slot="extra">
      <a-col :md="48" :sm="48">
        <span class="table-page-search-submitButtons" style="float: right">
          <a-popconfirm title="确认增补操作过后将无法更改，确认进行该操作吗？" @confirm="save(DictCache.VALUE.PURCHASE_DEMAND_PLAN_STATUS.PURCHASE_DEMAND_PLAN_STATUS_SHE)">
            <a-button type="danger">确认增补</a-button>
          </a-popconfirm>
          <a-button style="margin-left: 8px" @click="handleCancel()">返回</a-button>
        </span>
      </a-col>
    </a-row>
    <title-divider title="基本信息" width="90px"></title-divider>
    <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
              disabled="disabled"
              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-select
              v-decorator="['companyId', { rules: [{required: true, message: '公司不能为空'}]}]"
              placeholder="请选择"
              disabled="disabled"
            >
              <a-select-option
                v-for="item in companies"
                :key="item.deptId"
                :label="item.name"
                :value="item.deptId">{{ item.name }}
              </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="['projectId', { rules: [{required: true, message: '项目不能为空'}]}]"
              placeholder="请选择"
              disabled="disabled"
            >
              <a-select-option
                v-for="item in projects"
                :key="item.deptId"
                :label="item.name"
                :value="item.deptId">{{ item.name }}
              </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="['type', {initialValue: 10,rules: [{required: true, message: '类别不能为空'}]}]"
              disabled="disabled"
              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-number
              disabled="disabled"
              style="width: 100%"
              :min="0"
              :step="0.01"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              v-decorator="['planListControlMoney', {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
              disabled="disabled"
              style="width: 100%"
              :min="0"
              :step="0.01"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              v-decorator="['planListMoney', {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="['planMoney', {rules: [{required: true, message: '资金预算不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="备注"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-textarea
              v-decorator="['remark']"/>
          </a-form-item>
        </row-item>

      </row-list>
    </a-form>
    <title-divider title="需求明细" width="90px"></title-divider>
    <purchase-demand-plan-add-list ref="purchaseDemandPlanList" @ok="updateMoney"></purchase-demand-plan-add-list>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import { addPurchaseDemandPlanAdd } from '@/api/purchase/purchase-demand-plan-add'
import { queryDept } from '@/api/upms/dept'
import PurchaseDemandPlanAddList from '../PurchaseDemandPlanAddList'

export default {
  name: 'BasePurchaseDemandPlanAdd',
  components: { PurchaseDemandPlanAddList },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      companies: [],
      originMoney: 0,
      projects: [],
      projectMap: [],
      // 下拉框map
      typeMap: {}

    }
  },
  props: {},
  created () {
    // 下拉框map
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_TYPE)
    queryDept({ nature: 'FEN_GONG_SI' }).then(res => {
      this.companies = res.data
    })
  },
  methods: {
    base (record) {
      this.BaseTool.ListForm.clearOneList(this)
      this.visible = true
      const { form: { setFieldsValue } } = this
      const modal = this.$refs.purchaseDemandPlanList
      this.$store.dispatch('setPurchaseType', record.type)
      this.originMoney = record.planMoney
      modal.base(record.id, null)
      this.modalTitle = '总需求计划增补'
      this.getProjects(record.companyId)
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'name',
          'companyId',
          'companyName',
          'projectId',
          'projectName',
          'type',
          'planListMoney',
          'planListControlMoney',
          'planMoney',
          'usedMoney',
          'progress',
          'quantity',
          'boughtQuantity',
          'arrivedQuantity',
          'notArriveQuantity',
          'status',
          'remark',
          'delFlag'
        ])))
      })
    },
    getProjects (value) {
      queryDept({ nature: 'XIANG_MU_BU', parentId: value }).then(res => {
        this.projects = res.data
        this.projects.forEach((item) => {
          this.projectMap[item.deptId] = item.name
        })
      })
    },
    save (value) {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        this.BaseTool.ListForm.setOneListValues(this, values)
        values.addMoney = values.planMoney - this.originMoney
        values.submitType = value
        addPurchaseDemandPlanAdd(values)
          .then(() => {
            this.handleCancel(values)
            this.BaseTool.ListForm.clearOneList(this)
          }).catch(() => {
            this.confirmLoading = false
          })
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      this.$emit('ok', values)
    },
    updateMoney (planListControlMoney, planListMoney) {
      const { form: { setFieldsValue } } = this
      setFieldsValue({
        'planListControlMoney': planListControlMoney,
        'planListMoney': planListMoney
      })
    }

  }
}
</script>
