<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div>
      <Collapse :screen-form="screenForm">
        <template #right_btn>
          <el-button size="mini" @click="resetScreenForm">清空</el-button>
          <el-button size="mini" type="primary" @click="submitScreenForm">搜索</el-button>
        </template>
        <template #search>
          <el-form ref="screenForm" :model="screenForm" label-width="120px" size="mini" 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-row>
          </el-form>
        </template>
      </Collapse>
    </div>
    <!-- 按钮 -->
    <div class="btn-group clearfix">
      <div class="fl">
        <el-button
          type="primary"
          size="mini"
          v-if="$checkBtnRole('add', $route.meta.roles)"
          icon="el-icon-plus"
          @click=";(showDialog = true), (type = 1)"
          >新增</el-button
        >

        <el-popconfirm
          v-if="$checkBtnRole('del', $route.meta.roles)"
          confirm-button-text="好的"
          cancel-button-text="不用了"
          icon="el-icon-info"
          icon-color="red"
          title="内容确定删除吗？"
          @onConfirm="hanleDelete"
        >
          <el-button type="primary" size="mini" icon="el-icon-delete" 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="mini">导入</el-button>
        </el-upload>
        <el-button type="primary" size="mini" @click="handleExport">导出</el-button>
        <!-- <el-button type="primary" size="mini" @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"
          @select="hanleSelectAll"
        >
          <el-table-column type="selection" align="left" min-width="100" />

          <el-table-column align="left" label="经销商编码" prop="customerNumber" min-width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <CopyButton :copyText="scope.row.customerNumber" />
              <span>{{ scope.row.customerNumber }}</span>
            </template>
          </el-table-column>
          <el-table-column align="left" label="经销商名称" prop="customerName" min-width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <CopyButton :copyText="scope.row.customerName" />
              <span>{{ scope.row.customerName }}</span>
            </template>
          </el-table-column>
          <el-table-column align="right" label="存放免费天数" prop="freeDay" min-width="160" show-overflow-tooltip />
          <el-table-column align="right" label="收费规则(每立方米)" prop="toll" min-width="160" show-overflow-tooltip />
          <el-table-column align="left" label="创建人" prop="createBy" min-width="200" show-overflow-tooltip />
          <el-table-column align="left" label="创建时间" prop="createTime" min-width="200" show-overflow-tooltip />
          <el-table-column align="left" label="更新人" prop="updateBy" min-width="200" show-overflow-tooltip />
          <el-table-column align="left" label="更新时间" prop="updateTime" min-width="200" show-overflow-tooltip />
          <el-table-column align="center" label="操作" fixed="right" min-width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button
                type="text"
                v-if="$checkBtnRole('edit', $route.meta.roles)"
                class="textColor"
                @click="editFn(scope.row.id, scope.row)"
                >编辑</el-button
              >
              <el-popconfirm
                v-if="$checkBtnRole('del', $route.meta.roles)"
                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="50%"
      :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="请选择活动区域"
            filterable
            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"
            @mousewheel.native.prevent
            v-model="dialogForm.freeDay"
            class="inputStyle"
          />天内可免费受存储管理费用.
          <br />
          当超过
          <el-input
            type="number"
            @mousewheel.native.prevent
            v-model="dialogForm.freeDay"
            class="inputStyle"
          />天后,按每立方米
          <el-input v-model="dialogForm.toll" type="number" @mousewheel.native.prevent 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 { addToll, deleteToll, getCustomerList, getListToll, updateToll } 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 详情
      isCollapse: true
    }
  },
  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.getList()
          this.$successMsg('保存成功')
          this.kong()
        })
      } else if (this.type === 2) {
        const paramss = {
          ...this.dialogForm
        }
        updateToll(paramss).then(res => {
          this.$successMsg('编辑成功')
          this.getList()
          this.kong()
          this.diaLogForm.id = null
        })
      }
      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: 1,
        pageSize: -1,
        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>
>
