<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="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="充电桩状态"
          clearable
          style="width: 140px"
        >
          <el-option
            v-for="dict in dict.type.pile_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </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" icon="el-icon-refresh-right" size="mini" @click="handleQuery">刷新</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['pile:basic:batchAdd']">批量新增</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:basic: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:basic: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:basic:export']"
        >导出
        </el-button>
      </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="basicList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="桩号" align="center" prop="sn">
        <template slot-scope="scope">
          <router-link
            :to="'/pile/detail/index/' + scope.row.pileId"
            class="link-type"
          >
            <span>{{ scope.row.pileSn }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.pile_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="接口数量" align="center" prop="gunNum" />
      <el-table-column label="电桩类型" align="center" prop="pileType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.connector_type"
            :value="scope.row.pileType"
          />
        </template>
      </el-table-column>
      <el-table-column label="sim卡号" align="center" prop="iccid" width="220px"/>
      <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="merchantName"
        width="220px"
      />
      <el-table-column label="充电站" align="center" prop="stationName" />
      <el-table-column label="密钥" align="center" prop="secretKey" />
      <el-table-column
        label="注册时间"
        align="center"
        prop="registrationTime"
      />
      <!--<el-table-column label="备注" align="center" prop="remark"/>-->
      <!--<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:basic:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pile:basic: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="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="运营商" prop="merchantId">
              <el-select
                v-model="form.merchantId"
                placeholder="请选择运营商"
                @change="changeSelectMerchant(form.merchantId)"
              >
                <el-option
                  v-for="item in merchantList"
                  :key="item.merchantName"
                  :label="item.merchantName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="站点" prop="stationId">
              <el-select v-model="form.stationId" placeholder="请选择站点">
                <el-option
                  v-for="item in stationList"
                  :key="item.id"
                  :label="item.stationName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="软件协议" prop="softwareProtocol">
              <el-select
                v-model="form.softwareProtocol"
                placeholder="软件协议"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.software_protocol"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产日期" prop="productionDate">
              <el-date-picker
                clearable
                v-model="form.productionDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择生产日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="产品型号" prop="modelId">
              <el-select v-model="form.modelId" placeholder="请选择产品型号">
                <el-option
                  v-for="item in modelList"
                  :key="item.modelName"
                  :label="item.modelName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="桩类型" prop="modelId">
              <el-select
                v-model="form.chargerPileType"
                placeholder="请选择桩类型"
              >
                <el-option label="运营桩" value="1" />
                <el-option label="个人桩" value="2" />
              </el-select>
            </el-form-item>
            <!--<el-form-item label="充电桩型号" prop="modelId">
              <el-input v-model="form.modelId" placeholder="请输入充电桩型号"/>
            </el-form-item>-->
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="枪口数量" prop="connectorNum">
              <el-input
                v-model="form.connectorNum"
                placeholder="请输入枪口数量"
                style="width: 220px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产数量" prop="num">
              <el-input
                v-model="form.num"
                placeholder="请输入生产数量"
                style="width: 220px"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                placeholder="请输入备注"
                style="width: 95%"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </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 {
  listBasic,
  getBasic,
  delBasic,
  addBasic,
  updateBasic,
  batchAddBasic,
} from "@/api/pile/basic";

import { getMerchantList } from "@/api/pile/merchant";
import { getStationListByMerchantId } from "@/api/pile/station";
import { listModel } from "@/api/pile/model";

export default {
  name: "Basic",
  dicts: ["pile_status", "software_protocol", "sim_supplier", "connector_type"],
  data() {
    return {
      // 定时器
      timer: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      basicList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pileSn: null,
        status: null,
        businessType: null,
        softwareProtocol: null,
        productionDate: null,
        licenceId: null,
        modelId: null,
        ICCID: null,
        simSupplier: null,
        merchantId: null,
        stationId: null,
        faultReason: null,
      },
      createPile: {
        merchantId: null,
        stationId: null,
      },
      // 运营商列表
      merchantList: [],
      // 站点列表
      stationList: [],
      // 型号列表
      modelList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        merchantId: [
          { required: true, message: "请选择运营商", trigger: "blur" },
        ],
        // stationId: [{ required: true, message: "请选择站点", trigger: "blur" }],
        softwareProtocol: [
          { required: true, message: "请输入软件协议", trigger: "blur" },
        ],
        productionDate: [
          { required: true, message: "请选择生产日期", trigger: "blur" },
        ],
        modelId: [
          { required: true, message: "请输入产品型号", trigger: "blur" },
        ],
        connectorNum: [
          { required: true, message: "请输入枪口数量", trigger: "blur" },
        ],
        num: [{ required: true, message: "请输入生产数量", trigger: "blur" }],
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();

    this.getModelList();
  },
  methods: {
    changeSelectMerchant(value) {
      // 清空内容
      this.createPile.stationId = "";
      // 通过接口查
      getStationListByMerchantId(value).then((response) => {
        // console.log(response.data);
        this.stationList = response.data;
      });
    },
    getMerchantList() {
      getMerchantList().then((response) => {
        this.merchantList = response.rows;
      });
    },
    getModelList() {
      listModel().then((response) => {
        this.modelList = response.rows;
        // console.log(this.modelList);
      });
    },
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      listBasic(this.queryParams).then((response) => {
        this.basicList = response.rows;
        console.log(this.basicList, "首页信息");
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sn: null,
        status: "0",
        businessType: null,
        softwareProtocol: null,
        productionDate: null,
        licenceId: null,
        modelId: null,
        ICCID: null,
        simSupplier: null,
        merchantId: null,
        stationId: null,
        faultReason: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1
      };
      // 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.getMerchantList();
      this.title = "批量添加设备";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBasic(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log("提交按钮", this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBasic(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            batchAddBasic(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitBatchAdd() {
      batchAddBasic(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 delBasic(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "pile/basic/export",
        {
          ...this.queryParams,
        },
        `basic_${new Date().getTime()}.xlsx`
      );
    },
  },
  mounted() {
    // 开始定时器
    // this.getList();
    // this.timer = setInterval(() => {
    //   setTimeout(this.getList, 0);
    // }, 6000 * 60 * 60);
  },
  // 销毁定时器
  beforeDestroy() {
    clearInterval(this.timer);
    this.timer = null;
  },
};
</script>
