<template>
  <div class="hum-page-container">
    <div class="hum-page-title">测流任务</div>
    <div class="hum-page-main">
      <div class="hum-page-search">
        <el-form :model="queryParams" size="large" ref="queryForm" label-position="top">
          <el-row :gutter="30">
            <el-col :span="6">
              <el-form-item label="选择站点">
                <SiteSelector @change="siteIdHandler" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item class="hum-page-search-action">
                <el-button type="primary" @click="handleQuery">查询</el-button>
                <el-button @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="hum-page-action">
        <el-button type="primary" @click="$router.push('/site/site/add')">新增站点</el-button>
      </div>
      <el-table v-loading="loading" :data="list" border>
        <el-table-column label="任务编号" prop="taskid" fixed />
        <el-table-column label="任务模式">
          <template slot-scope="scope">
            <span>{{ ['移动测流', '固定测流'][scope.row.type] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="策略编号" prop="planid" />
        <el-table-column label="起测时间">
          <template slot-scope="scope">
            <span>{{ formatDateTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="水位" prop="waterlevel" />
        <el-table-column label="过水面积" prop="acreagesum" />
        <el-table-column label="瞬时流量（总）" prop="flowsum" />
        <el-table-column label="当前状态">
          <template slot-scope="scope">
            <span class="status status-running" v-if="scope.row.status === 0">
              <span class="status-circle"></span>
              <span class="status-text">测流中</span>
            </span>
            <span class="status status-stopped" v-else-if="scope.row.status === 1">
              <span class="status-circle"></span>
              <span class="status-text">已终止</span>
            </span>
            <span class="status status-finished" v-else-if="scope.row.status === 2">
              <span class="status-circle"></span>
              <span class="status-text">已完成</span>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <template v-if="scope.row.status === 0">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="goRealTime(scope.row)">实时动态</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleStop(scope.row)">中止</el-button>
            </template>
            <template v-else-if="scope.row.status === 2">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="goRealTime(scope.row)">测流成果</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleExport(scope.row)">导出</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.size"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { listAchievements, taskAction } from "@/api/analysis/achievement";
import SiteSelector from '@/components/SiteSelector';

export default {
  components: {
    SiteSelector,
  },
  data() {
    return {
      siteId: 0,
      queryParams: {
        page: 1,
        size: 10,
      },
      dateRange: [],
      siteTypeOptions: [
        { value: 1, label: '流量站' }
      ],
      loading: true,
      total: 0,
      list: []
    }
  },
  methods: {
    siteIdHandler(v) {
      this.siteId = v;
      this.handleQuery();
    },
    handleQuery() {
      this.getList();
    },
    handleSiteChange(siteId) {
      this.siteId = siteId;
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        page: 1,
        size: 10,
      };
      this.getList();
    },
    getList() {
      this.loading = true;
      const data = this.addDateRange(this.queryParams, this.dateRange);
      data.siteId = this.siteId;
      listAchievements(data).then(response => {
          this.list = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    goRealTime(task) {
      if (task.status === 0) {
        this.$router.push(`/analysis/task/realtime/${task.siteId}`);
      } else {
        this.$router.push(`/analysis/task/realtime/${task.siteId}?taskid=${task.taskid}`);
      }
    },
    handleExport(task) {
      window.open(`${process.env.VUE_APP_BASE_API}/achievement/downAchievement?resultId=${task.resultId}`, '_blank')
    },
    handleStop(task) {
      this.$modal.confirm('是否确认中止编号为"' + task.taskid + '"的测流任务？').then(function() {
        return taskAction(task.siteId, 0);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("中止成功");
      }).catch(() => {});
    },
  }
}
</script>

<style lang="scss" scoped>
.status {
  display: flex;
  align-items: center;
  &-circle {
    width: 8px;
    height: 8px;
    border-radius: 50%;
  }
  &-text {
    margin-left: 6px;
    color: #54606C;
  }
}

.status-running .status-circle {
  background: #22C777;
}
.status-stopped .status-circle {
  background: #FA3C58;
}
.status-finished .status-circle {
  background: #8D99A4;
}
</style>
