<template>
  <div>
    <el-dialog :title="reciveForm.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="param" label="名称/编号">
                <el-input size="small" v-model="searchForm.search" 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" @row-dblclick="choose"  @row-click="rowSelect" height="calc(100% - 40px)">
              <el-table-column show-overflow-tooltip type="selection" header-align="center" align="center" width="50"/>
              <!-- <el-table-column show-overflow-tooltip prop="id" header-align="center" align="center" min-width="90" :label="reciveForm.title + 'id'"/> -->
              <el-table-column show-overflow-tooltip prop="code" header-align="center" align="center" min-width="90" label="编码"/>
              <el-table-column show-overflow-tooltip prop="name" header-align="center" align="center" min-width="90" label="名称"/>
              <el-table-column show-overflow-tooltip v-if="warehouseAttributes" prop="warehouseAttributes" header-align="center" align="center" min-width="90" label="仓库属性"/>
              <el-table-column show-overflow-tooltip v-if="orgName" prop="orgName" header-align="center" align="center" min-width="90" label="所属组织"/>
              <el-table-column show-overflow-tooltip v-if="warehouseCode" prop="warehouseCode" header-align="center" align="center" min-width="90" label="所属仓库编码"/>
              <el-table-column show-overflow-tooltip v-if="warehouseName" prop="warehouseName" header-align="center" align="center" min-width="90" label="所属仓库"/>
              <el-table-column show-overflow-tooltip v-if="warehouseProperty" prop="warehouseProperty" header-align="center" align="center" min-width="90" label="仓库属性" :formatter="cksx"/>
              <el-table-column show-overflow-tooltip v-if="contactsName" prop="contactsName" header-align="center" align="center" min-width="90" label="联系人"/>
              <el-table-column show-overflow-tooltip v-if="contactsPhone" prop="contactsPhone" header-align="center" align="center" min-width="90" label="联系人电话"/>
              <el-table-column show-overflow-tooltip v-if="address" prop="address" header-align="center" align="center" min-width="90" label="详细地址"/>
              <el-table-column show-overflow-tooltip v-if="materialClassifyName" prop="materialClassifyName" header-align="center" align="center" min-width="90" label="物料分类"/>
              <el-table-column show-overflow-tooltip v-if="remark" prop="remark" header-align="center" align="center" min-width="90" label="备注"/>
              <el-table-column show-overflow-tooltip v-if="shortName" prop="shortName" header-align="center" align="center" min-width="90" label="简称"/>
              <el-table-column show-overflow-tooltip v-if="mneCode" prop="mneCode" header-align="center" align="center" min-width="90" label="助记码"/>
              <el-table-column show-overflow-tooltip v-if="deptName" prop="deptName" header-align="center" align="center" min-width="90" label="部门"/>
            </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 { getRefer } from '@/api/purchase/basic'
export default {
  data() {
    return {
      searchForm: {
        search: '',
        pageNo: 1,
        pageSize: 10,
      },
      reciveForm: {},
      dataListAllSelections: [], // 所有选中的数据包含跨页数据
      idKey: "id", // 标识列表数据中每一行的唯一键的名称(需要按自己的数据改一下)
      dataList: [],
      total: 0,
      orgName: false,
      deptName: false,
      warehouseAttributes: false,
      warehouseCode: false,
      warehouseName: false,
      warehouseProperty: false,
      contactsName: false,
      contactsPhone: false,
      address: false,
      materialClassifyName: false,
      remark: false,
      shortName: false,
      mneCode: false,
      orders: [],
      loading: false,
      visible: false,
    };
  },
  props: {
    selectData: {
      type: Array,
      default: () => {
        return [];
      },
    },
    // 是否启用单选
    single: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    cksx(row) {
      switch (row.warehouseProperty) {
        case '1':
          return '电商库存'
        case '2':
          return '区域分仓公共库存'
        case '3':
          return '各项目仓库存'
        case '4':
          return '中心仓公共库存'
        case '5':
          return '事业发展部仓库'
      }
    },
    init(val) {
      this.visible = true;
      console.log("🚀 ~ file: refers.vue:79 ~ init ~ init(val):", val)
      this.reciveForm = val
      this.searchForm.search = ''
      this.$nextTick(() => {
        this.dataListAllSelections = JSON.parse(JSON.stringify(this.selectData));
        this.resetSearch();
      });
    },
    // 获取数据列表
    refreshList() {
      this.loading = true;
      let params = {...this.searchForm, ...this.reciveForm}
      getRefer(params).then(res => {
        console.log('res', res)
        this.dataList = res.rows;
        this.total = res.total
        // 仓库里面加组织
        if(res.rows.length !== 0 && res.rows[0].orgName) {
          this.orgName = true
        } else {
          this.orgName = false
        }
        // 仓库里面加仓库属性
        if(res.rows.length !== 0 && res.rows[0].warehouseAttributes) {
          this.warehouseAttributes = true
        } else {
          this.warehouseAttributes = false
        }
        // 人员内加部门
        if(res.rows.length !== 0 && res.rows[0].deptId) {
          this.deptName = true
        } else {
          this.deptName = false
        }
        // 选择货位里面加仓库
        if(res.rows.length !== 0 && res.rows[0].warehouseCode) {
          this.warehouseCode = true
          this.warehouseName = true
        } else {
          this.warehouseCode = false
          this.warehouseName = false
        }
        // 选择收货地址内加详细信息
        if(res.rows.length !== 0 && res.rows[0].contactsName) {
          this.contactsName = true
          this.contactsPhone = true
          this.warehouseProperty = true
          this.address = true
          this.materialClassifyName = true
          this.remark = true
        } else {
          this.contactsName = false
          this.contactsPhone = false
          this.warehouseProperty = false
          this.address = false
          this.materialClassifyName = false
          this.remark = false
        }
        // 补单供应商档案
        if (res.rows.length !== 0 && res.rows[0].mneCode) {
          this.shortName = true
          this.mneCode = true
        } else {
          this.shortName = false
          this.mneCode = false
        }
        this.loading = false;
        this.$nextTick(() => {
          this.setSelectRow();
        });
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.searchForm.pageSize = val;
      this.refreshList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.searchForm.pageNo = val;
      this.refreshList();
    },
    // 排序
    resetSearch() {
      this.searchForm.pageNo = 1
      this.searchForm.search = ''
      this.refreshList();
    },
    // 表格选中数据
    rowSelect(row, column, event) {
      this.$refs.contractTable.clearSelection();
      this.$refs.contractTable.toggleRowSelection(row);
      this.dataListAllSelections = this.single ? [row] : selection
    },
    // 选中数据
    handleSelectionChange(selection, 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);
    },
    choose() {
      this.doSubmit()
    }
  },
};
</script>
<style lang="scss" scoped>
.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;
    }
  }
}
::v-deep .el-table__header-wrapper .el-checkbox {
  // display: none;//设置不成功，页面卡顿
  visibility: hidden;
}
</style>