<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="pileSn">
        <el-input
          v-model="queryParams.pileSn"
          placeholder="桩编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ICCID" prop="iccId">
        <el-input
          v-model="queryParams.iccId"
          placeholder="请输入ICCID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="套餐总流量" prop="totalData">-->
<!--        <el-input-->
<!--          v-model="queryParams.totalData"-->
<!--          placeholder="请输入套餐总流量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="剩余流量" prop="surplusData">-->
<!--        <el-input-->
<!--          v-model="queryParams.surplusData"-->
<!--          placeholder="请输入剩余流量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="到期时间" prop="expireTime">-->
<!--        <el-date-picker clearable-->
<!--                        v-model="queryParams.expireTime"-->
<!--                        type="date"-->
<!--                        value-format="yyyy-MM-dd"-->
<!--                        placeholder="请选择到期时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="SIM卡商" prop="simSupplier">-->
<!--        <el-input-->
<!--          v-model="queryParams.simSupplier"-->
<!--          placeholder="请输入SIM卡商"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="到期时间" prop="expiredTime">
        <el-select v-model="queryParams.expiredTime" clearable placeholder="请选择">
          <el-option
            v-for="item in dict.type.sim_expired_time"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="SIM卡商" prop="simSupplier">
      <el-select v-model="queryParams.simSupplier" clearable placeholder="请选择">
        <el-option
          v-for="item in dict.type.sim_supplier"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="站点" prop="stationId">
        <el-select
          v-model="queryParams.stationId"
          placeholder="请选择站点"
          clearable
          filterable
          style="width: 140px"
        >
          <el-option
            v-for="station in stationList"
            :key="station.id"
            :label="station.stationName"
            :value="station.id"
          />
        </el-select>
      </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:sim:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleRenewSim"
          v-hasPermi="['pile:sim: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:sim: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:sim:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="simList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="ICCID" align="center" prop="iccId" width="180px"/>
      <el-table-column label="套餐名称" align="center" prop="name" />
      <el-table-column label="对应桩号" align="center" prop="pileSn" width="180px"/>
      <el-table-column label="所属站点" align="center" prop="stationName" />
      <el-table-column label="sim卡商" align="center" prop="simSupplier" >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sim_supplier"
            :value="scope.row.simSupplier"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="simCardStatus" >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sim_status"
            :value="scope.row.simCardStatus"
          />
        </template>

      </el-table-column>
      <el-table-column label="套餐总流量" align="center" prop="totalData" />
      <el-table-column label="剩余流量" align="center" prop="surplusData" />
      <el-table-column label="到期时间" align="center" prop="expireTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="剩余天数" align="center" prop="remainingDays" >
        <template scope="scope">
          <span v-if="scope.row.remainingDays <= 7 " style="color: #bf1c1c"> {{scope.row.remainingDays}}</span>
          <span v-else-if="scope.row.remainingDays <= 30 " style="color: #ffba00"> {{scope.row.remainingDays}}</span>
          <span v-else-if="scope.row.remainingDays > 30 "> {{scope.row.remainingDays}}</span>
        </template>
      </el-table-column>
      <el-table-column label="SIM卡运营商" align="center" prop="operator" >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sim_operator"
            :value="scope.row.operator"
          />
        </template>

      </el-table-column>
      <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:sim:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleRenewSim(scope.row)"
            v-hasPermi="['pile:sim:edit']"
          >续期</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pile:sim: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"
    />

    <!-- 添加或修改充电桩SIM卡信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="套餐名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入套餐名称" />
        </el-form-item>
        <el-form-item label="ICCID" prop="iccid">
          <el-input v-model="form.iccid" placeholder="请输入ICCID" />
        </el-form-item>
        <el-form-item label="套餐总流量" prop="totalData">
          <el-input v-model="form.totalData" placeholder="请输入套餐总流量" />
        </el-form-item>
        <el-form-item label="剩余流量" prop="surplusData">
          <el-input v-model="form.surplusData" placeholder="请输入剩余流量" />
        </el-form-item>
        <el-form-item label="到期时间" prop="expireTime">
          <el-date-picker clearable
                          v-model="form.expireTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="SIM卡运营商" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入SIM卡运营商" />
        </el-form-item>
        <el-form-item label="删除标识" prop="delFlag">
          <el-input v-model="form.delFlag" 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>

    <!-- 续费按钮对话框  -->
    <el-dialog title="sim卡续费" @close="closeRenewSim" :visible.sync="openRenewSim" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="续费周期" prop="cycleNumber">
          <el-input v-model="simRenewCycleNumber" placeholder="请输入续费周期(大于12或为12的倍数)" @input="changeInput"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSimRenewCycleNumber">确 定</el-button>
        <el-button @click="resetSimRenewCycleNumber">取 消</el-button>
      </div>
    </el-dialog>



  </div>
</template>

<script>
import {listSim, getSim, delSim, addSim, updateSim, simRenew, getSimInfo} from "@/api/pile/sim";
import {listStation} from "@/api/pile/station";

export default {
  name: "Sim",
  dicts: ["sim_supplier", "sim_status", "sim_operator", "sim_expired_time"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充电桩SIM卡信息表格数据
      simList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openRenewSim: false,
      simRenewCycleNumber: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        iccId: null,
        status: null,
        totalData: null,
        surplusData: null,
        expireTime: null,
        operator: null,
        pileSn: null,
        simSupplier : null,
        stationId: null,
      },
      value: '',
      // 表单参数
      form: {},
      // 站点列表
      stationList: [],
      // 表单校验
      rules: {
      },
      iccidArray: []
    };
  },
  created() {
    this.getList();
    // 查询站点列表
    this.getStationList();
  },
  methods: {
    /** 查询充电桩SIM卡信息列表 */
    getList() {
      this.loading = true;
      // listSim(this.queryParams).then(response => {
      //   this.simList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });

      getSimInfo(this.queryParams).then(res =>{
        this.simList = res.rows;
        this.total = res.total;
        this.loading = false;
      })

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        iccid: null,
        status: "0",
        totalData: null,
        surplusData: null,
        expireTime: null,
        operator: 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.iccidArray = selection.map(item => item.iccid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充电桩SIM卡信息";
    },
    changeInput() {
      const pattern = /^[1-9][0-9]*$/; // 正整数的正则表达式
      // 不符合正整数时
      if (!pattern.test(this.simRenewCycleNumber)) {
        // input 框绑定的内容为空
        this.simRenewCycleNumber = ''
      }
    },
    // 关闭续费 对话框
    closeRenewSim() {
      console.log("关闭续费 对话框");
      this.resetSimRenewCycleNumber();
    },
    /** 续费按钮操作 */
    handleRenewSim(row) {
      this.openRenewSim = true;
      this.iccidArray.push(row.iccid);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSim(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充电桩SIM卡信息";
      });
    },
    /** 查询充电站信息列表 */
    getStationList() {
      const queryStationParams = {
        pageNum: 1,
        pageSize: 999
      };
      listStation(queryStationParams).then((response) => {
        console.log("订单列表页-查询站点列表", response)
        this.stationList = response.rows;
      });
    },
    // 重置 续费周期相关
    resetSimRenewCycleNumber() {
      console.log("重置 续费周期相关");
      this.openRenewSim = false;
      this.simRenewCycleNumber = "";
      this.iccidArray = [];
    },
    // 点击续费提交按钮
    submitSimRenewCycleNumber() {
      const param = {
        iccIds: this.iccidArray,
        cycleNumber: this.simRenewCycleNumber
      };
      console.log("sim卡续费 param:", param);
      simRenew(param).then( response => {
        console.log("sim卡续费 response:", response);
        this.$modal.msgSuccess("续费成功！");
        this.openRenewSim = false;
        this.getList();
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSim(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSim(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除充电桩SIM卡信息编号为"' + ids + '"的数据项？').then(function() {
        return delSim(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pile/sim/export', {
        ...this.queryParams
      }, `sim_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
