<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-input v-decorator="['mainOrderId']" 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-checkbox
              v-decorator="[ 'contractFlag', { valuePropName: 'checked',initialValue: false, }]" >
            </a-checkbox>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="是否需要发运"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-checkbox
              @change="changeDispatchFlag"
              v-decorator="[ 'dispatchFlag', { valuePropName: 'checked',initialValue: false, }]" >
            </a-checkbox>
          </a-form-item>
        </row-item>

        <row-item>

          <a-form-item
            label="是否需要集装箱"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-checkbox
              :disabled="!dispatchFlag"
              v-decorator="[ 'containerFlag', { valuePropName: 'checked',initialValue: false, }]" >
            </a-checkbox>
          </a-form-item>
        </row-item>

        <row-item>

          <a-form-item
            label="是否需要陆运"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-checkbox
              :disabled="!dispatchFlag"
              v-decorator="[ 'landFlag', { valuePropName: 'checked',initialValue: false, }]" >
            </a-checkbox>
          </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" :chooseFlag="chooseFlag"/>
  </a-card>
</template>

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

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

    }
  },
  props: {},
  created () {
    // 下拉框map
  },
  methods: {
    base (record, order) {
      this.BaseTool.ListForm.clearOneList(this)
      this.chooseFlag = !!order
      this.visible = true
      this.record = record || order
      this.getUsers(this.record.projectId)
      const { form: { setFieldsValue } } = this
      const modal = this.$refs.applyOrderList
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.$store.dispatch('setPurchaseApplyId', order.purchaseApplyId)
        this.$store.dispatch('setAddFlag', true)
        this.addFlag = true
        this.modalTitle = '添加采购单'
        this.dispatchFlag = order.dispatchFlag
        this.$nextTick(() => {
          setFieldsValue({
            companyName: order.companyName,
            projectName: order.projectName,
            mainOrderId: order.id,
            dispatchFlag: order.dispatchFlag,
            contractFlag: order.contractFlag,
            landFlag: order.landFlag,
            containerFlag: order.containerFlag,
            purchaseApplyId: order.purchaseApplyId
          })
        })
        modal.base('-1')
        return
      }
      this.$store.dispatch('setAddFlag', false)
      this.$store.dispatch('setPurchaseApplyId', record.purchaseApplyId)
      modal.base(record.id)
      this.addFlag = false
      this.modalTitle = '编辑采购单'
      this.dispatchFlag = record.dispatchFlag
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'companyName',
          'projectName',
          'dispatchFlag',
          'contractFlag',
          'landFlag',
          'containerFlag',
          'mainOrderId',
          'purchaserId',
          'purchaseApplyId',
          'remark'
        ])))
      })
    },
    // getUsers () {
    //   queryUser({ identityType: this.DictCache.VALUE.SYS_USER_IDENTITY_TYPE.CGY }).then(res => {
    //     this.users = res.data
    //   })
    // },
    getUsers (deptId) {
      queryProjectRole({ deptId: deptId,
        roleCodes: [this.DictCache.VALUE.ROLE_CODE.XMB_CGY,
          this.DictCache.VALUE.ROLE_CODE.FGS_CGY, this.DictCache.VALUE.ROLE_CODE.QYZB_CGY, this.DictCache.VALUE.ROLE_CODE.JT_CGY] }).then(res => {
        this.users = res.data
      })
    },
    changeDispatchFlag (e) {
      const value = e.target.checked
      this.dispatchFlag = value
      if (!value) {
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          setFieldsValue({
            landFlag: false,
            containerFlag: false
          })
        })
      }
    },
    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>
