<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="isMessagePush">
            <el-select
              v-model="deviceForm.isMessagePush"
              clearable
              placeholder="请选择是否短信推送"
              :disabled="dialogStatus === 'detail' ? true : false"
            >
              <el-option
                v-for="item in messagePushList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-show="dialogStatus === 'detail' ? true : false" :span="12">
          <el-form-item label="项目id" prop="devcode">
            <el-input
              v-model="deviceForm.devcode"
              clearable
              disabled
              placeholder="未知"
            />
          </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, addAlarmContent } from '@/api/systemConfig/tenantConfig'
export default {
  name: 'EditTenantConfig',
  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'
        }
      ],
      // 是否开启短信推送
      messagePushList: [
        {
          label: '否',
          value: '0'
        },
        {
          label: '是',
          value: '1'
        }
      ],
      dialogFormVisible: false, // 对话框显示隐藏
      dialogStatus: '', // 对话框类型：create,update,detail
      // 告警类型列表
      alarmTypeList: [
        {
          label: '告警类型一',
          value: '0'
        },
        {
          label: '告警类型二',
          value: '1'
        },
        {
          label: '告警类型三',
          value: '2'
        }
      ],
      // 对话框标题
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      },
      // 是否启用
      isValidList: [
        {
          label: '是',
          value: '1'
        },
        {
          label: '否',
          value: '0'
        }
      ],
      // 表单内容
      deviceForm: {
        communication: [],
        coordinateType: '',
        isJobGenerate: '',
        isAppPush: '',
        isMessagePush: '',
        devcode: ''
      },
      // 表单校验规则
      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'] }],
        isMessagePush: [{ required: true, message: '是否开启短信推送必选', trigger: ['blur', 'change'] }]
      }
    }
  },
  methods: {
    // 重置表单
    resetForm() {
      this.deviceForm = {
        communication: [],
        coordinateType: '',
        isJobGenerate: '',
        isAppPush: '',
        isMessagePush: '',
        devcode: ''
      }
    },
    // 父子组件通信
    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,
          isMessagePush: row.isMessagePush,
          devcode: row.tenentId
        }
      }
    },
    // 保存数据
    saveData() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 修改数据
    updateData() {
      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
            }
          })
        }
      })
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.deviceForm)
        if (valid) {
          addAlarmContent(this.deviceForm).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功，是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => {
                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>
