<template>
  <el-dialog :visible.sync="dialogFormVisible" :close-on-click-modal="false" title="人员数据下发" append-to-body @close="cancel">
    <el-form ref="dataForm" :model="form" :rules="rules" label-width="auto" >
      <el-row type="flex" justify="center">
        <el-col :span="20">
          <el-form-item label="已选人员">
            <el-tag v-for="item in personList" :key="item.id" class="person-span">{{ item.name }}</el-tag>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="20">
          <el-form-item label="下发设备" prop="selectDeivces">
            <el-select v-model="form.selectDeivces" multiple placeholder="请选择" style="width: 100%">
              <el-option
                v-for="item in deviceList"
                :key="item.id"
                :label="item.devCode"
                :value="''+item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="loading" type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDeviceListAll } from '../../api/device'
import { updatePersonData } from '../../api/person'

export default {
  name: 'UpdateData',
  data() {
    return {
      form: {
        personIds: [],
        selectDeivces: []
      },
      rules: {
        selectDeivces: [{ required: true, message: '下发设备不能为空', trigger: ['blur', 'change'] }]
      },
      personList: [],
      deviceList: [],
      dialogFormVisible: false,
      loading: false
    }
  },
  methods: {
    initDialog(dialogFormVisible, personList) {
      this.dialogFormVisible = dialogFormVisible
      this.personList = personList
      this.fetchDeviceList()
      this.form.selectDeivces = []
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    fetchDeviceList() {
      getDeviceListAll().then(res => {
        this.deviceList = res.data
      })
    },
    cancel() {
      this.dialogFormVisible = false
    },
    saveData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.form.personIds = this.personList.map(item => item.id)
          console.log(this.form.personIds)
          updatePersonData(this.form.personIds, this.form.selectDeivces).then(res => {
            if (res.code === 200) {
              this.loading = false
              this.$message.success('数据下发成功')
              this.dialogFormVisible = false
              this.$emit('finishUpdate')
            } else {
              this.loading = false
              this.dialogFormVisible = false
              this.$emit('finishUpdate')
            }
          }).catch((res) => {
            this.loading = false
            this.dialogFormVisible = false
            this.$emit('finishUpdate')
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.person-span{
  color: #909399;
  background-color: #f0f2f5;
  margin-right: 10px;
  border-color: #f0f2f5;
  padding: 0 5px;
}
.dialog-footer {
  margin: auto;
}
</style>
