<template>
  <el-dialog
    append-to-body
    :title="textMap[dialogStatus]"
    :visible.sync="dialogFormVisible"
  >
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="deviceForm"
      label-width="100px"
      label-well-code="right"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="通讯方式" prop="communication">
            <el-select
              v-model="deviceForm.communication"
              clearable
              multiple
              :disabled="dialogStatus === 'detail' ? true : false"
              placeholder="请选择支持通信方式"
            >
              <el-option
                v-for="item in commuList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="默认坐标系" prop="coordinateType">
            <el-input
              v-model="deviceForm.coordinateType"
              placeholder="请输入数据默认坐标系"
              clearable
              :disabled="dialogStatus === 'detail' ? true : false"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="生成工单" prop="isJobGenerate">
            <el-select
              v-model="deviceForm.isJobGenerate"
              clearable
              placeholder="请选择是否生成工单"
              :disabled="dialogStatus === 'detail' ? true : false"
            >
              <el-option
                v-for="item in jobList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="APP推送" prop="isAppPush">
            <el-select
              v-model="deviceForm.isAppPush"
              clearable
              placeholder="请选择是否APP推送"
              :disabled="dialogStatus === 'detail' ? true : false"
            >
              <el-option
                v-for="item in appPushList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="平台地址" prop="address">
            <el-input
              v-model="deviceForm.address"
              placeholder="请输入平台地址"
              clearable
              :disabled="dialogStatus === 'detail' ? true : false"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="应用名称" prop="planName">
            <el-input
              v-model="deviceForm.planName"
              clearable
              :disabled="dialogStatus === 'detail' ? true : false"
              placeholder="请输入应用名称"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="APPID" prop="APPID">
            <el-input
              v-model="deviceForm.APPID"
              placeholder="请输入APPID"
              clearable
              :disabled="dialogStatus === 'detail' ? true : false"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="APPKEY" prop="APPKEY">
            <el-input
              v-model="deviceForm.APPKEY"
              clearable
              :disabled="dialogStatus === 'detail' ? true : false"
              placeholder="请输入APPKEY"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div
      v-show="dialogStatus === 'detail' ? false : true"
      slot="footer"
      class="dialog-footer"
    >
      <el-button type="primary" @click="saveData">
        保存
      </el-button>
      <el-button @click="dialogFormVisible = false">
        取消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateAlarmContent } from '@/api/systemConfig/tenantConfig'
export default {
  name: 'EditAlarmConfig',
  data() {
    return {
      // 通讯方式
      commuList: [
        {
          label: 'LORA',
          value: '0'
        },
        {
          label: '3G',
          value: '1'
        },
        {
          label: '4G',
          value: '2'
        },
        {
          label: '联通移动NB',
          value: '3'
        },
        {
          label: '电信NB',
          value: '4'
        }
      ],
      // 是否生成工单
      jobList: [
        {
          label: '否',
          value: '0'
        },
        {
          label: '是',
          value: '1'
        }
      ],
      // 是否开启App推送
      appPushList: [
        {
          label: '否',
          value: '0'
        },
        {
          label: '是',
          value: '1'
        }
      ],
      dialogFormVisible: false, // 对话框显示隐藏
      dialogStatus: '', // 对话框类型：create,update,detail
      // 对话框标题
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      },
      // 是否启用
      isValidList: [
        {
          label: '是',
          value: '1'
        },
        {
          label: '否',
          value: '0'
        }
      ],
      // 表单内容
      deviceForm: {
        communication: [],
        coordinateType: '',
        isJobGenerate: '',
        isAppPush: '',
        planName: '',
        address: '',
        APPID: '',
        APPKEY: ''
      },
      // 表单校验规则
      rules: {
        communication: [{ required: true, message: '支持通讯方式必选', trigger: ['blur', 'change'] }],
        coordinateType: [{ required: true, message: '坐标系不能为空', trigger: ['blur', 'change'] }],
        isJobGenerate: [{ required: true, message: '是否生成工单必选', trigger: ['blur', 'change'] }],
        isAppPush: [{ required: true, message: '是否开启APP推送必选', trigger: ['blur', 'change'] }],
        address: [{ required: true, message: '平台地址不能为空', trigger: ['blur', 'change'] }],
        planName: [{ required: true, message: '应用名称不能为空', trigger: ['blur', 'change'] }],
        APPID: [{ required: true, message: 'APPID不能为空', trigger: ['blur', 'change'] }],
        APPKEY: [{ required: true, message: 'APPKEY不能为空', trigger: ['blur', 'change'] }]
      }
    }
  },
  methods: {
    // 重置表单
    resetForm() {
      this.deviceForm = {
        communication: [],
        coordinateType: '',
        isJobGenerate: '',
        isAppPush: '',
        planName: '',
        address: '',
        APPID: '',
        APPKEY: ''
      }
    },
    // 父子组件通信
    initDialog(dialogStatus, row = null) {
      this.dialogFormVisible = true
      this.dialogStatus = dialogStatus
      if (this.dialogStatus === 'create') {
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (this.dialogStatus === 'update' || this.dialogStatus === 'detail') {
        this.deviceForm = {
          communication: row.communication.split(','),
          coordinateType: row.coordinateType,
          isJobGenerate: row.isJobGenerate,
          isAppPush: row.isAppPush,
          planName: row.planName,
          address: row.address,
          APPID: row.address,
          APPKEY: row.APPKEY
        }
      }
    },
    // 修改数据
    saveData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.deviceForm.communication = this.deviceForm.communication.toString()
          updateAlarmContent(this.deviceForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    }
  }
}
</script>

<style>
  .el-select__tags {
    flex-wrap: inherit !important;
    overflow-x: auto !important;
  }
  ::-webkit-scrollbar {
    height: 4px;
  }
  /* //滚动条的滑块 */
  ::-webkit-scrollbar-thumb {
    background-color: #eaecf1;
    border-radius: 3px;
  }
</style>
