<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">
      <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.id"
          ><i></i
          ></el-radio>
        </template>
      </ele-pro-table>
    </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 {getPSaleEntrustedReceiveDetailAPI} from '@/api/bpm/components/saleManage/entrustedReceive';

export default {
  props: {
    orderId: String,
    type: String
  },

  data() {
    return {
      visible: false,
      currentIndex: null,
      currentData: [], //已经存在的产品
      datasource: [],
      columns: [
        {
          action: 'action',
          slot: 'action',
          align: 'center',
          label: '选择',
          width: 50,
        },
        {
          columnKey: 'index',
          type: 'index',
          width: 50,
          align: 'center',
          showOverflowTooltip: true,
          label: '序号'
        },
        {
          width: 200,
          prop: 'productName',
          label: '名称',
          slot: 'productName',
          align: "center"
        },
        {
          width: 200,
          prop: 'productCode',
          label: '编码',
          slot: 'productCode',
          align: "center"
        },
        {
          width: 200,
          prop: 'productCategoryName',
          label: '类型',
          slot: 'productCategoryName',
          align: "center"
        },
        {
          width: 150,
          prop: 'productBrand',
          label: '牌号',
          slot: 'productBrand',
          align: "center"
        },
        {
          minWidth: 120,
          prop: 'modelType',
          label: '型号',
          slot: 'modelType',
          align: "center"
        },
        {
          minWidth: 120,
          prop: 'specification',
          label: '规格',
          slot: 'specification',
          align: "center"
        },
        {
          width: 80,
          prop: 'totalCount',
          label: '数量',
          slot: 'totalCount',
          align: "center"
        },
        {
          minWidth: 100,
          prop: 'measuringUnit',
          label: '计量单位',
          slot: 'measuringUnit',
          align: "center"
        },
        {
          width: 120,
          prop: 'singleWeight',
          label: '单重',
          slot: 'singleWeight',
          align: "center"
        },
        {
          width: 100,
          prop: 'receiveTotalWeight',
          label: '收货总重',
          slot: 'receiveTotalWeight',
          align: "center"
        },
        {
          width: 100,
          prop: 'weightUnit',
          label: '重量单位',
          slot: 'weightUnit',
          align: "center"
        },
        {
          width: 80,
          prop: 'singlePrice',
          label: '单价',
          slot: 'singlePrice',
          align: "center"
        },
        {
          width: 100,
          prop: 'discountSinglePrice',
          label: '折让单价',
          slot: 'discountSinglePrice',
          align: "center"
        },
        {
          width: 80,
          prop: 'totalPrice',
          label: '合计',
          slot: 'totalPrice',
          align: "center"
        },
        {
          width: 100,
          prop: 'discountTotalPrice',
          label: '折让合计',
          slot: 'discountTotalPrice',
          align: "center"
        },
      ],

      radio: null
    };
  },

  watch: {
    orderId(id) {
      if (id) {
        this.getSaleOrderDetail(id);
      }
    }
  },
  methods: {
    open(data) {
      this.currentData = data || [];
      this.visible = true;
    },
    //获取订单详情
    async getSaleOrderDetail(id) {
      this.loading = true;
      const data = await getPSaleEntrustedReceiveDetailAPI(id)
      this.loading = false;
      if (data) {
        this.$nextTick(() => {
          let {productList} = data;
          this.datasource = productList;
        });
      }
    },

    /* 刷新表格 */
    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('请至少选择一条数据');
      }
      let idKey =
        this.type == 'receive' ? 'receiveProductId' : 'sendProductId';
      console.log(this.current);
      console.log(this.currentData);
      // return;
      let index = this.currentData.findIndex(
        (r) => r[idKey] == this.current.id
      );
      if (index != -1 && ['receive', 'send'].includes(this.type)) {
        return this.$message.error('选择的产品已经存在列表了');
      }
      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>
