<template>
  <div>
    <el-dialog title="物料编码选择" width="1000px" :close-on-click-modal="false" :append-to-body="true" v-dialogDrag
      class="userDialog" :visible.sync="visible">
      <el-container style="height: 500px">
        <el-container>
          <el-header style="text-align: left; font-size: 12px; height: 30px">
            <el-form size="small" :inline="true" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()"
              @submit.native.prevent>
              <el-form-item prop="code">
                <el-input size="small" v-model.trim="searchForm.code" placeholder="请输入物料编号查询" clearable></el-input>
              </el-form-item>
              <el-form-item prop="name">
                <el-input size="small" v-model="searchForm.name" placeholder="请输入物料名称查询" clearable></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="refreshList()" size="small" icon="el-icon-search">查询</el-button>
                <el-button @click="resetSearch()" size="small" icon="el-icon-refresh-right">重置</el-button>
              </el-form-item>
            </el-form>
          </el-header>
          <el-main>
            <el-table :data="dataList" v-loading="loading" size="small" border ref="contractTable"
              @select="handleSelectionChange" :selection="selectedRows"  height="calc(100% - 40px)" style="width: 100%">
              <el-table-column type="selection" header-align="center" align="center" width="50">
              </el-table-column>
              <el-table-column prop="code" header-align="center" align="center" sortable="custom" min-width="90"
                label="物料编码">
              </el-table-column>
              <el-table-column prop="name" header-align="center" align="center" sortable="custom" min-width="90"
                label="物料名称">
              </el-table-column>
              <el-table-column prop="specification" header-align="center" align="center" sortable="custom" min-width="90"
                label="规格">
              </el-table-column>
              <el-table-column prop="model" header-align="center" align="center" sortable="custom" min-width="90"
                label="型号">
              </el-table-column>
              <el-table-column prop="manufacturerIdName" header-align="center" align="center" sortable="custom" min-width="90"
                label="生产厂家">
              </el-table-column>
            </el-table>

            <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle"
              :current-page="searchForm.pageNo" :page-sizes="[5, 10, 15, 20]" :page-size="searchForm.pageSize"
              :total="total" layout="total, sizes, prev, pager, next, jumper">
            </el-pagination>
          </el-main>
        </el-container>
      </el-container>
      <span slot="footer">
        <el-button size="small" @click="visible = false" icon="el-icon-circle-close">关闭</el-button>
        <el-button size="small" type="primary" icon="el-icon-circle-check" @click="doSubmit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
import { getMaterialList } from '@/api/changeApply/basic'
export default {
  data() {
    return {
      searchForm: {
        code: '',
        name: '',
        isSync: '',
        pageNo: 1,
        pageSize: 10,
      },
      dataListAllSelections: [], // 所有选中的数据包含跨页数据
      idKey: "id", // 标识列表数据中每一行的唯一键的名称(需要按自己的数据改一下)
      dataList: [],
      total: 0,
      orders: [],
      loading: false,
      visible: false,
      selectedRows:[]
    };
  },
  props: {
    selectData: {
      type: Array,
      default: () => {
        return [];
      },
    },
    // 是否启用单选
    single: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    init(val) {
      this.visible = true;
      this.searchForm.isSync = val
      this.$nextTick(() => {
        this.dataListAllSelections = JSON.parse(JSON.stringify(this.selectData));
        this.resetSearch();
      });
    },
    // 获取数据列表
    refreshList() {
      this.loading = true;
      // axios({
      //   url: "http://172.16.62.241:8000/drp-admin/system/material/list", // 自己的接口路径
      //   method: "post",
      //   data: {
      //     // current: this.pageNo,
      //     size: this.pageSize,
      //     // orders: this.orders,
      //     // ...this.searchForm,
      //   },
      let params = {
        ...this.searchForm,
        // 默认查询已启用
        isEnable: '0'
      }
      getMaterialList(params).then(({ data }) => {
        console.log('data', data)
        this.dataList = data.tableBody.rows;
        this.total = data.tableBody.total;
        this.loading = false;
        this.$nextTick(() => {
          this.setSelectRow();
        });
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      console.log('每页数：', val)
      this.searchForm.pageSize = val;
      this.refreshList();
    },
    // 当前页
    currentChangeHandle(val) {
      console.log('当前页：', val)
      this.searchForm.pageNo = val;
      this.refreshList();
    },
    // 排序
    resetSearch() {
      this.$refs['searchForm'].resetFields();
      this.searchForm.pageNo = 1;
      this.refreshList();
    },
    watchSel(rows) {
      console.log('监听得到选中吗', rows)
      this.dataListAllSelections = rows
    },
    // 表格选中数据
    rowSelect(row) {
      // this.$refs.contractTable.clearSelection();
      // this.$refs.contractTable.toggleRowSelection(row);
      // this.dataListAllSelections = this.single ? [row] : selection
      this.$refs.contractTable.toggleRowSelection(row);
    },
    // 选中数据
    handleSelectionChange(selection, row) {
      console.log('selection',selection)
      console.log('row',[row])
      if (this.single && selection.length > 1) {
        this.$refs.contractTable.clearSelection();
        this.$refs.contractTable.toggleRowSelection(row);
      }
      this.dataListAllSelections = this.single ? [row] : selection
    },
    // 设置选中的方法
    setSelectRow() {
      this.$refs.contractTable.clearSelection();
      if (!this.dataListAllSelections || this.dataListAllSelections.length <= 0) {
        return;
      }
      for (let i = 0; i < this.dataList.length; i++) {
        if (this.dataListAllSelections.some(item => item[this.idKey] == this.dataList[i][this.idKey])) {
          // 设置选中，记住table组件需要使用ref="table"
          this.$refs.contractTable.toggleRowSelection(this.dataList[i], true);
        }
      }
    },
    doSubmit() {
      this.visible = false;
      console.log('选择的数据？', this.dataListAllSelections)
      this.$emit("doSubmit", this.dataListAllSelections);
    },
  },
};
</script>
<style lang="scss">
.userDialog {
  .el-dialog__body {
    padding: 10px 0px 0px 10px;
    color: #606266;
    font-size: 14px;
    word-break: break-all;
  }

  .el-main {
    padding: 20px 20px 5px 20px;

    .el-pagination {
      margin-top: 5px;
    }
  }
}
</style>