<template>
  <a-modal
    :title="modalTitle"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel"
  >
    <a-form :form="form">

      <a-form-item v-show="false" >
        <a-input v-decorator="['id']" 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
              v-decorator="['no', {rules: [{required: false, message: '维修单号不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item v-show="false">
          <a-form-item
            label="报修单id"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['applicationId', {rules: [{required: true, message: '报修单id不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="报修单号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              v-decorator="['applicationNo', {rules: [{required: true, message: '报修单号不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="故障类别"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-tree-select
              style="width: 100%"
              :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
              :treeData="treeData"
              :treeNodeFilterProp="'title'"
              :showSearch="true"
              v-decorator="['errorTypeId', {rules: [{required: true, message: '故障类别不能为空'}]}]"
              placeholder="请选择"
            >
            </a-tree-select>
          </a-form-item>
        </row-item>

        <row-item>
          <a-form-item
            label="维修耗时"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              style="width: 100%"
              :min="1"
              type="number"
              addonAfter="分钟"
              v-decorator="['minutes', {initialValue:1, rules: [{required: false, message: '维修耗时不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item v-show="false">
          <a-form-item
            label="维修人员"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['repairPersonId', {rules: [{required: true, message: '维修人员不能为空'}]}]" />
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="更换备件"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-button type="primary" @click="handleAddPartUsed">更换备件</a-button>
          </a-form-item>
        </row-item>
       <!-- <row-item>
          <a-form-item
            label="维修状态"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['status', {initialValue: DictCache.VALUE.REPAIR_FORM_STATUS.NOT_DEAL, rules: [{required: true, message: '维修状态不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="(label,value) in statusMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>-->
      </row-list>
      <row-list>
        <row-item>
          <a-form-item
            label="维修描述"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['descripition', {initialValue: DictCache.VALUE.YES_NO.NO,rules: [{required: true, message: '是否停机不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="(label,value) in descripitionMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>

        <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>



    <!--<a-tabs type="card" default-active-key="1">
      <a-tab-pane key="1" tab="入库详情">
        <div class="table-operator">
          <a-button size="small" type="primary" @click="handleDetailSelect">
            <a-icon type="plus"/>
            添加
          </a-button>
          <a-button class="margin-left8" size="small" type="danger" @click="handleDel">
            <a-icon type="delete"/>
            删除
          </a-button>
        </div>
        <a-table
          bordered
          :data-source="data"
          :columns="columns"
          tableLayout="auto"
          rowKey="id"
          :row-selection="rowSelection">

        </a-table>
      </a-tab-pane>
    </a-tabs>-->
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">完成</a-button>
    </template>
    <repair-application-form-select-modal ref="rafSelectModal" @selected="handleNoSelected"/>
    <error-type-select-modal ref="errorSelectModal" @selected="handleErrorTypeSelected"/>
    <spare-part-used-select-modal ref="sparePartUsedSelectModal" />
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addRepairForm, updateRepairForm } from '@/api/repair/form'
import RepairApplicationFormSelectModal from '@/views/repair/application-form/modules/RepairApplicationFormSelectModal'
import ErrorTypeSelectModal from '@/views/repair/errortype/modules/ErrorTypeSelectModal'
import { fetchErrorTypeTree } from '@/api/repair/errortype'
import RepairFeeTable from '@/views/repair/fee/modules/RepairFeeTable'
import RepairPartUsedTable from '@/views/repair/part-used/modules/RepairPartUsedTable'
import SparePartUsedSelectModal from '@/views/sqarepartmanage/sparepartused/modules/SparePartUsedSelectModal'

export default {
  name: 'BaseRepairForm',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      needStopMap: {},
      statusMap: {},
      descripitionMap:{},
      model: {},
      treeData: []
    }
  },
  components: {
    RepairApplicationFormSelectModal,
    ErrorTypeSelectModal,
    RepairPartUsedTable,
    RepairFeeTable,
    SparePartUsedSelectModal
  },
  props: {
  },
  created () {
    // 下拉框map
    this.needStopMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.YES_NO)
    this.descripitionMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIRE_ACTION)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_FORM_STATUS)
  },
  methods: {
    base (record) {
      this.setTree()
      this.visible = true
      this.model = record
      // 如果是空标识添加
      const { form: { setFieldsValue } } = this
      this.modalTitle = '维修'
      // 日期处理
      // console.log(record.endTime, record.startTime)
      // record.startTime = this.BaseTool.Moment(record.startTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
      // record.endTime = this.BaseTool.Moment(record.endTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'no',
          'applicationId',
          'applicationNo',
          'errorTypeId',
          'errorTypeName',
          'needStop',
          // 'startTime',
          'minutes',
          'repairPersonId',
          'descripition',
          'status',
          'times',
          '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)) {
          addRepairForm(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateRepairForm(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    } ,
    handleAddPartUsed () {
      /* const modal = this.$refs.repairFeeModal
      modal.base(null, record) */
      this.$refs.sparePartUsedSelectModal.base(this.model, { sbId: this.model.sbId, modelId: this.model.modelId })
    } ,
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    } ,
    handleNoSelect () {
      this.$refs.rafSelectModal.base()
    },
    handleNoSelected (record, keys, rows) {
      console.log(keys, rows)
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'applicationId': key,
          'applicationNo': row.no
        }))
      })
    },
    handleErrorTypeSelect () {
      this.$refs.errorSelectModal.base()
    },
    handleErrorTypeSelected (record, keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'errorTypeId': key,
          'errorTypeName': row.name
        }))
      })
    },
    /**
     * 设置类别树
     */
    setTree (record = {}) {
      fetchErrorTypeTree({ id: record.id }).then(res => {
        console.log(res.data)
        this.treeData = res.data
      })
    }
  }
}
</script>
