<template>
  <basic-container>
    <div class="avue-crud">
      <el-row>
        <div class="avue-crud__header">
          <!-- 头部左侧按钮模块 -->
          <div class="avue-crud__left">
            <el-button type="danger" icon="el-icon-bottom" @click="handleExportInfo" plain
              >导出
            </el-button>
          </div>
          <div class="avue-crud__left"></div>
          <!-- 头部右侧按钮模块 -->
          <div class="avue-crud__right">
            <el-button icon="el-icon-refresh" @click="searchChange" circle></el-button>
            <el-button icon="Operation" @click="showdrawer(true)" circle></el-button>
            <!-- <el-button icon="el-icon-search" @click="searchHide" circle></el-button> -->
          </div>
        </div>
      </el-row>
      <el-row>
        <!-- 列表模块 -->
        <tablecmt
          :columnList="columnList"
          :tableData="data"
          :loading="loading"
          @inputTxt="inputsc"
          @timeCheck="timesc"
          @btnCheck="btnsc"
          @selectCheck="selectsc"
          @selection="selectionChange"
        >
          <!--          <template #default="slotProps">-->
          <!--            <el-button type="primary" text icon="el-icon-view" @click="handleView(slotProps.scope)">查看</el-button>-->
          <!--            <el-button type="primary" text icon="el-icon-edit" @click="handleCallDeliveryOwn(slotProps.scope)">修改客户信息</el-button>-->
          <!--          </template>-->
        </tablecmt>
      </el-row>
      <el-row class="el-fy">
        <div class="avue-crud__pagination" style="width: 100%">
          <!-- 分页模块 -->
          <el-pagination
            align="right"
            background
            @size-change="sizeChange"
            @current-change="currentChange"
            :current-page="page.currentPage"
            :page-sizes="[30, 50, 80, 120]"
            :page-size="page.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
          >
          </el-pagination>
        </div>
      </el-row>
      <!-- 表单模块 -->
    </div>
  </basic-container>
  <edittablehead
    @closce="showdrawer"
    :drawerShow="drawerShow"
    :columnList="columnList"
    v-model="columnList"
  ></edittablehead>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove,
  $_warehouseStock,
} from '@/api/warehouse/warehouseStock';
import option from '@/option/warehouse/warehouseStock';
import { mapGetters } from 'vuex';
import { ElMessage } from 'element-plus';
import { downloadXls, checkParams } from '@/utils/util';
import dayjs from 'dayjs';
export default {
  data() {
    return {
      drawerShow: false,
      columnList: [
        {
          prop: '',
          label: '复选框',
          type: 0,
          values: '',
          width: '55',
          checkarr: [],
          fixed: true,
          sortable: false,
          head: false,
        },
        {
          prop: 'nodeName',
          label: '节点名称',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: true,
          sortable: false,
          head: false,
        },

        {
          prop: 'scanNodeType',
          label: '节点描述',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: true,
          sortable: false,
          head: false,
        },
        {
          prop: 'operatorTime',
          label: '操作时间',
          type: 4,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'serviceNumber',
          label: '服务号',
          type: 2,
          values: '',
          width: '180',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'waybillNumber',
          label: '运单号',
          type: 2,
          values: '',
          width: '180',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'orderCode',
          label: '订单自编码',
          type: 2,
          values: '',
          width: '200',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'orderPackageCode',
          label: '包件码',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'serviceNumber',
          label: '服务号',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'brandName',
          label: '品牌',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'materialCode',
          label: '物料编号',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'materialName',
          label: '物料名称',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'firsts',
          label: '一级品',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'second',
          label: '二级品',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          prop: 'thirdProduct',
          label: '三级品',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },
        {
          label: '包件状态',
          prop: 'orderPackageStatusName',
          search: true,
          width: '130',
        },
        {
          label: '包件冻结状态',
          prop: 'orderPackageFreezeStatusName',
          search: true,
          width: '150',
        },
        {
          label: '包件上架状态',
          prop: 'orderPackageGroundingStatusName',
          search: true,
          width: '150',
        },
        {
          label: '包件备货状态',
          prop: 'orderPackageStockupStatusName',
          search: true,
          width: '150',
        },
        {
          label: '包件预约状态',
          prop: 'orderPackageReservationStatusName',
          search: true,
          width: '150',
        },
        {
          label: '包件装车状态',
          prop: 'orderPackageLoadingStatusName',
          search: true,
          width: '150',
        },
        {
          prop: 'warehouseEntryTimeEnd',
          label: '入库时间',
          type: 2,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: true,
        },

        {
          prop: 'operatorName',
          label: '操作员',
          type: 1,
          values: '',
          width: '130',
          checkarr: [],
          fixed: false,
          sortable: false,
        },

        // {
        //   prop: '',
        //   label: '操作',
        //   type: 6,
        //   values: '',
        //   width: '230',
        //   checkarr: [],
        //   fixed: 'right',
        //   hide: true,
        // },
        // 更多列的配置...
      ],
      height: 0,
      // 弹框标题
      title: '',
      // 是否展示弹框
      box: false,
      // 是否显示查询
      search: true,
      // 加载中
      loading: false,
      // 是否为查看模式
      view: false,
      // 查询信息
      query: {},
      // 分页信息
      page: {
        currentPage: 1,
        pageSize: 30,
        total: 40,
      },
      // 表单数据
      form: {},
      // 选择行
      selectionList: [],
      // 表单配置
      option: option,
      // 表单列表
      data: [],
    };
  },
  mounted() {
    this.init();
    // this.onLoad(this.page);
  },
  computed: {
    ...mapGetters(['permission']),
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(',');
    },
  },
  methods: {
    // 导出表格
    handleExportInfo() {
      let row = {};
      if (!!this.ids) {
        row.ids = this.ids;
      } else {
        row.ids = '';
      }
      $_warehouseStock(row).then(res => {
        console.log(res.data);
        downloadXls(res.data, '扫描记录.xlsx');
      });
    },
    showdrawer(value) {
      this.drawerShow = value;
    },

    selectionsc(value) {
      console.log('11111', value);
    },
    delectsolt(scope) {
      const { row } = scope;
      console.log('2222222', row);
    },
    editsolt(scope) {
      const { row } = scope;
      console.log('33333', row);
    },
    btnsc(index, row) {
      console.log('4444444', index, row);
    },
    selectsc(index, row) {
      console.log(index, row);
    },

    checkParams(data, targetParam) {
      const keys = Object.keys(data);

      if (
        keys.includes(targetParam) &&
        keys.every(key => key === targetParam || data[key] === '')
      ) {
        return true; // 指定参数存在且没有其他参数或其他参数值为空时返回 true
      } else {
        return false; // 其他情况返回 false
      }
    },

    timesc(index, row) {
      // index = this.formatDate(index);
      if (!!index) {
        index = dayjs(index).format('YYYY-MM-DD');
      }
      this.query[row.prop] = index;
      // if(checkParams(this.query, 'operatorTime')){
      //   ElMessage({
      //   message: '请携带其他任意参数',
      //   type: 'warning',
      // })
      //   return
      // }
      this.onLoad(this.page);
    },
    inputsc(index, row) {
      this.query[row.prop] = index;
      console.log(this.checkParams(this.query, 'operatorTime'), '22222'); // 输出 true
      if (this.query.operatorTime) {
        if (checkParams(this.query, 'operatorTime')) {
          ElMessage({
            message: '请携带其他任意参数',
            type: 'warning',
          });
          return;
        }
      } else {
        ElMessage({
          message: '请选择日期参数',
          type: 'warning',
        });
      }

      this.onLoad(this.page);
    },
    init() {
      this.height = this.setPx(document.body.clientHeight - 340);
    },
    searchHide() {
      this.search = !this.search;
    },
    searchChange() {
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },
    handleSubmit() {
      if (!this.form.id) {
        add(this.form).then(() => {
          this.box = false;
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
      } else {
        update(this.form).then(() => {
          this.box = false;
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
      }
    },
    handleAdd() {
      this.title = '新增';
      this.form = {};
      this.box = true;
    },
    handleEdit(row) {
      this.title = '编辑';
      this.box = true;
      getDetail(row.id).then(res => {
        this.form = res.data.data;
      });
    },
    handleView(row) {
      this.title = '查看';
      this.view = true;
      this.box = true;
      getDetail(row.id).then(res => {
        this.form = res.data.data;
      });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.selectionClear();
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
        });
    },
    beforeClose(done) {
      done();
      this.form = {};
      this.view = false;
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      // this.$refs.table.clearSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query))
        .then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style scoped lang="scss">
:deep(.avue-crud) {
  display: flex;
  height: 100%;
  flex-direction: column;
}
:deep(.el-card__body) {
  height: 100%;
}
:deep(.el-card) {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.el-fy {
  flex: 1;
  display: flex;
  align-items: flex-end;
}
</style>