<!-- 详情弹窗 -->
<template>
  <ele-modal
    title="同步观看数据"
    width="680px"
    :visible="visible"
    @update:visible="updateVisible"
  >
    <el-form size="mini" label-width="82px" class="ele-form-detail">
      <el-row :gutter="15">
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
          <el-form-item label="频道:">
            <div class="ele-text-secondary">
              {{ data.channelId }}
            </div>
          </el-form-item>
        </el-col>
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
          <el-form-item label="场次ID:">
            <div class="ele-text-secondary">
              {{ data.sessionId }}
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
          <el-form-item label="同步场次:">
            <div class="ele-text-secondary">
              {{ data.sessionTime }}
            </div>
          </el-form-item>
        </el-col>
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
          <el-form-item label="同步观看:">
            <div class="ele-text-secondary">
              {{ data.viewlogTime }}
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <div style="margin: 12px 0">
        <el-divider />
      </div>
      <el-form-item label="同步进度:" v-if="loading">
        <el-progress :percentage="percent" style="padding-top: 7px" />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">关闭</el-button>
      <el-button type="primary" :loading="loading" @click="save('do')">
        点击同步数据>>>
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import { viewApi } from '@/api/sylive/live';

  export default {
    props: {
      // 弹窗是否打开
      visible: Boolean,
      data: Object
    },
    data() {
      return {
        // 提交状态
        loading: false,
        doing: true,
        percent: 0,
        page: 1
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      /* 保存编辑 */
      save(command) {
        if (command == 'do') {
          this.loading = true;
          this.doing = true;
          this.percent = 0;
          this.page = 1;
        }
        if (!this.doing) {
          return;
        }
        viewApi({
          activityId: this.data.activityId,
          channelId: this.data.channelId,
          sessionId: this.data.sessionId,
          page: this.page
        })
          .then((data) => {
            this.percent = data.percent;
            if (data.nextPageNumber) {
              this.page = data.nextPageNumber;
              this.save('next');
            } else {
              this.loading = false;
              this.updateVisible(false);
              this.$emit('done');
            }
          })
          .catch((e) => {
            this.loading = false;
            this.$message.error(e.message);
          });
      },
      /* 更新visible */
      updateVisible(value) {
        if (!value) {
          this.loading = false;
          this.doing = false;
          this.percent = 0;
          this.page = 1;
        }
        this.$emit('update:visible', value);
      }
    }
  };
</script>
