<template>
  <el-drawer
    :append-to-body="true"
    :show-close="false"
    :visible.sync="visible"
    size="40%"
  >
    <div slot="title" style=" background-color:#02BCFF;font-size:1.5em;color:white;padding:5px 20px">
      {{ !dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter }}
    </div>
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="100px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item :label="$t('module.equipmentManager.eqManagerManage.equipmentName')" prop="equipmentId">
        <el-select v-model="dataForm.equipmentId" :placeholder="$i18nForm(['placeholder.select', $t('module.equipmentManager.eqManagerManage.equipmentName')])" clearable>
          <el-option v-for="item in eqList" :key="item.id" :value="item.id" :label="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('module.equipmentManager.eqManagerManage.equipmentCode')" prop="equipmentId">
        <el-select v-model="dataForm.equipmentId" :placeholder="$i18nForm(['placeholder.select', $t('module.equipmentManager.eqManagerManage.equipmentCode')])" clearable disabled>
          <el-option v-for="item in eqList" :key="item.id" :value="item.id" :label="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('module.equipmentManager.eqManagerManage.worker')" prop="workerId">
        <el-select v-model="dataForm.workerId" :placeholder="$i18nForm(['placeholder.select', $t('module.equipmentManager.eqManagerManage.worker')])" clearable @change="changeWorker">
          <el-option v-for="item in roleList" :key="item.id" :value="item.userId" :label="item.name" />
        </el-select>
      </el-form-item>
    </el-form>
    <div class="drawer-footer">
      <el-button @click="visible = false">{{ 'btn.back' | i18nFilter }}</el-button>
      <el-button type="primary" :loading="btnLoading" @click="dataFormSubmit()">{{ 'btn.confirm' | i18nFilter }}</el-button>
    </div>
  </el-drawer>
</template>

<script>
import { getInfo, add, edit } from '@/api/equipment/eqManager'

export default {
  props: {
    workerList: {
      type: Array,
      default: () => []
    },
    eqList: {
      type: Array,
      default: () => []
    },
    roleList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      visible: false,
      btnLoading: false,
      dataForm: {
        id: 0,
        equipmentId: '',
        workerId: ''
      },
      roleId: '',
      dataRule: {
        equipmentId: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.equipmentManager.eqManagerManage.equipmentName')]), trigger: 'blur' }
        ],
        workerId: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.equipmentManager.eqManagerManage.worker')]), trigger: 'blur' }
        ]
      },
      roleObj: {}
    }
  },
  watch: {
    roleList: function(val) {
      this.roleObj = {}
      val.map(item => {
        this.roleObj[item.id] = item.dataName
      })
    }
  },
  mounted() {},
  methods: {
    init(id) {
      this.dataForm.id = id || ''
      this.btnLoading = false
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getInfo({ id: this.dataForm.id }).then(res => {
            this.dataForm.equipmentId = res.data.equipmentId
            this.dataForm.workerId = res.data.workerId
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.btnLoading = true
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = {
            'equipmentId': this.dataForm.equipmentId,
            'workerId': this.dataForm.workerId,
            'id': this.dataForm.id
          }
          if (this.dataForm.id) {
            edit(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.btnLoading = false
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            add(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.$refs['dataForm'].resetFields()
                  this.btnLoading = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    },
    changeWorker(v) {
      for (let i = 0; i < this.workerList.length; i++) {
        if (v === this.workerList[i].id) {
          this.roleId = this.workerList[i].roleId
          return
        }
      }
    }
  }
}
</script>

<style scoped>
.drawer-footer {
  width: 100%;
  margin-top: 50px;
  border-top: 1px solid #e8e8e8;
  padding: 10px 50px;
  text-align: left;
  background: #fff;
}
</style>
