<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-refresh"
          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
          v-hasPermi="['billing:template:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['billing:template:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['billing:template:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['billing:template:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="templateList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="${comment}" align="center" prop="id" />-->
      <el-table-column label="模板名称" align="center" prop="name" />
      <el-table-column label="模板备注" align="center" prop="remark" />
      <el-table-column label="车辆类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.vehicle_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <!--<el-table-column label="充电站" align="center" prop="stationId" />-->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['billing:template:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            v-hasPermi="['billing:template:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</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="70%"
      append-to-body
      style="overflow: hidden"
      @close="closeDialog"
    >
      <!-- body -->
      <add-billing @success="success" :billingTemplateId="billingTemplateId" ref="addBill"></add-billing>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTemplate,
  getTemplate,
  delTemplate,
  addTemplate,
  updateTemplate,
} from "@/api/billing/template";
import basic from "./components/basic.vue";
import AddBilling from "./components/addBilling.vue";

export default {
  name: "Template",
  components: { basic, AddBilling },
  dicts: ["vehicle_type"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPileBillingDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 计费模板表格数据
      templateList: [],

      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      form: {
        type: "1",
      },
      title: "",
      openOld: null,
      // 需要向子组件传递的值
      billingTemplateId: ""
    };
  },
  created() {
    this.getList();
  },
  methods: {
    closeDialog() {
      console.log("closeDialog");
      this.billingTemplateId = "";
    },
    // 通知父 关闭
    success() {
      this.open = false;
      this.$refs.addBill.resetForm();
      this.getList();
    },
    /** 查询计费模板列表 */
    getList() {
      this.loading = true;
      listTemplate(this.queryParams).then((response) => {
        this.templateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 确定按钮
    // 表单重置
      reset() {
      this.form = {
        name: null,
        remark: null,
        type: "1",
      };
      this.pileBillingDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.loading = true;
      setTimeout(() => {
        this.queryParams.pageNum = 1;
        this.getList();
        this.loading = false;
      }, 800);
    },
    /** 重置按钮操作 */
    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) {
      console.log("点击修改按钮", row);
      this.billingTemplateId = String(row.id);
      // this.reset();
      this.open = true;
      this.title = "修改计费模板";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.pileBillingDetailList = this.pileBillingDetailList;
          if (this.form.id != null) {
            updateTemplate(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTemplate(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 delTemplate(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 计费模板详情序号 */
    rowPileBillingDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },

    /** 复选框选中数据 */
    handlePileBillingDetailSelectionChange(selection) {
      this.checkedPileBillingDetail = selection.map((item) => item.index);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "billing/template/export",
        {
          ...this.queryParams,
        },
        `template_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped lang="scss">
.txt {
  margin-left: 150px;
  margin-bottom: 20px;
  border: 1px solid #fff;
  background-color: skyblue;
}
.btn {
  margin-left: 150px;
  width: 180px;
  height: 70px;
  border: 1px solid green;
  .p1 {
    font-size: 18px;
    padding: 2px;
    height: 35px;
    background-color: aquamarine;
  }
  .energy {
    margin: 5px;
  }
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.frame {
  width: 126px;
  height: 36px;
  line-height: 36px;
  background-color: #409eff;
}

.el-tag + .el-tag {
  margin-left: 10px;
  // min-height: 100px;
  // padding-bottom: 95px;
}
</style>
