<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-item>
          <a-form-item>
            <a-input v-decorator="['sbId']" type="hidden"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item>
            <a-input v-decorator="['storeId']" 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-select
              @change="statusChange"
              v-decorator="['status', {initialValue: parseInt(DictCache.VALUE.SB_OIL_STATUS.AGREE),rules: [{required: true, message: '状态不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                :key="2"
                label="同意"
                :value="parseInt(DictCache.VALUE.SB_OIL_STATUS.AGREE)">同意
              </a-select-option>
              <a-select-option
                :key="4"
                label="不同意"
                :value="parseInt(DictCache.VALUE.SB_OIL_STATUS.NOT_AGREE)">不同意
              </a-select-option>
            </a-select>
          </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="['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 { updateAuditSbOil } 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) {
          this.confirmLoading = false
          return
        }
        updateAuditSbOil(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>
