<template>
  <work-form-base :editable="editable" ref="baseForm" title="上报保养">
    <ui-form ref="form" :model="formData" :editable="editable" :rules="formRules" label-position="top">
      <ui-form-item prop="vehicleNum" label="保养车辆">
        <ui-select
          v-model="formData.vehicleNum"
          :remote="true"
          :editable="editable"
          :filterable="true"
          text-field="vehicleNum"
          value-field="vehicleNum"
          placeholder="请选择维修车辆"
          :query-method="handleQueryVehicleData"
        ></ui-select>
      </ui-form-item>
      <ui-form-item prop="reportType" label="保养类型">
        <ui-select
          :editable="editable"
          v-model="formData.reportType"
          :query-method="handleQueryTypeOptions"
          placeholder="请选择维修类型"
          text-field="text"
          value-field="value"
        ></ui-select>
      </ui-form-item>
      <ui-form-item label="问题描述">
        <ui-textarea
          :editable="editable"
          v-model="formData.describes"
          placeholder="请输入问题描述"
        ></ui-textarea>
      </ui-form-item>
      <ui-form-item label="照片（0/3）">
        <ui-upload
          v-model="formData.picList"
          :editable="editable"
          type="card"
        ></ui-upload>
      </ui-form-item>
      <ui-toolbar v-if="editable">
        <ui-button type="primary" :click="handleConfirmClick">确定</ui-button>
      </ui-toolbar>
      <template v-else>
        <bi-operate-log
          :data="operateLogData"
          :send-comment-method="handleSendComment"
        ></bi-operate-log>
      </template>
    </ui-form>
  </work-form-base>
</template>
<script>
import WorkFormBase from './report-work-form-base'
import { queryVehicleData, loadWorkflowTypeOptions, createWorkflowComment } from '../apis'

export default {
  name: 'WorkFormMaintain',
  components: {
    WorkFormBase
  },
  props: {
    data: {
      type: Object
    },
    editable: {
      type: Boolean,
      default: true
    },
    success: {
      type: Function
    }
  },
  data() {
    return {
      formData: {
        reportType: null,
        describes: null, // 原因描述',
        vehicleNum: null, // 车辆牌照',
        chassis_kilometers: null, // '保养时底盘公里数',
        sum_volume: null, // '保养时累计方量',
        picList: []
      }
    }
  },
  computed: {
    formRules() {
      return {
        vehicleNum: [{
          required: true,
          message: '保养车辆不能为空',
          trigger: 'change'
        }],
        reportType: [{
          required: true,
          message: '保养类型不能为空',
          trigger: 'change'
        }]
      }
    },
    operateLogData() {
      return (this.formData ? this.formData.operLogs || [] : []).map(v => {
        return {
          name: v.operatorName,
          content: v.operationContent,
          date: v.createTime
        }
      })
    }
  },
  watch: {
    data() {
      this.syncFormData()
    }
  },
  created() {
    this.syncFormData()
  },
  methods: {
    syncFormData() {
      if (this.data) {
        this.formData = JSON.parse(JSON.stringify(this.data))
      }
    },
    handleConfirmClick() {

    },
    handleQueryVehicleData({ keyword }) {
      return queryVehicleData(keyword || '')
    },
    handleQueryTypeOptions() {
      return loadWorkflowTypeOptions()
    },
    handleSendComment(evt) {
      return createWorkflowComment({
        id: this.formData.id,
        solveContent: evt.content
      }).then(() => {
        return Promise.all(this.success ? [this.success()] : null).finally(() => {
          this.$success('操作成功')
        })
      })
    }
  }
}
</script>
