<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel"
  >
    <a-form :form="form">
      <row-list :col="2">

        <a-form-item v-show="false" >
          <a-input v-decorator="['id']" type="hidden"/>
        </a-form-item>
        <row-item v-show="!visibleReport">
          <a-form-item
            label="上报处理"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-decorator="['reportHandleType']"
              @change="changeReportFlag"
              placeholder="请选择">
              <a-select-option
                v-for="item in reportHandleTypeMap"
                :key="item.value"
                :label="item.label"
                :value="item.value">{{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
      </row-list>

      <row-list :col="2">
        <row-item v-show="visibleReport|| model.reportHandleType===3">
          <a-form-item
            label="上报对象"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              @change="changePlanFlag"
              v-decorator="['reportLeaderId']"
              placeholder="请选择">
              <a-select-option
                v-for="item in roleUserMap"
                :key="item.userId"
                :label="item.realName"
                :value="item.userId">{{ item.realName }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>

        <row-item v-show="!visibleReport&&model.reportHandleType===1">
          <a-form-item
            label="派工对象"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              @change="changePlanFlag"
              v-decorator="['reportLeaderId']"
              placeholder="请选择">
              <a-select-option
                v-for="item in roleUserMap"
                :key="item.userId"
                :label="item.realName"
                :value="item.userId">{{ item.realName }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </row-item>
      </row-list>
      <row-list :col="1">
        <row-item v-show="model.reportHandleType===4">
          <a-form-item
            label="挂起原因"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea v-decorator="['remark']"/>
          </a-form-item>
        </row-item>
        <row-item v-show="(visibleReport&&model.reportHandleType===2) || (!visibleReport&&model.reportHandleType===3)">
          <a-form-item
            label="上报原因"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea v-decorator="['remark']"/>
          </a-form-item>
        </row-item>
        <row-item v-show="!visibleReport&&model.reportHandleType===1">
          <a-form-item
            label="上报备注"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea disabled="disabled" 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 { reportRepair } from '@/api/repair/application-form'
import { queryRepairUser } from '@/api/upms/user-dept'
import SparePartUsedSelectModal from '@/views/sqarepartmanage/sparepartused/modules/SparePartUsedSelectModal'

export default {
  name: 'ReportUpForm',
  data () {
    return {
      model: null,
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      chooseType: null,
      roleType: null,
      roleUserMap: [],
      // 下拉框map
      reportLeaderId: null,
      visibleReport: false,
      reportHandleTypeMap: [
        {
          label: '派工',
          value: 1
        },
        {
          label: '上报',
          value: 3
        },
        {
          label: '挂起',
          value: 4
        }
      ]
    }
  },
  components: {
    SparePartUsedSelectModal
  },
  props: {
  },
  created () {
    // 下拉框map
  },
  methods: {
    base (record) {
      this.visible = true
      var queryType = 1
      // 如果是空标识添加
      if (record.reportHandleType === 2) {
        this.modalTitle = '维修上报'
        this.visibleReport = true
      } else {
        this.modalTitle = '上报处理'
        this.visibleReport = false
        queryType = 2
      }
      this.model = record
      this.roleType = record.roleType
      const params = { roleType: record.roleType, deptId: record.repairDeptId, queryType: queryType }
      this.getUsers(params)
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'remark',
          'reportHandleType'
        ])))
      })
    },
    getUsers (params) {
      queryRepairUser(params).then(res => {
        this.roleUserMap = res.data
      })
    },
    changePlanFlag (value) {
      this.reportLeaderId = value
    },
    changeReportFlag (value) {
      this.model.reportHandleType = value
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        this.model.reportLeaderId = values.reportLeaderId
        if (this.model.reportHandleType === 1) { // 维修上报处理
          // 维修上报处理
          if (this.model.roleType === 2 || this.model.roleType === 3) {
            this.model.repairUserId = values.reportLeaderId
          } else {
            this.model.dispatchUserId = values.reportLeaderId
          }
        }
        this.model.remark = values.remark
        this.model.roleType = this.roleType // 上报人角色类型 3 工程师
        reportRepair(this.model).then(() => {
          this.$message.info('操作成功')
          if (this.model.reportHandleType === 2) {
            this.handleCancel(2)
          } else {
            this.handleCancel(1)
          }
        }).catch(() => {
          this.confirmLoading = false
        })
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      if (values === 1) {
        this.$emit('ok')
      } else {
        this.$emit('okk')
      }
      /* if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok')
      } else {
        this.$emit('ok')
      } */
    }
  }
}
</script>
