<template>
  <div class="app-container">
    <el-row>
      <div class="tabSetting">
        <div>
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            >导出
          </el-button>
        </div>
        <div style="display:flex">
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
          <div style="margin: 0 12px">
            <el-tooltip
              class="item"
              effect="dark"
              content="列设置"
              placement="top"
            >
              <el-button
                icon="el-icon-setting"
                size="mini"
                circle
                @click="showSetting = !showSetting"
              ></el-button>
            </el-tooltip>
          </div>
        </div>
      </div>
    </el-row>
    <el-dialog title="自定义列显示" :visible.sync="showSetting" width="700px">
      <div>配置排序列数据（拖动调整顺序）</div>
      <div style="margin-left: 17px">
        <el-checkbox
          v-model="allCheck"
          label="全选"
          @change="allChecked"
        ></el-checkbox>
      </div>
      <div style="padding: 4px; display: flex; justify-content: center">
        <draggable
          v-model="setRowList"
          group="site"
          animation="300"
          @start="onStart"
          @end="onEnd"
          handle=".indraggable"
        >
          <transition-group>
            <div
              v-for="item in setRowList"
              :key="item.surface"
              class="listStyle"
            >
              <div style="width: 500px" class="indraggable">
                <div class="progress" :style="{ width: item.width + 'px' }">
                  <el-checkbox
                    :label="item.name"
                    v-model="item.checked"
                    :true-label="0"
                    :false-label="1"
                    >{{ item.name }}
                  </el-checkbox>
                </div>
              </div>
              <el-input-number
                v-model.number="item.width"
                controls-position="right"
                :min="1"
                :max="500"
                size="mini"
              ></el-input-number>
            </div>
          </transition-group>
        </draggable>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showSetting = false">取 消</el-button>
        <el-button @click="delRow" type="danger">重 置</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </span>
    </el-dialog>

    <el-table v-loading="loading" :data="stockDate" ref="table" :height="tableHeight">
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column
        v-for="(item, index) in getRowList"
        :key="index"
        :label="item.name"
        :width="item.width"
        :prop="item.label"
        align="center"
        :show-overflow-tooltip="true"
        sortable
        :fixed="item.fixed"
      />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { financialStatistics, feesExport } from "@/api/outExcel/outExcel";
import { addSet, select, resetModule } from "@/api/system/set";
import Cookies from "js-cookie";
import draggable from "vuedraggable";

export default {
  name: "Warehousebills",
  components: {},
  data() {
    return {
      tableHeight: '0',
      // 遮罩层
      loading: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      stockDate: [],
      showSearch: true,
      showSetting: false,
      drag: false,
      setRowList: [],
      getRowList: [],
      tableDate: [
        {
          surface: "1",
          label: "userName",
          name: "对账人",
          checked: 0,
          width: 100,
        },
        {
          surface: "2",
          label: "corpName",
          name: "客户",
          checked: 0,
          width: 120,
        },
        {
          surface: "3",
          label: "feelMonth",
          name: "月份",
          checked: 0,
          width: 120,
        },
        {
          surface: "4",
          label: "ccf",
          name: "仓储费",
          checked: 0,
          width: 120,
        },
        {
          surface: "5",
          label: "fAmount",
          name: "出入库费",
          checked: 0,
          width: 120,
        },
        {
          surface: "6",
          label: "totalAmount",
          name: "合计人民币",
          checked: 0,
          width: 120,
        },
        {
          surface: "7",
          label: "accountPeriod",
          name: "账期",
          checked: 0,
          width: 120,
        },
        {
          surface: "8",
          label: "maturity",
          name: "协议到期日",
          checked: 0,
          width: 120,
        },
        {
          surface: "9",
          label: "isInvoice",
          name: "开发票情况",
          checked: 0,
          width: 150,
        },
        {
          surface: "10",
          label: "isAccountPeriod",
          name: "是否超账期",
          checked: 0,
          width: 150,
        },
        {
          surface: "11",
          label: "collectionResult",
          name: "应收款催收结果",
          checked: 0,
          width: 150,
        },
      ],
      allCheck: false,
    };
  },
  created() {
    this.setRowList = this.tableDate;
    this.getRowList = this.tableDate;
    this.getList();
    this.getRow();
  },
  mounted() {
    this.$nextTick(() => {
      // 根据浏览器高度设置初始高度
      this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 160
      // 监听浏览器高度变化，改变表格高度
      window.onresize = () => {
        this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 70
      }
    })
  },
  methods: {
    //列设置全选
    allChecked() {
      if (this.allCheck == true) {
        this.setRowList.map((e) => {
          return (e.checked = 0);
        });
      } else {
        this.setRowList.map((e) => {
          return (e.checked = 1);
        });
      }
    },
    //查询列数据
    getRow() {
      let that = this;
      this.data = {
        tableName: "总账统计",
        userId: Cookies.get("userName"),
      };
      select(this.data).then((res) => {
        if (res.data.length != 0) {
          this.getRowList = res.data.filter((e) => e.checked == 0);
          this.setRowList = res.data;
          this.setRowList = this.setRowList.reduce((res, item) => {
            res.push({
              surface: item.surface,
              label: item.label,
              name: item.name,
              checked: item.checked,
              width: item.width,
              fixed: item.fixed,
            });
            return res;
          }, []);
        }
      });
    },
    //重置列表
    delRow() {
      this.data = {
        tableName: "总账统计",
        userId: Cookies.get("userName"),
      };
      resetModule(this.data).then((res) => {
        if (res.code == 200) {
          this.showSetting = false;
          this.setRowList = this.tableDate;
          this.getRowList = this.tableDate;
        }
      });
    },
    //保存列设置
    save() {
      this.showSetting = false;
      this.data = {
        tableName: "总账统计",
        userId: Cookies.get("userName"),
        sysTableSetList: this.setRowList,
      };
      addSet(this.data).then((res) => {
        if (res.code == 200) {
          this.showSetting = false;
          this.getRowList = this.setRowList.filter((e) => e.checked == 0);
        }
      });
    },
    //开始拖拽事件
    onStart() {
      this.drag = true;
    },
    //拖拽结束事件
    onEnd() {
      this.drag = false;
    },
    getList() {
      financialStatistics(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.stockDate = res.rows;
          this.total = res.total;
          this.loading = false;
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有订单主数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return feesExport(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.tabSetting {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}
.listStyle {
  display: flex;
  border-top: 1px solid #dcdfe6;
  border-left: 1px solid #dcdfe6;
  border-right: 1px solid #dcdfe6;
}
.listStyle:last-child {
  border-bottom: 1px solid #dcdfe6;
}
.progress {
  display: flex;
  align-items: center;
  padding: 2px;
  background-color: rgba(0, 0, 0, 0.05);
  height: 100%;
}
</style>
