<template>
  <div class="app-container">
    <!-- <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>仓库费规则设施</span>
      </div>
      <el-row>
        <el-col :span="4">
          <strong>仓储费设置:</strong>
        </el-col>
        <el-col :span="16">
          <div class="set">
            <strong>货品仓库存放</strong
            ><input class="inputs" v-model="input" placeholder="10" /> ><strong
              >天内可免费受存储管理费用.</strong
            >
          </div>
          <div>
            <strong>当超过</strong
            ><input class="inputs" v-model="input" placeholder="10" />
            <strong>天后,按每立方米</strong
            ><input class="inputs" v-model="input" placeholder="10" /><strong
              >元/天计算</strong
            >
          </div>
          <h5>当商品不足1立方米时,按一立方米计算</h5>
        </el-col>
      </el-row>
      <h4 class="zhu">
        注:改规则适用所有经销商,个性化液之后,则不再按该规则执行
      </h4>
    </el-card> -->

    <!-- 筛选条件 -->
    <div>
      <el-form
        ref="screenForm"
        :model="screenForm"
        label-width="70px"
        size="small"
        label-position="left"
      >
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="条件" prop="customerName">
              <el-input
                placeholder="请输入筛选条件"
                v-model="screenForm.customerName"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :lg="18" class="tr">
            <el-form-item label="">
              <el-button size="small" @click="resetScreenForm">清空</el-button>
              <el-button size="small" type="primary" @click="submitScreenForm"
                >搜索</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 按钮 -->
    <div class="btn-group clearfix">
      <div class="fl">
        <el-button
          type="primary"
          size="small"
          @click="(showDialog = true), (type = 1)"
          >新增</el-button
        >

        <el-popconfirm
          confirm-button-text="好的"
          cancel-button-text="不用了"
          icon="el-icon-info"
          icon-color="red"
          title="内容确定删除吗？"
          @onConfirm="hanleDelete"
        >
          <el-button
            type="primary"
            size="small"
            slot="reference"
            class="el-popover-left"
            >删除</el-button
          >
        </el-popconfirm>
      </div>
      <div class="fr">
        <el-upload
          class="import-btn"
          :action="baseURL + 'student/import'"
          :http-request="handleImport"
          :file-list="importFileList"
        >
          <el-button type="primary" size="small">导入</el-button>
        </el-upload>
        <el-button type="primary" size="small" @click="handleExport"
          >导出</el-button
        >
        <el-button type="primary" size="small" @click="hanlePrint"
          >打印</el-button
        >
      </div>
    </div>

    <div class="mymain-container">
      <!-- 列表 -->
      <div class="table">
        <!-- startprint -->
        <el-table
          v-loading="listLoading"
          :data="dataList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
          @select-all="hanleSelectAll"
        >
          <el-table-column type="selection" align="center" min-width="100" />

          <el-table-column
            align="center"
            label="经销商编码"
            prop="customerNumber"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="经销商名称"
            prop="customerName"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="存放免费天数"
            prop="freeDay"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="收费规则(每立方米)"
            prop="toll"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="创建人"
            prop="createBy"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="创建时间"
            prop="createTime"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="更新人"
            prop="updateBy"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="更新时间"
            prop="updateTime"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="操作"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                class="textColor"
                @click="editFn(scope.row.id, scope.row)"
                >编辑</el-button
              >
              <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="内容确定删除吗？"
                @onConfirm="hanleDelete(scope.row.id)"
              >
                <el-button
                  slot="reference"
                  type="text"
                  class="textColor el-popover-left"
                  >删除</el-button
                >
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- endprint -->
      </div>
      <!-- 分页 -->
      <div class="fr">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 弹窗 -->
    <el-dialog
      title="仓库费规则配置"
      :visible.sync="showDialog"
      width="30%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form :model="dialogForm" label-width="100px" label-position="right">
        <el-form-item label="经销商编码" prop="customerNumber">
          <el-select
            v-model="dialogForm.customerNumber"
            placeholder="请选择活动区域"
            class="inputStyle"
            v-el-select-loadmore="loadmore"
          >
            <!-- v-el-select-loadmore="loadMore" -->
            <el-option
              v-for="(item, index) in customerList"
              :key="index"
              :label="item.number"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="经销商名称" prop="customerName">
          <el-input
            v-model="dialogForm.customerName"
            class="inputStyle"
          ></el-input>
        </el-form-item>
        <el-form-item label="仓库存放" class="sty" prop="freeDay">
          <el-input
            type="number"
            v-model="dialogForm.freeDay"
            class="inputStyle"
          />天内可免费受存储管理费用. 当超过
          <el-input
            type="number"
            v-model="dialogForm.freeDay"
            class="inputStyle"
          />天后,按每立方米
          <el-input
            v-model="dialogForm.toll"
            type="number"
            class="inputStyle"
          />
          元/天计算
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="(showDialog = false), kong()">取 消</el-button>
        <el-button type="primary" @click="hanleInfo(dialogForm)"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Mixin from "@/mixin/index";
import { downloadFiles, handleImport } from "@/utils/util";
import {
  getListToll,
  getDetail,
  updateToll,
  addToll,
  deleteToll,
  getCustomerList,
} from "@/api/basic_data/warehouse";
export default {
  mixins: [Mixin],
  data() {
    return {
      dialogForm: {
        id: null,
        customerName: "",
        customerNumber: "",
        freeDay: 0,
        toll: 0,
      },
      sleectBox: {
        currentPage: 1, // 当前页码
        pageSize: 10, // 每页数量
        listTotal: 0, // 列表总数
      },
      baseURL: "",
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      importLoading: false, // 导入加载loading
      importFileList: [], // 导入列表

      customerList: [],
      showDialog: false,
      dataList: [],
      screenForm: {
        customerName: "",
        freeDay: "",
        toll: "",
      },
      type: null, // 1 新增  2 修改  null 详情
    };
  },
  created() {
    this.getList();
    this.getCustomerList();
  },
  directives: {
    "el-select-loadmore": {
      bind(el, binding) {
        // 获取element-ui定义好的scroll盒⼦
        const SELECTWRAP_DOM = el.querySelector(
          ".el-select-dropdown .el-select-dropdown__wrap"
        );
        SELECTWRAP_DOM.addEventListener("scroll", function () {
          /**
           * scrollHeight 获取元素内容⾼度(只读)
           * scrollTop 获取或者设置元素的偏移值,常⽤于, 计算滚动条的位置, 当⼀个元素的容器没有产⽣垂直⽅向的滚动条, 那它的scrollTop的值默认为0.
           * clientHeight 读取元素的可见⾼度(只读)
           * 如果元素滚动到底, 下⾯等式返回true, 没有则返回false:
           * ele.scrollHeight - ele.scrollTop === ele.clientHeight;
           */
          const condition =
            this.scrollHeight - this.scrollTop <= this.clientHeight;
          if (condition) {
            binding.value();
          }
        });
      },
    },
  },
  methods: {
    editFn(id, row) {
      (this.dialogForm = {
        id: id,
        customerName: row.customerName,
        customerNumber: row.customerNumber,
        freeDay: row.freeDay,
        toll: row.toll,
      }),
        (this.type = 2);
      this.showDialog = true;
    },
    hanleInfo(dialogForm) {
      if (this.type === 1) {
        const params = {
          ...dialogForm,
        };
        console.log(params, 78798);
        addToll(params).then((res) => {
          this.$successMsg("保存成功");
          this.kong();
          this.getList();
        });
      } else if (this.type === 2) {
        const paramss = {
          ...this.dialogForm,
        };
        updateToll(paramss).then((res) => {
          this.$successMsg("编辑成功");
          this.kong();
          this.diaLogForm.id = null;
          this.getList();
        });
      }
      this.showDialog = false;
    },
    getList() {
      this.listLoading = true;
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        customerName: this.screenForm.customerName,
        freeDay: this.screenForm.freeDay,
        toll: this.screenForm.toll,
      };
      getListToll(params).then((res) => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
      });
    },
    loadmore() {
      this.sleectBox.currentPage++;
      this.getCustomerList();
    },
    getCustomerList() {
      let params = {
        pageNum: this.sleectBox.currentPage,
        pageSize: this.sleectBox.pageSize,
        keyword: "",
      };
      getCustomerList(params).then((res) => {
        if (res.data.records.length) {
          this.customerList = [...this.customerList, ...res.data.records];
        }
        // this.listTotal = res.data.total;
      });
    },
    // hanleDetail(detail) {
    //   this.type = null;
    //   this.showDialog = true;
    //   this.diaLogForm = detail;
    // },

    handleExport() {
      let screenData = {
        customerName: this.screenForm.customerName,
        freeDay: this.screenForm.freeDay,
        toll: this.screenForm.toll,
      };
      downloadFiles("/stock/exportToll", screenData);
    },
    // 删除数据
    hanleDelete(id) {
      deleteToll({ id }).then((res) => {
        this.$successMsg("删除成功");
        this.getList();
      });
    },
    // 导入
    async handleImport(param) {
      this.importLoading = true;
      const file = param.file;
      console.log(file, 123);
      const formData = new FormData();
      formData.append("file", file);
      let result = await handleImport("/stock/importToll", formData);
      this.importLoading = false;
      this.importFileList = [];
      if (result.code == 200) {
        this.$alert(result.message, "导入成功", {
          confirmButtonText: "确定",
        });
        this.getList();
      } else {
        this.$alert(result.message, "导入失败", {
          confirmButtonText: "确定",
        });
      }
    },
    kong() {
      this.dialogForm = {
        customerName: "",
        customerNumber: "",
        freeDay: 0,
        toll: 0,
      };
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .sty {
  width: 85%;
  .el-form-item__content .el-input {
    width: 22%;
    margin: 0px 10px 16px 0;
  }
}
.zhu {
  padding-top: 18px;
}
.import-btn {
  display: inline-block;
  margin-right: 10px;
}
.set {
  margin-bottom: 30px;
}
.inputs {
  margin: 0 18px;
}
::v-deep .el-form {
  .inputStyle {
    width: 80%;
  }
}
::v-deep .dialog-footer {
  display: flex;
  justify-content: center;
}
::v-deep .el-dialog__header {
  background-color: #dddddd;
}
.inp {
  margin: 0 12px;
}

.right {
  margin-top: 12px;
  float: right;
}

.table {
  margin-top: 12px;
}
.search {
  display: flex;
  margin-top: 12px;
  ::v-deep .el-input {
    width: 50%;
    margin-right: 12px;
  }
}
.main {
  padding: 12px;
}
</style>
>
