// 添加/修改设备
<template>
  <el-dialog
    :title="isEdit ? '编辑设备' : '添加设备'"
    width="500px"
    :visible.sync="visible"
    :destroy-on-close="false"
    :close-on-click-modal="false"
    :center="true"
  >
    <el-form ref="form" :rules="rules" :model="form" label-position="left" label-width="120px">
      <el-form-item prop="id" style="margin-bottom: 0;">&nbsp;</el-form-item>
      <el-form-item label="设备sn" prop="sn" required>
        <el-input v-model="form.sn" />
      </el-form-item>
      <el-form-item label="服务商平台名称" prop="tenantId" required>
        <el-select v-model="form.tenantId" class="filter-item" placeholder="服务商平台名称" filterable>
          <el-option v-for="(item, index) in merchantLists" :key="index" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="公司名称" prop="company" required>
        <el-input v-model="form.company" />
      </el-form-item>
      <el-form-item label="场景" prop="sceneId" :required="false">
        <el-select v-model="form.sceneId" class="filter-item" placeholder="请选择场景" clearable>
          <el-option v-for="(item, index) in sceneLists" :key="index" :label="item.sceneName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="模式" prop="mode" required>
        <el-select v-model="form.mode" class="filter-item" placeholder="请选择模式">
          <el-option v-for="(item, index) in modeLists" :key="index" :label="item.value" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="!(!form.mode || form.mode === 'public')" label="⽤户库" prop="userLibId" :required="false">
        <el-select v-model="form.userLibId" class="filter-item" placeholder="请选择⽤户库" clearable>
          <el-option v-for="(item, index) in userlibLists" :key="index" :label="item.userLibName" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button type="primary" :loading="submitLoading" @click="submit">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add } from '@/api/device'
import { getAllList } from '@/api/merchant'
import { getList as getSceneList } from '@/api/scene'
import { getList as getUserlibList } from '@/api/userlib'

export default {
  data() {
    return {
      merchantLists: [],
      sceneLists: [],
      userlibLists: [],
      modeLists: [
        {
          key: 'public',
          value: '开放'
        },
        {
          key: 'close',
          value: '封闭'
        },
        {
          key: 'elasticity',
          value: '弹性'
        }
      ],
      form: {
        id: null,
        sn: null,
        tenantId: null,
        company: null,
        sceneId: null,
        mode: null,
        userLibId: null
      },
      rules: {
        sn: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        tenantId: [
          { required: true, type: 'number', whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        company: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ],
        mode: [
          { required: true, whitespace: true, message: '不能为空!', trigger: ['change', 'blur'] }
        ]
      },
      visible: false,
      submitLoading: false,
      isEdit: false
    }
  },
  watch: {
    'form.mode': {
      handler(value) {
        if (!value || value === 'public') {
          this.form.userLibId = null
        }
      },
      immediate: true
    }
  },
  created() {
    this.getMerchantList()
    this.getSceneList()
    this.getUserlibList()
  },
  methods: {
    open(obj) {
      console.log(obj);
      this.visible = true
      this.submitLoading = false
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        if (obj && Object.keys(obj).length > 0) {
          this.form = obj
          this.isEdit = true
        } else {
          this.isEdit = false
        }
      })
    },
    getMerchantList() {
      getAllList().then(res => {
        this.merchantLists = res.data
      })
    },
    getSceneList() {
      getSceneList({ pageNumber: 1, pageSize: 1000 }).then(res => {
        this.sceneLists = res.data.records
      })
    },
    getUserlibList() {
      getUserlibList({ pageNumber: 1, pageSize: 1000 }).then(res => {
        this.userlibLists = res.data.records
      })
    },
    submit(e) {
      e.preventDefault()
      this.$refs.form.validate(async(valid) => {
        if (!valid) {
          return false
        }
        if (this.submitLoading) {
          return
        }
        this.submitLoading = true
        try {
          const params = {
            id: this.form.id || null,
            sn: this.form.sn,
            tenantId: this.form.tenantId,
            company: this.form.company,
            sceneId: this.form.sceneId,
            userLibId: this.form.userLibId,
            mode: this.form.mode
          }
          await add(params)
          this.visible = false
          this.$emit('success', this.isEdit)
        } catch (e) {
          console.info('add', e)
        } finally {
          this.submitLoading = false
        }
      })
    }
  }
}
</script>

<style scoped>
::v-deep .el-select {
  width: 100%;
}
</style>
