<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :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>
<!--      <a-form-item label="计划id" v-show="false">
        <a-input
          v-decorator="['planId', {rules: [{required: true, message: '计划id不能为空'}]}]"/>
      </a-form-item>
      <a-form-item label="设备id" v-show="false">
        <a-input
          v-decorator="['sbId', {rules: [{required: true, message: '设备id不能为空'}]}]"/>
      </a-form-item>
      <a-form-item label="部件id" v-show="false">
        <a-input
          v-decorator="['partId', {rules: [{required: false, message: '部件id不能为空'}]}]"/>
      </a-form-item>-->
      <a-form-item label="标准id" v-show="false">
        <a-input
          v-decorator="['standardId', {rules: [{required: true, message: '标准id不能为空'}]}]"/>
      </a-form-item>
      <!-- <a-form-item label="负责人id" v-show="false">
        <a-input
          v-decorator="['checkUserId', {rules: [{required: false, message: '计划id不能为空'}]}]"/>
      </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="['name', {rules: [{required: true, message: '负责人不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="计划名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              v-decorator="['planName', {rules: [{required: true, message: '点计划名称不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="计划单号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              v-decorator="['planNo', {rules: [{required: true, message: '计划单号不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="负责人"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              v-decorator="['checkUserName', {rules: [{required: true, message: '负责人不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="设备名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              v-decorator="['sbName', {rules: [{required: true, message: '设备名称不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="执行日期"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-date-picker
              showTime
              disabled
              style="width: 100%"
              :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
              v-decorator="['startTime', {rules: [{required: true, message: '执行日期不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="截至日期"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-date-picker
              showTime
              disabled
              style="width: 100%"
              :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
              v-decorator="['endTime', {rules: [{required: true, message: '截至日期不能为空'}]}]"/>
          </a-form-item>
        </row-item>
        <row-item>

          <a-form-item
            label="实际开始时间"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-date-picker
              showTime
              style="width: 100%"
              :format="BaseTool.Date.PICKER_NORM_DATETIME_PATTERN"
              v-decorator="['actualStartTime']"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="实际结束时间"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-date-picker
              showTime
              style="width: 100%"
              :format="BaseTool.Date.PICKER_NORM_DATETIME_PATTERN"
              v-decorator="['actualEndTime']"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="是否停机"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-decorator="['sbStatus', {initialValue:1,rules: [{required: true, message: '是否停机不能为空'}]}]"
              placeholder="请选择">
              <a-select-option
                v-for="(label,value) in sbStatusMap"
                :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.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              disabled
              v-decorator="['status']"
              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 :col="1">
        <!--<row-item>
          <a-form-item
            label="要求"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea
              disabled
              :rows="4"
              v-decorator="['requirement']"/>
          </a-form-item>
        </row-item>
        <row-item>
          <a-form-item
            label="备注"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea
              disabled
              :rows="4"
              v-decorator="['remark']"/>
          </a-form-item>
        </row-item>-->
        <row-item>
          <a-form-item
            label="完成图片"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-upload
              :action="uploadUrl"
              :multiple="true"
              list-type="picture"
              accept="image/*"
              :file-list="this.defaultCheckImgList"
              @change="handleCheckImgChange"
              :headers="headers"
            >
              <a-button> <a-icon type="upload" /> 选择上传图片 </a-button>
            </a-upload>
          </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="['feedback', {rules: [{required: true, message: '检查结果不能为空'}]}]"/>
          </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 { addCheckJob, finishJob } from '@/api/check/checkjob'
import { uploadUrl } from '@/api/upms/file'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'

export default {
  name: 'BaseCheckJob',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      statusMap: {},
      sbStatusMap: {},
      checkImgList: [],
      defaultCheckImgList: [],
      uploadUrl: uploadUrl,
      headers: {
        Authorization: 'Bearer ' + Vue.ls.get(ACCESS_TOKEN)
      }
    }
  },
  props: {
    /**
     * 检查类型： 1-负责 2-巡检
     */
    checkType: {
      type: Number,
      default: 1
    }
  },
  created () {
    // 下拉框map
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CHECK_JOB_STATUS)
    this.sbStatusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.YES_NO)
  },
  methods: {
    base (record) {
      this.visible = true
      // 如果是空标识添加
      console.log(record, 87878)
      if (this.BaseTool.Object.isBlank(record)) {
        this.modalTitle = '添加'
        return
      }
      this.modalTitle = '编辑'
      this.defaultCheckImgList = this.BaseTool.UPLOAD.transImg(this.imgList)
      const { form: { setFieldsValue } } = this
      // 日期处理
      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)
      record.actualStartTime = this.BaseTool.Moment(record.actualStartTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
      record.actualEndTime = this.BaseTool.Moment(record.actualEndTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'standardId',
          'feedback'
          /* 'name',
          'planId',
          'planNo',
          'planName',
          'sbId',
          'sbName',
          'partId',
          'partName',
          'checkUserId',
          'checkUserName',
          'startTime',
          'endTime',
          'status',
          'sbStatus',
          'requirement',
          'remark' */
        ])))
        if (record.actualStartTime.isValid()) {
          setFieldsValue(Object.assign(pick(record, [
            'actualStartTime'
          ])))
        }
        if (record.actualEndTime.isValid()) {
          setFieldsValue(Object.assign(pick(record, [
            'actualEndTime'
          ])))
        }
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        values.startTime = this.BaseTool.Date.formatter(values.startTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
        values.endTime = this.BaseTool.Date.formatter(values.endTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
        values.actualStartTime = this.BaseTool.Date.formatter(values.actualStartTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
        values.actualEndTime = this.BaseTool.Date.formatter(values.actualEndTime, this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN)
        // 上传文件
        values.imgList = this.checkImgList
        if (this.BaseTool.String.isBlank(values.id)) {
          addCheckJob(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          finishJob(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)
      }
    },
    handleCheckImgChange (info) {
      this.defaultCheckImgList = info.fileList
      this.checkImgList = this.setFileList(info, 23)
    },
    setFileList (info, type) {
      const file = info.file
      const fileList = info.fileList
      if (file.status === 'done') {
        return this.BaseTool.UPLOAD.getUploadFileDTO(fileList, type)
      } else if (file.status === 'removed') {
        return this.BaseTool.UPLOAD.getUploadFileDTO(fileList, type)
      } else if (file.status === 'error') {
        this.$message.error('上传失败')
        return null
      }
    }

  }
}
</script>
