<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"
    v-if="visible"
    :close-on-press-escape="false"
    append-to-body
    width="70%"
  >
    <el-card shadow="never">
      <searchContract @search="reload"></searchContract>
      <ele-split-layout
        width="244px"
        allow-collapse
        :right-style="{ overflow: 'hidden' }"
      >
        <div class="ele-border-lighter split-layout-right-content">
          <AssetTree
            @handleNodeClick="handleNodeClick"
            id="20"
            :isFirstRefreshTable="false"
            ref="treeList"
            :filterCode="this.type"
          />
        </div>
        <!-- 表格 -->
        <template v-slot:content>
          <ele-pro-table
            ref="table"
            :columns="columns"
            :datasource="datasource"
            row-key="id"
            height="calc(100vh - 500px)"
            class="dict-table"
            @cell-click="cellClick"
          >
            <!-- 表头工具栏 -->
            <template v-slot:action="{ row }">
              <el-radio
                class="radio"
                v-model="radio"
                :label="row.id"
                v-if="!isAcc"
                ><i></i
              ></el-radio>
              <el-button
                v-if="isAcc"
                :disabled="row.hasInvoiceApply"
                type="primary"
                @click="selected1(row)"
              >
                选择
              </el-button>
            </template>
          </ele-pro-table>
        </template>
      </ele-split-layout>
    </el-card>

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

<script>
  import { contactTypeTree } from '@/api/saleManage/contact';
  import searchContract from './searchContract.vue';
  import { getTableList } from '@/api/contractManage/contractBook';
  import AssetTree from '@/components/AssetTree';

  export default {
    components: {
      AssetTree,
      searchContract
    },
    props: {
      type: {
        default: 1,
        type: Number
      },
      hasGeneratedOrder: {
        default: 0,
        type: [Number, String]
      },
      contactData: {
        type: Object,
        default: () => {
          return {};
        }
      },
      isChange: {
        default: false,
        type: Boolean
      },
      isAdd: { //对账单选择合同 
        default: false,
        type: Boolean
      },
      sourceType:{
        default: '',
        type: String
      }
    },
    data() {
      return {
        visible: false,
        currentIndex: null,
        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            action: 'action',
            slot: 'action',
            align: 'center',
            label: '选择',
            minWidth: 100
          },
          {
            prop: 'contractNo',
            label: '合同编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 170
          },
          {
            prop: 'contractNumber',
            label: '合同编号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'contractName',
            label: '合同名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'typeName',
            label: '合同类型',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'partaName',
            label: '甲方名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'partaLinkName',
            label: '甲方联系人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 130
          },

          {
            prop: 'partaTel',
            label: '甲方联系电话',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 130
          },
          {
            prop: 'partbName',
            label: '乙方名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 130
          },
          {
            prop: 'partbLinkName',
            label: '乙方联系人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 130
          },
          {
            prop: 'partbTel',
            label: '乙方联系电话',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 130
          },

          {
            prop: 'enabled',
            label: '是否可用',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140,
            formatter: (_row, _column, cellValue) => {
              return _row.enabled ? '是' : '否';
            }
          },
          {
            prop: 'contractStartDate',
            label: '签订日期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'contractEndDate',
            label: '结束日期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'receiptPaymentType',
            label: '收付款类型',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 130,
            formatter: (_row, _column, cellValue) => {
              return _row.receiptPaymentType == 1 ? '固定' : '分期';
            }
          },
          {
            prop: 'totalPrice',
            label: '合同总金额',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 130
          },

          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          }
        ],
        isAcc: false, //是否对账
        radio: null
      };
    },

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

        this.visible = true;
      },

      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        if (this.contactData.id) {
          where['contactId'] = this.contactData.id;
        }
        if (this.type) {
          where['type'] = this.type;
        }
        if(this.isChange){
          where['ifRelation'] =0;
        }
        if(this.sourceType){
          where['requireSourceType'] = this.sourceType;
        }
        let params = {
          pageNum: page,
          size: limit,

          status: 2,
          ...where
        };
        // console.log(this.hasGeneratedOrder);
        // if(this.hasGeneratedOrder!=3) params.hasGeneratedOrder = this.hasGeneratedOrder
        return getTableList(params);
      },

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

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

      // 单击获取id
      cellClick(row) {
        this.current = row;
        console.log(row);
        this.radio = row.id;
      },
      handleClose() {
        this.visible = false;
        this.current = null;
        this.radio = '';
      },
      async addConfirm() {
        return await this.$confirm('该合同已经生成过订单, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return true;
          })
          .catch(() => {
            return false;
          });
      },
      async selected() {
        if (!this.current) {
          return this.$message.warning('请至少选择一条数据');
        }
        if (this.isChange) {
          this.$emit('changeParent', this.current, this.currentIndex);
          this.handleClose();
          return
        }
        if (this.isAdd) {
          this.$emit('changeParent', this.current, this.currentIndex);
          this.handleClose();
          return
        }
        if (this.current.hasGeneratedOrder > 0 && !(await this.addConfirm())) {
          return;
        }

        this.$emit('changeParent', this.current, this.currentIndex);
        this.handleClose();
      },
      selected1(item) {
        this.$emit('changeParent', item);
        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>
