<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">
      <a-form-item v-show="false" >
        <a-input v-decorator="['id']" type="hidden"/>
        <a-input v-decorator="['repairId']" type="hidden"/>
      </a-form-item>
      <a-form-item
        label="维修内容"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-textarea
          rows="8"
          v-decorator="['name', {rules: [{required: true, message: '维修内容不能为空'}]}]" />
      </a-form-item>
<!--      <a-form-item
        label="编码"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['no']" />
      </a-form-item>
      <a-form-item
        label="维修类别"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['type']" />
      </a-form-item>
      <a-form-item
        label="维修工艺"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['technology']" />
      </a-form-item>
      <a-form-item
        label="标准工时"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['standardHours']" />
      </a-form-item>
      <a-form-item
        label="标准费用"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['standardMoney']" />
      </a-form-item>
      <a-form-item
        label="考核工时"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['checkHours']" />
      </a-form-item>
      <a-form-item
        label="排序"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input-number
          style="width: 100%"
          :min="1"
          v-decorator="['sort', {rules: [{required: true, message: '排序不能为空'}]}]" />
      </a-form-item>
      <a-form-item
        label="助记码"
        :labelCol="BaseTool.Constant.labelCol"
        :wrapperCol="BaseTool.Constant.wrapperCol"
      >
        <a-input
          v-decorator="['remark', {rules: [{required: false, message: '助记码不能为空'}]}]" />
      </a-form-item>-->
    </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 { addRepairProject, updateRepairProject } from '@/api/repair/repairproject'

export default {
  name: 'BaseRepairProject',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      sbTypeTreeData: [],
      // 下拉框map
      typeMap: {},
      delFlagMap: {},
      treeData: [],
      modelList: [],
      repairProjectMap: {},
      repairTechnologyMap: {}
    }
  },
  props: {
  },
  created () {
    // 下拉框map
    this.repairProjectMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_PROJECT_TYPE)
    this.repairTechnologyMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_TECHNOLOGY_TYPE)
  },
  methods: {
    base (record) {
      this.setTree(record)
      this.visible = true
      // 如果是空标识添加
      const { form: { setFieldsValue } } = this
      if (this.BaseTool.Object.isBlank(record.id)) {
        this.modalTitle = '添加'
        this.$nextTick(() => {
          setFieldsValue(Object.assign(pick(record, [
            'repairId'
          ])))
        })
        return
      }
      this.modalTitle = '编辑'
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'no',
          'repairId',
          'name',
          'type',
          'technology',
          'standardHours',
          'standardMoney',
          'checkHours',
          'sort',
          'remark'
        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        if (this.BaseTool.String.isBlank(values.id)) {
          addRepairProject(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateRepairProject(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)
      }
    },
    /**
     * 设置备件类别树
     */
    setTree (record = {}) {
    }

  }
}
</script>
