<template>
  <el-dialog
    title="选择发货单"
    custom-class="ele-dialog-form long-dialog-form"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    top="5vh"
    :close-on-press-escape="false"
    append-to-body
    width="70%"
  >
    <el-card shadow="never">
      <searchTable @search="reload"></searchTable>
      <el-tabs
        v-model="activeName"
        style="margin-top: 15px"
        type="border-card"
        @tab-click="activeNameChange"
      >
        <el-tab-pane label="发货单" name="1">
          <ele-pro-table
            ref="table"
            :selectable="getSelectAble"
            :columns="columns"
            :datasource="datasource"
            row-key="id"
            height="calc(100vh - 500px)"
            class="dict-table"
            :selection.sync="selectionList"
            @cell-click="cellClick"
          >
            <template v-slot:toolbar>
              <div>
                <el-radio-group v-model="queryType">
                  <el-radio :label="0">退物料/托盘</el-radio>
                  <el-radio :label="1">仅退物料</el-radio>
                  <el-radio :label="2">仅退托盘</el-radio>
                </el-radio-group>
              </div>
            </template>
            <!-- 表头工具栏 -->
            <template v-slot:action="{ row }">
              <el-radio class="radio" v-model="radio" :label="row.id"
                ><i></i
              ></el-radio>
            </template>
          </ele-pro-table>
        </el-tab-pane>
        <el-tab-pane label="发货确认单" name="2">
          <ele-pro-table
            ref="table1"
            :selectable="getSelectAble"
            :columns="columns1"
            :datasource="datasource1"
            row-key="id"
            height="calc(100vh - 500px)"
            class="dict-table"
            :selection.sync="selectionList"
            @cell-click="cellClick"
          >
            <template v-slot:toolbar>
              <div>
                <el-radio-group v-model="queryType">
                  <el-radio :label="0">退物料/托盘</el-radio>
                  <el-radio :label="1">仅退物料</el-radio>
                  <el-radio :label="2">仅退托盘</el-radio>
                </el-radio-group>
              </div>
            </template>
            <!-- 表头工具栏 -->
            <template v-slot:action="{ row }">
              <el-radio class="radio" v-model="radio" :label="row.id"
                ><i></i
              ></el-radio>
            </template>
          </ele-pro-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>

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

<script>
  import { contactTypeTree } from '@/api/saleManage/contact';
  import searchTable from '@/views/saleManage/saleOrder/invoice/components/searchTable.vue';
  import { getSendTableList } from '@/api/saleManage/saleordersendrecord';
  import { reviewStatus } from '@/enum/dict';
  import { getSendConfirmTableList } from '@/api/saleManage/invoiceConfirm';

  export default {
    components: {
      searchTable
    },
    //客户管理数据
    props: {
      contactData: {
        type: Object,
        default: () => {
          return {};
        }
      },
      saleOrderData: {
        type: Object,
        default: () => {
          return {};
        }
      }
    },
    data() {
      return {
        visible: false,
        currentIndex: null,
        queryType: 0,
        selectionList: [],
        radio: null,
        activeName: '1'
      };
    },
    computed: {
      columns() {
        return [
          // {
          //   width: 45,
          //   type: 'selection',
          //   columnKey: 'selection',
          //   align: 'center',
          //   selectable: (row, index) => {
          //     if (this.selectionList.length > 0) {
          //       return this.selectionList[0].orderNo == row.orderNo;
          //     } else {
          //       return true;
          //     }
          //   }
          // },
          {
            action: 'action',
            slot: 'action',
            align: 'center',
            label: '选择'
          },
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },

          // {
          //   action: 'action',
          //   slot: 'action',
          //   align: 'center',
          //   label: '选择'
          // },

          {
            prop: 'docNo',
            label: '发货编码',
            align: 'center',
            slot: 'docNo',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'orderNo',
            label: '订单编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'contactName',
            label: '客户名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 180
          },
          {
            prop: 'replied',
            label: '是否回执',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200,
            formatter: (_row, _column, cellValue) => {
              return _row.replied == 1 ? '是' : '否';
            }
          },
          {
            prop: 'reviewStatus',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200,
            formatter: (_row, _column, cellValue) => {
              return reviewStatus[_row.reviewStatus];
            }
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 170
          }
        ];
      },
      columns1() {
        return [
          // {
          //   width: 45,
          //   type: 'selection',
          //   columnKey: 'selection',
          //   align: 'center',
          //   selectable: (row, index) => {
          //     if (this.selectionList.length > 0) {
          //       return this.selectionList[0].orderNo == row.orderNo;
          //     } else {
          //       return true;
          //     }
          //   }
          // },
          {
          action: 'action',
          slot: 'action',
          align: 'center',
          label: '选择'
        },
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },

          // {
          //   action: 'action',
          //   slot: 'action',
          //   align: 'center',
          //   label: '选择'
          // },

          {
            prop: 'docNo',
            label: '确认单编码',
            align: 'center',
            sortable: true,
            slot: 'docNo',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'sendNo',
            label: '发货单编码',
            align: 'center',
            sortable: true,
            slot: 'sendNo',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'orderNo',
            label: '订单编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'contactName',
            label: '客户名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 180
          },
          {
            prop: 'replied',
            label: '是否回执',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200,
            formatter: (_row, _column, cellValue) => {
              return _row.replied == 1 ? '是' : '否';
            }
          },
          {
            prop: 'reviewStatus',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200,
            formatter: (_row, _column, cellValue) => {
              return reviewStatus[_row.reviewStatus];
            }
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 170
          }
        ];
      }
    },
    methods: {
      open(item, currentIndex) {
        this.activeName = '1';
        this.currentIndex = currentIndex;
        if (item && item.id) {
          this.radio = item.id;
        }

        this.visible = true;
      },
      getSelectAble(row) {
        console.log(this.selectionList);
        if (this.selectionList.length > 0) {
          return this.selectionList[0].orderNo == row.orderNo;
        } else {
          return true;
        }
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        if (this.contactData.id) {
          where['contactId'] = this.contactData.id;
        }
        if (this.saleOrderData.id) {
          where['orderNo'] = this.saleOrderData.orderNo;
        }

        return getSendTableList({
          pageNum: page,
          size: limit,
          ...where,
          reviewStatus: 2
        });
      },
      /* 表格数据源 */
      datasource1({ page, limit, where, order }) {
        if (this.contactData.id) {
          where['contactId'] = this.contactData.id;
        }
        if (this.saleOrderData.id) {
          where['orderNo'] = this.saleOrderData.orderNo;
        }

        return getSendConfirmTableList({
          pageNum: page,
          size: limit,
          ...where,
          reviewStatus: 2
        });
      },
      activeNameChange() {
        this.selectionList = [];
        this.reload();
      },
      /* 刷新表格 */
      reload(where) {
        where = {
          ...where,
          reviewStatus: 2
        };
        if (this.activeName == 1) {
          this.$refs.table.reload({ pageNum: 1, where: where });
          console.log(
            this.$refs.table.setSelectedRowKeys,
            'this.$refs.table.setSelectedRowKeys'
          );
          this.$refs.table.setSelectedRowKeys([]);
          this.radio=''
        } else {
          this.$refs.table1.reload({ pageNum: 1, where: where });
          this.$refs.table1.setSelectedRowKeys([]);
          this.radio=''
        }
      },

      handleNodeClick(data, node) {
        this.reload({ categoryId: data.id });
      },

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

      selected() {
        if (!this.radio) {
          return this.$message.warning('请至少选择一条数据');
        }
        let params = {
          id: this.current.id,
          // queryType: this.queryType,
          activeName:this.activeName
        };
        this.$emit('changeParent', params);
        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;
  }
  /* 禁用全选框的样式 */
  ::v-deep .is-leaf .el-checkbox__inner {
    background-color: #f5f7fa !important;
    border-color: #dcdfe6 !important;
    cursor: not-allowed !important;
    pointer-events: none; /* 禁止点击事件 */
    display: none;
  }
</style>
