<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-button type="primary" @click="save()">提交</a-button>
          <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-input v-decorator="['purchaseApplyId']" 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="['companyName']"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="项目名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled="disabled"
              v-decorator="['projectName']"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="采购员"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-decorator="['purchaserId']"
              placeholder="请选择">
              <a-select-option
                v-for="item in users"
                :key="item.userId"
                :label="item.realName"
                :value="item.userId">{{ item.realName }}
              </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-textarea
              v-decorator="['remark']"/>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <title-divider title="采购单明细" width="100px"></title-divider>
    <purchase-apply-order-list ref="applyOrderList"/>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import { addPurchaseOrder, updatePurchaseOrder } from '@/api/purchase/purchase-order'
import { queryUser } from '@/api/upms/user'
import PurchaseApplyOrderList from '../PurchaseApplyOrderList'

export default {
  name: 'BasePurchaseOrder',
  components: { PurchaseApplyOrderList },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      users: []

    }
  },
  props: {},
  created () {
    // 下拉框map
  },
  methods: {
    base (record, apply, purchaseApplyId) {
      this.BaseTool.ListForm.clearOneList(this)
      this.getUsers()
      this.visible = true
      const { form: { setFieldsValue } } = this
      const modal = this.$refs.applyOrderList
      this.$store.dispatch('setPurchaseApplyId', purchaseApplyId)
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.$store.dispatch('setAddFlag', true)
        this.addFlag = true
        this.modalTitle = '添加采购单'
        this.$nextTick(() => {
          setFieldsValue({
            companyName: apply.companyName,
            projectName: apply.projectName,
            purchaseApplyId: purchaseApplyId
          })
        })
        modal.base('-1')
        return
      }
      this.$store.dispatch('setAddFlag', false)
      modal.base(record.id)
      this.addFlag = false
      this.modalTitle = '编辑采购单'
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'companyName',
          'projectName',
          'purchaserId',
          'purchaseApplyId',
          'remark'
        ])))
      })
    },
    getUsers () {
      queryUser({ identityType: this.DictCache.VALUE.SYS_USER_IDENTITY_TYPE.CGY }).then(res => {
        this.users = res.data
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        values.demandDate = this.BaseTool.Date.formatter(values.demandDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        this.BaseTool.ListForm.setOneListValues(this, values)
        if (this.addFlag) {
          addPurchaseOrder(values)
            .then(() => {
              this.handleCancel(values)
              this.BaseTool.ListForm.clearOneList(this)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updatePurchaseOrder(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)
    }

  }
}
</script>
