<template>
  <div id="detail">
    <el-row>
      <el-col :span="24">
        <el-col :span="24" class="messgae">
          <el-form ref="form" :model="form" label-width="120px" :rules="rules">
            <el-form-item label="标题" prop="title">
              <el-input v-model.number="form.title" placeholder="请输入房间标题"></el-input>
            </el-form-item>
            <el-form-item label="房间号" prop="name">
              <el-input v-model.number="form.name" placeholder="房间号必须是数字"></el-input>
            </el-form-item>
            <el-form-item label="主播" prop="anchorid">
              <el-select v-model="form.anchorid" placeholder="请选择主播" filterable>
                <el-option v-for="item in newlist" :key="item.id" :label="item.name" :value="item.id"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="直播时间" required>
              <el-col :span="9">
                <el-form-item prop="starttime">
                  <el-date-picker
                    v-model="form.starttime"
                    type="datetime"
                    style="width: 60%;"
                    placeholder="请选择直播开始时间"
                    format="yyyy-MM-dd HH:mm"
                    value-format="yyyy-MM-dd HH:mm"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col class="line" :span="2">-</el-col>
              <el-col :span="9">
                <el-form-item prop="endtime">
                  <el-date-picker
                    v-model="form.endtime"
                    type="datetime"
                    style="width: 60%;"
                    placeholder="请选择直播结束时间"
                    format="yyyy-MM-dd HH:mm"
                    value-format="yyyy-MM-dd HH:mm"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col class="line" :span="4"></el-col>
            </el-form-item>
            <el-form-item label="类型" prop="type">
              <el-radio v-model="form.type" label="0">直播</el-radio>
              <el-radio v-model="form.type" label="1">会议</el-radio>
            </el-form-item>
            <el-form-item label="封面图片" prop="filedir">
              <upload :limit="1" :data="form.filedir" type="filedir" :url="'/files/filedir/upload'" @upload="uploadSuccess"></upload>
            </el-form-item>
            <el-form-item label="简介" prop="content">
              <el-input
                type="textarea"
                maxlength="300"
                show-word-limit
                :autosize="{ minRows: 2, maxRows: 4 }"
                v-model="form.content"
                placeholder="请输入简介"
              ></el-input>
            </el-form-item>
            <el-form-item label="是否开启广告" prop="isadvert">
              <el-switch v-model="form.isadvert" active-text="开启" inactive-text="关闭"> </el-switch>
            </el-form-item>
            <el-form-item label="广告位" v-if="form.isadvert == true">
              <el-col :span="20">
                <el-table :data="form.adverts" style="width: 100%" border>
                  <el-table-column prop="title" label="标题" align="center"> </el-table-column>
                  <el-table-column label="图片地址" align="center">
                    <template slot-scope="scope">
                      <el-image style="width: 30px; height: 30px" :src="scope.row.imgdir"></el-image>
                    </template>
                  </el-table-column>
                  <el-table-column prop="imgurl" label="链接地址" align="center"> </el-table-column>
                  <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                      <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-col>
              <el-col :span="4">
                <el-button @click="drawer = true" type="primary" style="margin-left: 16px;">
                  上传广告图片
                </el-button>
                <el-drawer title="广告位" class="drawer" :visible.sync="drawer" :direction="direction" :before-close="handleClose">
                  <el-form-item label="标题">
                    <el-input v-model="drawerform.title" placeholder="请输入标题"></el-input>
                  </el-form-item>
                  <el-form-item label="图片">
                    <upload :limit="1" :data="drawerform.imgdir" type="imgdir" :url="'/files/advert/upload'" @upload="advertSuccess"></upload>
                  </el-form-item>
                  <el-form-item label="链接">
                    <el-input v-model="drawerform.imgurl" placeholder="请输入链接地址"></el-input>
                  </el-form-item>
                  <el-col :span="24" style="text-align:center">
                    <el-button type="primary" @click="submitDrawer">保存</el-button>
                  </el-col>
                </el-drawer>
              </el-col>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit('form')">提交</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import upload from '@/components/upload.vue';
export default {
  name: 'detail',
  props: {
    form: null,
    newlist: null,
  },
  components: {
    upload,
  },
  data: () => ({
    adverts: [],
    direction: 'rtl',
    drawer: false,
    imgurl: '',
    imgdir: '',
    title: '',
    rules: {
      title: [{ required: true, message: '请输入房间标题', trigger: 'blur' }],
      name: [
        { required: true, message: '请输入房间号', trigger: 'blur' },
        // { type: 'number', message: '房间号必须为数字值' },
      ],
      anchorid: [{ required: true, message: '请选择主播', trigger: 'change' }],
      filedir: [{ required: true, message: '请选择头像图片', trigger: 'blur' }],
      type: [{ required: true, message: '请选择类型', trigger: 'change' }],
      content: [{ required: false, message: '请输入信息简介', trigger: 'blur' }],
      starttime: [{ required: false, message: '请选择直播開始時間', trigger: 'change' }],
      endtime: [{ required: false, message: '请选择直播結束時間', trigger: 'change' }],
    },
    drawerform: {},
  }),
  created() {},
  computed: {},
  methods: {
    uploadSuccess({ type, data }) {
      this.$set(this.form, `${type}`, data.uri);
    },
    onSubmit() {
      this.$emit('submitDate', { data: this.form });
    },
    advertSuccess({ type, data }) {
      this.$set(this.drawerform, `${type}`, data.uri);
    },
    handleClose(done) {
      done();
    },
    submitDrawer() {
      if (this.drawerform.title != null && this.drawerform.imgdir != null && this.drawerform.imgurl != null) {
        this.form.adverts.push(this.drawerform);
      }
      this.drawerform = {};
      this.drawer = false;
    },
    handleDelete(index, row) {
      this.form.adverts.splice(index, 1);
    },
  },
};
</script>

<style lang="less" scoped>
.top {
  padding: 15px 0;
}
.top .topTitle {
  text-align: left;
}
.top .topBtn {
  text-align: right;
  padding: 0 5px;
}
/deep/.el-table td {
  padding: 5px 0;
}
/deep/ .el-table th {
  padding: 5px 0;
}
/deep/.el-form-item .el-form-item {
  margin-bottom: 20px;
}
</style>
