<template>
  <div class="order">
    <div class="frame">
      <!-- <el-autocomplete
        class="mr20"
        label
        v-model="parameter.params.id"
        :fetch-suggestions="querySearchAsync"
        value-key="refineryName"
        value="id"
        placeholder="炼厂名称"
      ></el-autocomplete> -->

      <autocomplete :params="parameter.params" :config="configAutocomplete" />

      <el-select
        v-model="parameter.params.refineryType"
        placeholder="炼厂类型"
        clearable
      >
        <el-option
          v-for="item in refineryTypeEnum"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-select
        v-model="parameter.params.accountStatus"
        placeholder="启用禁用"
        clearable
      >
        <el-option label="启用" value="ENABLE"> </el-option>
        <el-option label="禁用" value="DISABLE"> </el-option>
      </el-select>
      <div class="buttons">
        <el-button icon="el-icon-search" @click="search">查询</el-button>
        <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
      </div>
    </div>
    <div class="table">
      <div class="operation">
        <el-button @click="addition">新增炼厂账户</el-button>
        <el-button @click="addition">充值</el-button>
      </div>
      <el-table
        v-if="tableHeight"
        ref="multipleTable"
        :height="tableHeight"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="orderSerialNumber"
          label="炼厂账户名称"
          show-overflow-tooltip
        >
          <template slot-scope="{ row }">
            <el-tag
              v-if="row.accountStatus === 'ENABLE'"
              type="success"
              size="mini"
              >启用</el-tag
            >
            <el-tag v-else type="danger" size="mini">禁用</el-tag>
            <span>{{ row.accountName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="refineryName"
          label="炼厂名称"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="orderSerialNumber" label="账户类型" width="90">
          <template slot-scope="{ row }">
            <p v-if="row.accountType">
              {{
                refineryAccountTypeEnum.find(
                  (item) => item.value === row.accountType
                ).label
              }}
            </p>
          </template>
        </el-table-column>
        <el-table-column prop="balance" label="总余额" width="90">
        </el-table-column>
        <el-table-column prop="rechargeBalance" label="充值余额" width="100">
        </el-table-column>
        <el-table-column
          prop="totalRechargeBalance"
          label="累计充值余额"
          width="120"
        >
        </el-table-column>

        <el-table-column prop="orderSerialNumber" label="操作" width="100">
          <template slot-scope="{ row }">
            <el-link type="primary" :underline="false" @click="detail(row)"
              >详情</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pagination :parameter="parameter" @searchAgain="getByPage" />
    </div>
    <add
      :controlWindows="controlWindows"
      :refineryAccountTypeEnum="refineryAccountTypeEnum"
      @closeWindow="search"
    />

    <el-drawer
      title="详情"
      direction="ltr"
      size="60%"
      :withHeader="false"
      :visible="controlWindows.detail"
    >
      <general-details
        title="详情"
        :isHeader="true"
        v-if="controlWindows.detail"
        :sourceData="oilCompanyMatch"
        :mappingData="mappingData"
        @close="controlWindows.detail = false"
      >
      </general-details>
    </el-drawer>
  </div>
</template>

<script>
import refineryInfoServe from "api/refineryInfo.js";
import serve from "api/refineryAccount.js";

import add from "./components/add.vue";

import pagination from "components/pagination/index.vue";
import autocomplete from "components/autocomplete/index.vue";
import generalDetails from "components/generalDetails/index.vue";

import { refineryTypeEnum, refineryAccountTypeEnum } from "utils/dataType.js";

export default {
  components: {
    add,
    pagination,
    autocomplete,
    generalDetails,
  },
  data() {
    return {
      refineryTypeEnum: refineryTypeEnum,
      refineryAccountTypeEnum: refineryAccountTypeEnum,
      controlWindows: {
        add: false,
        addInfo: {},
        detail: false,
      },
      configAutocomplete: {
        serveTarget: refineryInfoServe.findByEntity,
        autocomplateKey: "refineryName",
        valueKey: "refineryName",
        placeholder: "炼厂名称",
        querykey: "refineryId",
      },
      refineryList: [],
      tableHeight: 0,
      tableData: [],
      parameter: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
        params: {},
      },
      oilCompanyMatch: {},
      mappingData: [
        {
          carTitle: "",
          carItems: [
            { label: "账户名称", value: "accountName" },
            { label: "账户类型", value: "refineryType" },
            { label: "炼厂名称", value: "refineryName" },
            { label: "炼厂ID", value: "refineryId" },
            { label: "启用状态", value: "accountStatus" },
            { label: "创建用户ID", value: "createUser" },
            { label: "创建时间", value: "createTime" },
            { label: "修改用户ID", value: "updateUser" },
            { label: "修改时间", value: "updateTime" },
          ],
        },
      ],
    };
  },

  mounted() {
    this.$nextTick(() => {
      this.heightHandle();
    });
    window.addEventListener(
      "resize",
      this.$utils.debounce(this.heightHandle, 500)
    );
  },
  created() {
    this.getByPage();
  },
  methods: {
    search() {
      this.parameter.currentPage = 1;
      this.getByPage();
    },
    //table list
    getByPage() {
      serve.getByPage(this.parameter).then((res) => {
        this.tableData = res.data.list;
        this.parameter.total = res.data.totalCount;
      });
    },
    //炼厂list
    // findByEntity() {
    //   refineryInfoServe.findByEntity().then((res) => {
    //     this.refineryList = res.data;
    //   });
    // },
    //远程搜索
    // querySearchAsync(queryString, cb) {
    //   if (queryString) {
    //     refineryInfoServe
    //       .findByEntity({ refineryName: queryString })
    //       .then((res) => {
    //         let timeInstance = setTimeout(() => {
    //           clearTimeout(timeInstance);
    //           if (res.data.length) {
    //             cb(res.data);
    //           } else cb([]);
    //         }, 1000 * Math.random());
    //       });
    //   } else cb([]);
    // },
    //新增
    addition() {
      this.controlWindows.addInfo.title = "账户新增";
      this.controlWindows.add = true;
    },
    //修改
    update(row) {
      this.controlWindows.addInfo = {
        title: "账户新增",
        ...row,
      };
      this.controlWindows.add = true;
    },
    //详情
    detail(row) {
      this.oilCompanyMatch = row;
      this.controlWindows.detail = true;
    },
    //重置
    reset() {
      this.parameter = {
        currentPage: 1,
        pageSize: 10,
        total: 0,
        params: {},
      };
    },
    // table height
    heightHandle() {
      let bodyHeight = document.body.clientHeight;
      let frameHeight = this.obtainElement(".frame").clientHeight;
      let operationHeight = this.obtainElement(".operation").clientHeight;
      let paginationHeight = this.obtainElement(".el-pagination").clientHeight;
      this.tableHeight =
        bodyHeight - frameHeight - operationHeight - paginationHeight - 145;
    },
    obtainElement(className) {
      return document.documentElement.querySelector(className);
    },
  },
};
</script>

<style lang="scss" scoped>
.order {
  .frame {
    margin: 20px;
    padding: 20px;
    // width: 100%;
    border-radius: 6px;
    border: 1px solid #e3e3e5;
    background: #fff;
    .el-input,
    .el-select {
      width: 183px;
      height: 40px;
    }
    .el-autocomplete + .el-select,
    .el-input + .el-input,
    .el-input + .el-select,
    .el-select + .el-select,
    .el-select + .el-input {
      margin-right: 20px;
      margin-bottom: 15px;
    }
    .mr20 {
      margin-right: 20px;
    }
    .buttons {
      text-align: right;
    }
  }

  .table {
    overflow: hidden;
    margin: 0 20px;
    padding-bottom: 20px;
    background: #fff;
    border-radius: 6px;
    border: 1px solid #e3e3e5;
    > .operation {
      box-sizing: content-box;
      padding: 15px;
    }
    .gray {
      color: #999;
      span {
        color: #333;
      }
    }
    .el-table {
      margin-bottom: 20px;
      border-radius: 10px 10px 0px 0px;
    }
    .el-link {
      margin-right: 10px;
    }
  }
}
</style>
