<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="站点名称" prop="stationName">
        <el-input
          v-model="queryParams.stationName"
          placeholder="请输入站点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pile:station:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pile:station:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pile:station:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pile:station:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="stationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="80" align="center" />
      <!--<el-table-column label="" align="center" prop="id" />-->
      <el-table-column label="站点名称" align="center" prop="stationName">
        <template slot-scope="scope">
          <router-link
            :to="'/station/detail/index/' + scope.row.id"
            class="link-type"
          >
            <span>{{ scope.row.stationName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <!--<el-table-column label="容量" align="center" prop="capacity" />-->
      <!--<el-table-column label="充电站国家代码" align="center" prop="countryCode" />-->
      <el-table-column label="省市区" align="center" prop="areaCode">
        <template slot-scope="scope">
          {{ getCodeToText(scope.row.areaCode, null) }}
        </template>
      </el-table-column>
      <el-table-column label="站点地址" align="center" prop="address" />
      <el-table-column label="所属运营商" align="center" prop="merchantName" />
      <!--<el-table-column label="站点电话" align="center" prop="stationTel" />-->
      <!--<el-table-column label="服务电话" align="center" prop="serviceTel" />-->
      <el-table-column label="站点类型" align="center" prop="stationType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.station_type"
            :value="scope.row.stationType"
          />
        </template>
      </el-table-column>
      <el-table-column label="站点状态" align="center" prop="stationStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.station_status"
            :value="scope.row.stationStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="充电设备数量" align="center" prop="pileNum" />
      <el-table-column label="是否对外开放" align="center" prop="publicFlag">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.publicFlag"
            active-value="1"
            inactive-value="0"
            @change="changeFlag(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否营业中" align="center" prop="openFlag">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.openFlag"
            active-value="1"
            inactive-value="0"
            @change="changeFlag(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="管理员" align="center" prop="merchantAdminName" />

      <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pile:station:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pile:station:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充电站信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属运营商">
          <el-select v-model="form.merchantId" placeholder="请选择运营商">
            <el-option
              v-for="item in merchantList"
              :key="item.merchantName"
              :label="item.merchantName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="站点名称" prop="stationName">
          <el-input v-model="form.stationName" placeholder="请输入站点名称" />
        </el-form-item>
        <el-form-item label="省市区" prop="areaCode">
          <!--<el-input v-model="form.areaCode" placeholder="请输入充电站省市辖区编码" />-->
          <el-cascader
            size="large"
            :options="options"
            v-model="form.areaCode"
            @change="handleChange"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="站点地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入站点地址" />
        </el-form-item>

        <el-form-item label="站点管理员" prop="merchantAdminName">
          <el-input v-model="form.stationAdminName" placeholder="站点管理员" />
        </el-form-item>

        <el-form-item label="站点电话" prop="stationTel">
          <el-input v-model="form.stationTel" placeholder="站点电话" />
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStation,
  getStation,
  delStation,
  addStation,
  updateStation,
  fastCreateStation,
} from "@/api/pile/station";
import { getMerchantList } from "@/api/pile/merchant";
import { regionData, CodeToText } from "element-china-area-data";

export default {
  name: "Station",
  dicts: ["station_status", "station_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充电站信息表格数据
      stationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationName: null,
        aloneApply: null,
        accountNumber: null,
        capacity: null,
        publicParking: null,
        parkingNumber: null,
        countryCode: null,
        areaCode: null,
        address: null,
        stationTel: null,
        serviceTel: null,
        stationType: null,
        stationStatus: null,
        parkNums: null,
        stationLng: null,
        stationLat: null,
        siteGuide: null,
        construction: null,
        pictures: null,
        matchCars: null,
        parkInfo: null,
        parkOwner: null,
        parkManager: null,
        openAllDay: null,
        businessHours: null,
        parkFree: null,
        payment: null,
        supportOrder: null,
        toiletFlag: null,
        storeFlag: null,
        restaurantFlag: null,
        loungeFlag: null,
        canopyFlag: null,
        printerFlag: null,
        barrierFlag: null,
        parkingLockFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stationName: [
          { required: true, message: "请输入站点名称", trigger: "blur" },
        ],
        areaCode: [
          { required: true, message: "请输入选择区域", trigger: "blur" },
        ],
        address: [
          { required: true, message: "请输入站点地址", trigger: "blur" },
        ],
      },
      options: regionData,
      selectedOptions: [],
      merchantList: [],
    };
  },
  created() {
    this.getList();
    this.getMerchantList();
  },
  methods: {
    getMerchantList() {
      getMerchantList().then((response) => {
        this.merchantList = response.rows;
      });
    },
    changeFlag(info) {
      // console.log(info);
      updateStation(info).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    getCodeToText(codeStr, codeArray) {
      if (null === codeStr && null === codeArray) {
        return null;
      } else if (null === codeArray) {
        codeArray = codeStr.split(",");
      }

      let area = "";
      switch (codeArray.length) {
        case 1:
          area += CodeToText[codeArray[0]];
          break;
        case 2:
          area += CodeToText[codeArray[0]] + "/" + CodeToText[codeArray[1]];
          break;
        case 3:
          area +=
            CodeToText[codeArray[0]] +
            "/" +
            CodeToText[codeArray[1]] +
            "/" +
            CodeToText[codeArray[2]];
          break;
        default:
          break;
      }
      return area;
    },
    handleChange(value) {
      let areaCode = value.join(",");
      console.log("选择区域", areaCode);
      this.form.areaCode = areaCode;
    },
    /** 查询充电站信息列表 */
    getList() {
      this.loading = true;
      listStation(this.queryParams).then((response) => {
        this.stationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stationName: null,
        aloneApply: null,
        accountNumber: null,
        capacity: null,
        publicParking: null,
        parkingNumber: null,
        countryCode: null,
        areaCode: null,
        address: null,
        stationTel: null,
        serviceTel: null,
        stationType: null,
        stationStatus: "0",
        parkNums: null,
        stationLng: null,
        stationLat: null,
        siteGuide: null,
        construction: null,
        pictures: null,
        matchCars: null,
        parkInfo: null,
        parkOwner: null,
        parkManager: null,
        openAllDay: null,
        businessHours: null,
        parkFree: null,
        payment: null,
        supportOrder: null,
        remark: null,
        toiletFlag: null,
        storeFlag: null,
        restaurantFlag: null,
        loungeFlag: null,
        canopyFlag: null,
        printerFlag: null,
        barrierFlag: null,
        parkingLockFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充电站信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getStation(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充电站信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateStation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // addStation(this.form).then((response) => {
            //   this.$modal.msgSuccess("新增成功");
            //   this.open = false;
            //   this.getList();
            // });
            this.fastCreateStation();
          }
        }
      });
    },
    // 快速建站
    fastCreateStation() {
      console.log("点击快速建站");
      fastCreateStation(this.form).then((response) => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除充电站信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delStation(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "pile/station/export",
        {
          ...this.queryParams,
        },
        `station_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
