<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['id']" />
      </a-form-item>
      <a-form-item style="display:none">
        <a-input v-decorator="['version']" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="类别名称">
        <a-input placeholder="名称" v-decorator="['name']" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="派单员">
        <a-select show-search v-decorator="['staffId', {rules: [{ required: true, message: '派单员' }]}]" optionFilterProp="label"
                  >
          <a-select-option v-for="item in staffLists" :key="item.id" :label="item.mobile">{{ item.username }}--{{ item.mobile }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="维修员">
        <a-select show-search mode="multiple"
                  v-decorator="['workerId', {rules: [{ required: true, message: '维修员' }]}]" optionFilterProp="label">
          <a-select-option v-for="item in staffLists" :key="item.id" :label="item.mobile">{{ item.username }}--{{ item.mobile }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
        <a-input placeholder="备注" v-decorator="['remark']" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { saveRepairType, selectUserListByType } from '@/api/admin/repair/repairDeviceType'
import pick from 'lodash.pick'

export default {
  name: 'RepairTypeModal',
  props: {},
  components: {},
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this),
      staffLists: []
    }
  },
  beforeCreate () {

  },
  created () {
  },
  methods: {
    selectUserListByType () {
      selectUserListByType().then(res => {
        this.staffLists = res.data
      })
    },
    handleSearch (value) {
      console.log(value)
      selectUserListByType({ 'phone': value }).then(res => {
        console.log(res.data)
        // this.staffLists = res.data
      })
    },
    add () {
      this.form.resetFields()
      this.edit({ id: 0 })
    },
    edit (record) {
      this.selectUserListByType()

      this.mdl = Object.assign(record)
      console.log(this.mdl)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'id', 'name', 'staffId', 'workerId', 'staffLists', 'remark', 'deleteFlag', 'createBy', 'createTime', 'updateBy', 'updateTime', 'tenantId', 'parkId'))
      })
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveRepairType(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
