<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-09-05 10:03:30
 * @LastEditors: 无尘
 * @LastEditTime: 2019-09-05 10:03:30
 -->
<template>
  <el-dialog title="选择环境" :visible.sync="dialogVisible" width="600px" @close="close">
    <div class="dialog-body">
      <el-table :data="tableData" style="width: 100%" class="dialog-table" v-loading="loading">
        <el-table-column prop="sceneName" label="环境名称"> </el-table-column>
        <el-table-column prop="updateTime" label="更新时间">
          <template slot-scope="scope">
            <div class="color-606266 line-18">{{ scope.row.updateTime | timeStampToYmd }}</div>
            <div class="color-606266 line-18">{{ scope.row.updateTime | timeStampToHms }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="enableFlag" label="开启状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.enableFlag" @change="changeFlag(scope.row)"> </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="goList(scope.row)">立即进入</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </el-dialog>
</template>

<script>
import { getRequest } from '@/api/api';
export default {
  props: {
    pluginId: {
      type: String,
      default() {
        return '';
      }
    },
    pluginType: {
      type: Number,
      default() {
        return 0;
      }
    }
  },
  data() {
    return {
      dialogVisible: true,
      tableData: [],
      loading: false
    };
  },
  created() {
    this.dialogVisible = true;
    this.getData();
  },
  methods: {
    getData() {
      var that = this;
      let params = {
        pluginId: this.pluginId
      };
      this.loading = true;
      getRequest('/gateway-manage/plugin/scene/list', params)
        .then(res => {
          if (res.data.code == '0000') {
            if (res.data.result) {
              this.tableData = res.data.result;
              for (let item of this.tableData) {
                item.enableFlag = item.enableFlag === 1 ? true : false;
              }
            } else {
              this.tableData = [];
            }
            this.loading = false;
          } else {
            this.$message.error(res.data.message);
          }
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    changeFlag(item) {
      var that = this;
      let params = {
        pluginId: this.pluginId,
        sceneCode: item.sceneCode,
        enableFlag: item.enableFlag ? 1 : 0
      };
      getRequest('/gateway-manage/plugin/scene/enable', params)
        .then(res => {
          if (res.data.code == '0000') {
            this.getData();
          } else {
            item.enableFlag = !item.enableFlag;
            this.$message.error(res.data.message);
          }
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    goList(item) {
      this.dialogVisible = false;
      if (this.pluginType === 0) {
        //AHAS
        this.$router.push({ path: '/plugAhsh', query: { sceneCode: item.sceneCode, pluginId: this.pluginId } });
      } else if (this.pluginType === 1) {
        //开放平台
        this.$router.push({ path: '/plugPlatform', query: { sceneCode: item.sceneCode, pluginId: this.pluginId } });
      }
    },
    close() {
      this.dialogVisible = false;
      this.$emit('closeDialog');
    }
  }
};
</script>
<style lang="less" scoped>
.dialog-table {
  margin-bottom: 30px;

  .el-button {
    &.el-button--text:hover,
    &.el-button--text:focus {
      color: #2656d4;
    }
  }
}
</style>
