<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel"
  >
    <a-form :form="form">
      <row-list :col="1" v-show="false">
        <row-item>
          <a-form-item>
            <a-input v-decorator="['id']" type="hidden"/>
          </a-form-item>
        </row-item>
      </row-list>
      <row-list :col="2">
        <row-item>
          <a-form-item
            label="设备名称"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.sbName }}</label>
          </a-form-item>
        </row-item>

        <row-item>
          <a-form-item
            label="设备编号"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.sbNo }}</label>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="仓库名称"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.storeName }}</label>
          </a-form-item>
        </row-item>

        <row-item>
          <a-form-item
            label="申请油量"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ BaseTool.Amount.formatter(model.applyOil) }}升</label>
          </a-form-item>
        </row-item>

        <row-item>
          <a-form-item
            label="燃油类型"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.spareName }}</label>
          </a-form-item>
        </row-item>

        <row-item v-show="false">
          <a-form-item
            label="工程项目"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.projectQuantityName }}</label>
          </a-form-item>
        </row-item>

        <row-item>
          <a-form-item
            label="用途"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.reason }}</label>
          </a-form-item>
        </row-item>

        <row-item>
          <a-form-item
            label="申请说明"
            class="marginBottom"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <label>{{ model.applyRemark }}</label>
          </a-form-item>
        </row-item>

        <a-divider />
        <row-item>
          <a-form-item
            label="实际加油量"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input-number
              style="width: 80%"
              :min="0"
              :step="0.01"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              v-decorator="['realOil', {initialValue: model.realOil||model.applyOil,rules: [{required: true, message: '实际加油量不能为空'}]}]"/>
            <span class="ant-input-group-addon" style="display: inline-block; width: 20%;height: 31px;line-height: 31px;margin-bottom: 6px;">升</span>
          </a-form-item>
        </row-item>
      </row-list>
      <row-list :col="1">
        <row-item>
          <a-form-item
            label="说明"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea
              :rows="4"
              v-decorator="['handleRemark',{initialValue: model.remark}]"/>
          </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 { updateOilSbOil } from '@/api/sb/oil'

export default {
  name: 'BaseSbOil',
  components: {
  },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      status: 2,
      model: {},
      visible: false,
      // 下拉框map
      statusMap: {}
    }
  },
  props: {},
  created () {
    // 下拉框map
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_OIL_STATUS)
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record.id)) {
        this.modalTitle = '添加'
      } else {
        this.modalTitle = '填报'
      }
      const { form: { setFieldsValue } } = this
      this.model = record
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id'
        ])))
      })
    },
    statusChange (val) {
      console.log(val)
      this.status = val
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          console.log(44444, values)
          this.confirmLoading = false
          return
        }
        console.log(33333, values)
        updateOilSbOil(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>
