<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :modal-append-to-body="false"
    :close-on-click-modal="false"
    @close="cancelPlatform()"
  >
    <el-form ref="dialogPlatformForm" :model="dialogPlatformForm" :rules="platformRules" label-width="100px">
      <el-form-item label="平台名称" prop="channel">
        <el-select
          v-model="dialogPlatformForm.channel"
          placeholder="请选择平台"
          clearable
          style="width: 100%"
        >
          <el-option :label="item.label" :value="item.id" v-for="item in channelArr"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="推流地址" prop="pushUrl">
        <el-input
          v-model="dialogPlatformForm.pushUrl"
          placeholder="请输入推流地址"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="推流码/秘钥" prop="pushParam">
        <el-input
          v-model="dialogPlatformForm.pushParam"
          placeholder="请输入推流码/秘钥"
          clearable
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelPlatform()">取 消</el-button>
      <el-button type="primary" @click="confirmPlatformDialog()">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { channelArr } from "@/utils/options";
export default {
  props: {
    title:{
      type: String
    },
    visible: {
      type: Boolean,
      default:  false
    },
    pushList: {
      type: Array,
      default: ()=>[]
    }
  },
  data() {
    return {
      channelArr,
      dialogPlatformForm: {
        channel: '',
        pushUrl: '',
        isPush: 1,
        pushParam: ''
      },
      platformRules: {
        channel: [
          { required: true, message: "请选择平台", trigger: "blur" },
        ],
        pushUrl: [
          { required: true, message: "请输入推流地址", trigger: "blur" },
        ],
        pushParam: [{ required: true,message: "请输入推流参数",  trigger: "blur" }]
      },
    }
  },
  computed: {
    pushObj() {
      let obj = {}
      this.pushList.forEach(item=>{
        obj[item.channel] = {
          pushUrl: item.pushUrl,
          pushParam: item.pushParam
        }
      })
      return obj
    },
  },
  methods: {
    cancelPlatform() {
      this.dialogPlatformForm = {
        channel: '',
        pushUrl: '',
        isPush: 1,
        pushParam: ''
      }
      this.$emit('close')
    },
    confirmPlatformDialog() {
      this.$refs.dialogPlatformForm.validate(valid => {
        if (valid) {
          const childPushObj = this.pushObj[this.dialogPlatformForm.channel]
          if(this.title === '添加转播平台信息' && childPushObj && childPushObj.pushUrl === this.dialogPlatformForm.pushUrl && childPushObj.pushParam === this.dialogPlatformForm.pushParam) {
            return  this.$message.warning('该条转播信息已存在！')
          }
          this.$emit('confirm',this.dialogPlatformForm)
          this.cancelPlatform()
        }
      });
    },
  },
}
</script>