<template>
  <a-modal
    title="生成任务"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-form :form="form">
      <row-list :col="1" v-show="false">
        <row-item>
          <a-form-item>
            <a-input v-decorator="['gatherId']" type="hidden"/>
          </a-form-item>
        </row-item>
      </row-list>
      <a-row type="flex">
        <a-col :sm="20" >
          <a-form-item
            label="巡检任务名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input v-decorator="['name', {rules: [{required: true, message: '巡检任务名称不能为空'}]}]" />
          </a-form-item>
        </a-col>

        <a-col :sm="20" >
          <a-form-item
            label="巡检人"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['checker', {rules: [{required: true, message: '巡检人不能为空'}]}]">
              <a-select-option
                v-for="item in ipqcMap"
                :key="item.userId"
                :label="item.realName"
                :value="item.userId">
                {{ item.realName }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="20" >
          <a-form-item
            label="巡检主管"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['checkLeader', {rules: [{required: true, message: '巡检主管不能为空'}]}]">
              <a-select-option
                v-for="item in directorMap"
                :key="item.userId"
                :label="item.realName"
                :value="item.userId">
                {{ item.realName }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="20" >
          <a-form-item
            label="巡检设备"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            {{ sbIds.length }}台
            <a-button style="width: 30% ;float: right;" type="primary" @click="handleSbSelect">选择</a-button>
          </a-form-item>
        </a-col>
        <a-col :sm="20" >
          <a-form-item
            label="巡检内容项"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            {{ infoNum }}个
            <a-button style="width: 30% ;float: right;" type="primary" @click="handleInfoSelect">查看</a-button>
          </a-form-item>
        </a-col>
        <a-col :sm="20" >
          <a-form-item
            label="巡检频次"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              type="number"
              :min="0"
              v-decorator="['period', {rules: [{required: true, message: '巡检频次不能为空'}]}]">
              <span slot="suffix">天</span>
            </a-input>
          </a-form-item>
        </a-col>
        <a-col :sm="20" >
          <a-form-item
            label="要求时间"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              type="number"
              :min="0"
              v-decorator="['requireHour', {rules: [{required: true, message: '要求时间不能为空'}]}]">
              <span slot="suffix">小时</span>
            </a-input>
          </a-form-item>
        </a-col>
        <a-col :sm="20" >
          <a-form-item
            label="超时预警时间"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              type="number"
              :min="0"
              v-decorator="['warningHour', {rules: [{required: true, message: '超时预警时间不能为空'}]}]">
              <span slot="suffix">小时</span>
            </a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
    </template>
    <sb-info-select-modal ref="sbInfoSelectModal" type="checkbox" @selected="handleSbSelectd"/>
    <info-select-modal ref="infoSelectModal"/>

  </a-modal>
</template>

<script>
import { getQueryUser, addFillTasks } from '@/api/fill/gather'
import SbInfoSelectModal from './SbInfoSelectModal'
import InfoSelectModal from './InfoSelectModal'

export default {
  name: 'BaseFillGather',
  data () {
    return {
      form: this.$form.createForm(this),
      visible: false,
      ipqcMap: [],
      model: {},
      sbIds: [],
      directorMap: [],
      infoNum: 0,
      confirmLoading: false
    }
  },
  components: {
    SbInfoSelectModal,
    InfoSelectModal
  },
  created () {
    this.getUser()
  },
  methods: {
    base (record) {
      this.visible = true
      this.model = record
      this.infoNum = record.fillInfos.length
      console.log(this.infoNum)
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          gatherId: record.id,
          period: record.period,
          warningHour: 2
        }))
      })
    },
    // 获取巡检人与巡检主管信息
    getUser () {
      getQueryUser(this.DictCache.VALUE.QUERY_ROLE_TYPE.IPQC).then(res => {
        this.ipqcMap = res.data
      })
      getQueryUser(this.DictCache.VALUE.QUERY_ROLE_TYPE.DIRCTOR).then(res => {
        this.directorMap = res.data
      })
    },
    handleSbSelect () {
      this.$refs.sbInfoSelectModal.base({}, {})
    },
    handleInfoSelect () {
      this.$refs.infoSelectModal.base(this.model)
    },
    handleSbSelectd (keys, rows) {
      this.sbIds = keys
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      // this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          Object.values(errors).map(item => {
            this.$message.error(item.errors[0].message)
          })
          this.confirmLoading = false
        } else if (this.sbIds.length === 0) {
          console.log(values)
          this.$message.error('巡检设备不能为空！')
          return
        }
        values.sbIds = this.sbIds
        console.log(values)
        addFillTasks(values).then(res => {
          this.$message.success('创建成功！')
          this.handleCancel()
        })
      })
    },
    handleCancel (values) {
      this.visible = false
      this.form.resetFields()
      this.$emit('ok', values)
    }

  }
}
</script>
<style lang="less" scoped>

</style>
