<template>
  <div class="app-container">
    <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="订单" name="order">
        <stationOrderList ref="order" :stationId="stationId"/>
      </el-tab-pane>

      <el-tab-pane label="设备" name="pile">
        <pileList ref="pile" :stationId="stationId"/>
      </el-tab-pane>

      <el-tab-pane label="充电接口" name="connector">
        <connectorList ref="connector" :stationId="stationId"/>
      </el-tab-pane>

      <el-tab-pane label="基本资料" name="stationInfo">
        <div class="over">
          <div id="map_wrap">
            <MapContainer ref="map" :stationLat="stationLat" :stationLng="stationLng"/>
          </div>
          <div class="menu">
            <site-info ref="stationInfo"/>
          </div>
        </div>
      </el-tab-pane>

      <!--<el-tab-pane label="账单" ></el-tab-pane>-->

      <el-tab-pane label="计费模块" name="billing">
        <!-- billing 文件到时引入 -->
        <billing ref="billing" :stationId="stationId"/>
      </el-tab-pane>

<!--      <el-tab-pane label="客户管理" name="member">-->
<!--        角色管理-->
<!--      </el-tab-pane>-->

      <el-tab-pane label="运营管理" name="operation">
        <el-form>
          <el-form-item label="当前配置二维码规则:">
            <el-input v-model="stationDetail.qrcodePrefix" maxlength="100px" :disabled="true"/>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-setting" size="big" @click="handleCreate" v-has-permi = "['pile:station:edit']">配置参数</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>

      <el-tab-pane label="站点白名单" name="whitelist">
        <whitelist ref="whitelist" :stationId="stationId"/>
      </el-tab-pane>
    </el-tabs>

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" @submit.native.prevent>
        <el-form-item label="二维码前缀:" prop="prefix" label-width="100px">
          <el-input v-model="form.prefix" placeholder="请输入二维码前缀" style="width: 300px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Billing from "./components/billing.vue";
import SiteInfo from "./components/SiteInfo.vue";
import connectorList from "./connectorList.vue";
import pileList from "./pileList.vue";
import MapContainer from "../../../components/MapContainer/MapContainer.vue";
import stationOrderList from "@/views/pile/station/stationOrderList";
import stationWhiteList from "@/views/pile/station/stationWhiteList";
import {getStationInfo, updateStationQRCodePrefix} from "@/api/pile/station";
import Whitelist from "@/views/pile/station/stationWhiteList";

export default {
  components: {
    Whitelist,
    SiteInfo,
    pileList,
    connectorList,
    Billing,
    MapContainer,
    stationOrderList,
    stationWhiteList
  },
  data() {
    return {
      activeName: 'pile',
      form: {
        prefix: '',
      },
      dialogTitle: '',
      qrcodePrefix: '',
      dialogFormVisible: false,
      stationId: this.$route.params.id,
      stationName: this.$route.params.stationName,
      stationDetail: {},
      stationLat:null,
      stationLng:null
    };
  },
  created() {
    // console.log(this.stationId, "充电桩详情 create");
    // const stationId = this.stationId;
    const stationName = this.stationName;
    const title = "【" + stationName + "】站点详情";
    const route = Object.assign({}, this.$route, { title: `${title}` })
    this.$store.dispatch('tagsView/updateVisitedView', route);
    this.queryStationInfo();
  },
  mounted() {
    this.initializeData(this.activeName);
  },
  methods: {
    handleClick(tab, event) {
      // console.log(tab, event);
      this.initializeData(tab.name);
    },
    // 执行对应页面的查询数据方法
    initializeData(name) {
      console.log("执行对应页面的查询数据方法", name)
      if (name === "pile") {
        this.$refs.pile.getPileList();
      } else if (name === "connector") {
        this.$refs.connector.getList();
      } else if (name === "billing") {
        this.$refs.billing.getStationBillingTemplateList();
      } else if (name === "stationInfo") {
        this.$refs.map.initMap();
        this.$refs.stationInfo.queryStationInfo();
      } else if (name === "order") {
        this.$refs.order.dataLoading();
      }
    },
    queryStationInfo() {
      console.log("stationId", this.stationId);
      getStationInfo(this.stationId).then((res) => {
        // console.log(res);
        this.stationDetail = res.data;
        this.stationLat = res.data.stationLat
        this.stationLng = res.data.stationLng
        console.log(this.stationLat,this.stationLng,"父组件里面的经纬度")
      });
      console.log("queryStationInfo==", this.stationDetail);
    },
    // 配置参数按钮
    handleCreate() {
      this.dialogTitle = '配置参数';
      this.form = {
        prefix: '',
      };
      this.dialogFormVisible = true;
    },
    // 配置参数提交按钮
    submitForm(form) {
      const param = {
        stationId: this.stationId,
        qrcodePrefix: this.form.prefix
      }
      console.log("form:", this.form);
      console.log("param:", param);
      this.$refs.form.validate((valid) => {
        if(valid){
          updateStationQRCodePrefix(param).then((response) => {
            this.$modal.msgSuccess('修改成功')
            this.dialogFormVisible = false
          })
        } else{
          return false
        }
      })
    },
    // 取消按钮
    cancel() {
      this.dialogFormVisible = false
    }
  }
};
</script>

<style scoped lang="scss">
#container {
  width: 800px;
  height: 400px;
  // margin-left: 50%; transform: translate(-50%,0);
}
</style>
