<template>
  <el-dialog
    title="选择产品"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    top="5vh"
    :close-on-press-escape="false"
    append-to-body
    width="80%"
  >
    <el-card shadow="never">
      <searchProduct @search="reload"></searchProduct>
      <ele-split-layout
        width="244px"
        allow-collapse
        :right-style="{ overflow: 'hidden' }"
      >
        <div class="ele-border-lighter split-layout-right-content">
          <productTree
            @handleNodeClick="handleNodeClick"
            :isFirstRefreshTable="false"
            ref="treeList"
          />
        </div>
        <!-- 表格 -->
        <template v-slot:content>
          <ele-pro-table
            ref="table"
            :columns="columns"
            :datasource="datasource"
            row-key="id"
            height="calc(100vh - 350px)"
            class="dict-table"
            @cell-click="cellClick"
          >
            <!-- 表头工具栏 -->
            <template v-slot:action="{ row }">
              <el-radio class="radio" v-model="radio" :label="row.code"
                ><i></i
              ></el-radio>
            </template>
          </ele-pro-table>
        </template>
      </ele-split-layout>
    </el-card>

    <div class="btns">
      <el-button type="primary" size="small" @click="selected">选择</el-button>
      <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getProductList } from '@/api/bpm/components/saleManage/contact';
  import searchProduct from './searchProduct.vue';
  import productTree from '@/components/productTree';
  export default {
    components: {
      productTree,
      searchProduct
    },

    data() {
      return {
        visible: false,
        currentIndex: null,
        columns: [
          {
            action: 'action',
            slot: 'action',
            align: 'center',
            label: '选择'
          },
          {
            columnKey: 'index',
            type: 'index',
            width: 50,
            align: 'center',
            showOverflowTooltip: true,
            label: '序号'
          },
          {
            prop: 'code',
            label: '编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'name',
            label: '名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },

          {
            prop: 'brandNum',
            align: 'center',
            label: '牌号',
            showOverflowTooltip: true
          },
          {
            prop: 'modelType',
            label: '型号',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'specification',
            label: '规格',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'measuringUnit',
            label: '计量单位',
            showOverflowTooltip: true,
            minWidth: 90
          },

          {
            prop: 'weightUnit',
            label: '重量单位',
            showOverflowTooltip: true,
            minWidth: 90
          },

          {
            prop: 'roughWeight',
            label: '毛重',
            showOverflowTooltip: true,
            minWidth: 90
          },

          {
            prop: 'netWeight',
            label: '净重',
            showOverflowTooltip: true,
            minWidth: 90
          },

          {
            prop: 'packingUnit',
            align: 'center',
            label: '包装单位',
            showOverflowTooltip: true
          },
          {
            prop: 'categoryLevelPath',
            label: '分类',
            align: 'center',
            showOverflowTooltip: true
          }
        ],
        radio: null
      };
    },

    watch: {},
    methods: {
      open(item, currentIndex) {
        this.currentIndex = currentIndex;
        if (item && item.id) {
          this.radio = item.id;
        }

        this.visible = true;
      },

      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return getProductList({
          pageNum: page,
          size: limit,
          ...where
        });
      },

      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ pageNum: 1, where: where });
      },

      handleNodeClick(data, node) {
        this.curNodeData = data;
        this.reload({ categoryLevelId: data.id });
      },

      // 单击获取id
      cellClick(row) {
        this.current = row;
        this.radio = row.id;
      },
      handleClose() {
        this.visible = false;
        this.current = null;
        this.radio = '';
      },

      selected() {
        if (!this.current) {
          return this.$message.warning('请至少选择一条数据');
        }
        this.$emit('changeParent', this.current, this.currentIndex);
        this.handleClose();
      }
    }
  };
</script>

<style lang="scss" scoped>
  .tree_col {
    border: 1px solid #eee;
    padding: 10px 0;
    box-sizing: border-box;
    height: 500px;
    overflow: auto;
  }

  .table_col {
    padding-left: 10px;

    ::v-deep .el-table th.el-table__cell {
      background: #f2f2f2;
    }
  }

  .pagination {
    text-align: right;
    padding: 10px 0;
  }

  .btns {
    text-align: center;
    padding: 10px 0;
  }

  .topsearch {
    margin-bottom: 15px;
  }
</style>
